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.2.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.2-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for philiprehberger_docstring_cli-0.1.2.tar.gz
Algorithm Hash digest
SHA256 1361fa2020d6a663af9f8725ac1fefa9b96e8bae78cc34b18bcb09836784a48a
MD5 8189316cd0993e9beb9fd1b0841ac14f
BLAKE2b-256 0de7a6f495af34f63acd581ad368bb936b821467494277cde87eb6fcd8e1cca4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for philiprehberger_docstring_cli-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e889646f090f8ae95e6266d2b5460a76ce07ecd9159bcbdda85fb866eae2bdf9
MD5 dd95c921c73a76ba2e50dea057df13d6
BLAKE2b-256 6e8ef65a41062676ffe8c22bf4af5d6b6901eeb32aac19de563837a83f4e7dd4

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