Automatically generate CLI interfaces from function signatures and docstrings
Project description
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
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 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)
- Builds a parser from the function and parses the given
argv(orsys.argv[1:]) - Does not require the
@clidecorator
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file philiprehberger_docstring_cli-0.1.2.tar.gz.
File metadata
- Download URL: philiprehberger_docstring_cli-0.1.2.tar.gz
- Upload date:
- Size: 5.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1361fa2020d6a663af9f8725ac1fefa9b96e8bae78cc34b18bcb09836784a48a
|
|
| MD5 |
8189316cd0993e9beb9fd1b0841ac14f
|
|
| BLAKE2b-256 |
0de7a6f495af34f63acd581ad368bb936b821467494277cde87eb6fcd8e1cca4
|
File details
Details for the file philiprehberger_docstring_cli-0.1.2-py3-none-any.whl.
File metadata
- Download URL: philiprehberger_docstring_cli-0.1.2-py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e889646f090f8ae95e6266d2b5460a76ce07ecd9159bcbdda85fb866eae2bdf9
|
|
| MD5 |
dd95c921c73a76ba2e50dea057df13d6
|
|
| BLAKE2b-256 |
6e8ef65a41062676ffe8c22bf4af5d6b6901eeb32aac19de563837a83f4e7dd4
|