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

Development

pip install -e .
python -m pytest tests/ -v

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.5.tar.gz (5.7 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.5-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for philiprehberger_docstring_cli-0.1.5.tar.gz
Algorithm Hash digest
SHA256 b2aa0cf509d0917ef15fdc2c96408ae1ce6c11875823454e493e6ee98033da0d
MD5 baa6352c7e76799b0173f560f26b9d02
BLAKE2b-256 f71e52dc20b581c52acb4726f05bd1b35335efe22ac15201eb745cc70843fabb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for philiprehberger_docstring_cli-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 cfbcca4fed8ff147e21b3e418250ecbe479e482093683eeacc57565ab1ca767f
MD5 4af286e8c4a100c2168735538582058e
BLAKE2b-256 1e363b5fc67e1f3b2336cd07082305b30b013b667510ce5596a893595a1054f0

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