philiprehberger-docstring-cli
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
Adding --version
Pass version="..." to run() to enable a built-in --version flag:
from philiprehberger_docstring_cli import run
def add(a: int, b: int):
"""Add two numbers."""
return a + b
run(add, version="1.0.0")
# Invoking with --version prints "1.0.0" and exits.
Introspecting commands
Use command_info(fn) to inspect a @cli-decorated (or plain) function
without invoking it. Useful for building help screens, completion data,
or programmatic command listings.
from philiprehberger_docstring_cli import cli, command_info
@cli
def greet(name: str, count: int = 1):
"""Greet someone by name.
Args:
name: The person to greet.
count: Number of times to greet.
"""
...
info = command_info(greet)
# {
# "name": "greet",
# "description": "Greet someone by name.",
# "params": [
# {"name": "name", "type": "str", "default": None, "required": True, "help": "The person to greet."},
# {"name": "count", "type": "int", "default": 1, "required": False, "help": "Number of times to greet."},
# ],
# }
API
| Name | Description |
|---|---|
cli |
Decorator that makes a function callable from the CLI. |
run |
Run any function as a CLI command without decorating it. |
command_info |
Introspect a function and return its CLI metadata. |
@cli
- Reads type hints to set argument types (
int,float,str, etc.) - Parameters without defaults become positional arguments
- Parameters with defaults become optional
--flags boolparameters become--flag/--no-flagtoggles- 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, *, version=None)
- Builds a parser from the function and parses the given
argv(orsys.argv[1:]) - Does not require the
@clidecorator version: when set, the parser accepts--versionand prints this string
command_info(func)
- Returns a dict with
name,description, andparams - Each entry in
paramsis a dict withname,type,default,required, andhelp - Works on
@cli-decorated functions as well as plain functions
Development
pip install -e .
python -m pytest tests/ -v
Support
If you find this project useful:
License
Metadata
Release files for philiprehberger-docstring-cli 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| philiprehberger_docstring_cli-0.2.0.tar.gz | 189.9 kB | Without provenance | Details for philiprehberger_docstring_cli-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| philiprehberger_docstring_cli-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for philiprehberger_docstring_cli-0.2.0-py3-none-any.whl |
Total release size: 196.2 kB
Release files / philiprehberger_docstring_cli-0.2.0.tar.gz
| Download URL | philiprehberger_docstring_cli-0.2.0.tar.gz |
|---|---|
| Size | 189.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7f6bcb74a1ba21c87f9a6394c3892a6e28e09e33ab4682c07034b99ebf6f3d5d
|
|
BLAKE2b-256 checksum How to use checksums |
2101fd7adae7cf81fe3cc673626df522094e383bfd6c2d1cfbfb37985fd99d64
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.13
|
Release files / philiprehberger_docstring_cli-0.2.0-py3-none-any.whl
| Download URL | philiprehberger_docstring_cli-0.2.0-py3-none-any.whl |
|---|---|
| Size | 6.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3340c40ac1b41bf47eee9bb545bbfa6b2ba7fea3a58fe7e660bded9c1b4070c4
|
|
BLAKE2b-256 checksum How to use checksums |
aa296912f4b6af0bbaee50eb23bcd9515381258557b2c309cf0664e6711a87dc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.13
|
Security for philiprehberger-docstring-cli 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.