Targ

0.6.0 · active · verified Thu Apr 16

Targ is a Python library that simplifies the creation of command-line interfaces (CLIs) for applications, leveraging Python's type hints and docstrings. It automatically generates CLI arguments and help text from function signatures, reducing boilerplate. The current version is 0.6.0. It maintains an active release cadence, with recent updates focusing on modern Python version support and type annotation syntax.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates creating a simple CLI with `targ` by decorating a function. The `greet` function defines two parameters, `name` (required string) and `greeting` (optional string with a default). `targ` automatically converts these into command-line arguments, inferring types and descriptions from the type hints and docstring. The `CLI(greet)()` call registers the function as a command-line entry point.

from targ import CLI

def greet(name: str, greeting: str = "Hello"):
    """
    Say hello to someone with an optional greeting.

    :param name: The name of the person to greet.
    :param greeting: The greeting message. Defaults to 'Hello'.
    """
    print(f"{greeting}, {name}!")

if __name__ == "__main__":
    # To run: python your_script.py greet --name World
    # Or: python your_script.py greet --name Alice --greeting Hi
    CLI(greet)()

view raw JSON →