Skip to main content

Automatically generate CLI interfaces from function signatures and docstrings

Project description

philiprehberger-docstring-cli

Tests PyPI version License

Automatically generate CLI interfaces from function signatures and docstrings.

Installation

pip install philiprehberger-docstring-cli

Usage

With the @cli decorator

from philiprehberger_docstring_cli import cli


@cli
def greet(name: str, count: int = 1, loud: bool = False):
    """Greet someone by name.

    Args:
        name: The person to greet.
        count: Number of times to greet.
        loud: Whether to shout.
    """
    greeting = f"Hello, {name}!"
    if loud:
        greeting = greeting.upper()
    for _ in range(count):
        print(greeting)


# Call from CLI: python greet.py Alice --count 3 --loud
# Or call normally: greet("Alice", count=3, loud=True)

The decorated function can still be called normally with arguments. When called with no arguments, it parses sys.argv and runs as a CLI command.

Use .cli(argv=[...]) to pass explicit arguments:

greet.cli(["Alice", "--count", "2", "--loud"])

With run()

For one-off usage without decorating:

from philiprehberger_docstring_cli import run


def add(a: int, b: int):
    """Add two numbers.

    Args:
        a: First number.
        b: Second number.
    """
    return a + b


run(add, ["3", "4"])  # prints 7

API

Name Description
cli Decorator that makes a function callable from the CLI.
run Run any function as a CLI command without decorating it.

@cli

  • Reads type hints to set argument types (int, float, str, etc.)
  • Parameters without defaults become positional arguments
  • Parameters with defaults become optional --flags
  • bool parameters become --flag / --no-flag toggles
  • Underscore parameter names are converted to hyphenated flags (dry_run -> --dry-run)
  • Google-style docstring Args: sections are used for help text

run(func, argv=None)

  • Builds a parser from the function and parses the given argv (or sys.argv[1:])
  • Does not require the @cli decorator

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

philiprehberger_docstring_cli-0.1.1.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

philiprehberger_docstring_cli-0.1.1-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file philiprehberger_docstring_cli-0.1.1.tar.gz.

File metadata

File hashes

Hashes for philiprehberger_docstring_cli-0.1.1.tar.gz
Algorithm Hash digest
SHA256 330a3fceed208b2072ac7b00855075c5a80bf0a4ef0815dab866645b57614248
MD5 717f61193d8809d4c961f89a14e49192
BLAKE2b-256 e0e1c67b262a48b8c50b8835c460ba0cf379365fead327a479d5c8756444719c

See more details on using hashes here.

File details

Details for the file philiprehberger_docstring_cli-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for philiprehberger_docstring_cli-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a0fde80400dfba7744010ef5f3e9d035db5fbf2312d98980d4d54355d75197a7
MD5 fdbea403542d443a70aeab2453170c8b
BLAKE2b-256 a5f84b4ae06bf5adab4f91fa3561a40119f48cd64c19e3f2faa11e49897df8a9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page