Skip to main content

Automatically generate CLI interfaces from function signatures and docstrings

Project description

philiprehberger-docstring-cli

Tests PyPI version Last updated

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

Support

If you find this project useful:

Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

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.6.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

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

File details

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

File metadata

File hashes

Hashes for philiprehberger_docstring_cli-0.1.6.tar.gz
Algorithm Hash digest
SHA256 f31c7ee1f7fb96d168eed17708b160704ce92f930bd54cb4b1e14dee122d2db5
MD5 e64c178d730dcf53d541c16a87ab5281
BLAKE2b-256 230bf65a0bfd9e35beab83f7cdd049c0f002b8a7c31074cf84490e71da6cc835

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for philiprehberger_docstring_cli-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 24479b85b8a29f617cb006441cf2a82b3f1c94b1cf718e21b8c82e6a3685855c
MD5 f2e4ac13dd7c7be2ea05822e547766c4
BLAKE2b-256 3ad74e55e7b7f5c381edb7166b55fe6e5c09d0745cc308160b4eb842b06eeeb3

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