Type-safe facade for the venerable argparse
Project description
paramspecli
paramspecli is a facade for the venerable argparse. It's simple, composable, and fun. But, most important, it's type-safe thanks to the ParamSpec magic.
Quick start
from paramspecli import Command, argument, option, flag
def ping(addr: str, *, until_stopped: bool, count: int | None):
print(f"Pinging {addr} ...")
cli = Command(ping, prog="ping")
cli.bind(
-argument("IP"),
until_stopped=-flag("-t"),
count=-option("-n", type=int),
)
result = cli.parse()
result()
Running it:
$ python ping.py -t -n 10 127.0.0.1
Pinging 127.0.0.1 ...
Type checker catches common errors:
cli.bind(
# "bool" is not assignable to "str"
-argument("IP", type=bool),
# Type "bool | None" is not assignable to type "bool"
until_stopped=-flag("-t", default=None),
#
# Argument missing for parameter "count"
)
IDE suggestions work too:
cli.bind(⏎
(addr: str, *, until_stopped: bool, count: int | None) -> None
Installation
pip install paramspecli
Key points
paramspecli builds hierarchical CLIs with groups and commands.
- Commands match handler functions parameters with arguments and options.
- Arguments are bound to the handler's positional parameters.
- Options are bound to the handler's keyword parameters.
- Groups organize the commands. Groups may be nested. They could act like an intermediate commands, i.e. have own handlers, options and arguments.
- CLI 'compiles' to the
argparse, runs it, then outputs the parse result. - Parse result is a callable. Calling it invokes handlers along the route.
paramspecli supports several advanced argparse features: help sections, mutually exclusive
options, options with the same destination. And adds a few own, like const options and passing contexts. It also includes a markdown documentation generator.
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 paramspecli-0.2.2.tar.gz.
File metadata
- Download URL: paramspecli-0.2.2.tar.gz
- Upload date:
- Size: 19.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.31.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fc4c5f1b476bc638192f84af3f493e555882ced2a8ae4eff8df7bc7568887a5b
|
|
| MD5 |
1c435e081e6c98f251f190061bdaa393
|
|
| BLAKE2b-256 |
302d5498d081b9e2171b31d669bbc6bbecd842421d8447c8f64b1b4f5c56b978
|
File details
Details for the file paramspecli-0.2.2-py3-none-any.whl.
File metadata
- Download URL: paramspecli-0.2.2-py3-none-any.whl
- Upload date:
- Size: 21.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.31.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3d087ab8755a77f8b1593c6112811398a0ada61ddbc28d12e5069c013724b78e
|
|
| MD5 |
96ab111b16a58dfb39a3a106481ef31f
|
|
| BLAKE2b-256 |
74937ade027e2f436360a04c7a94cbf7364e83ec721e9a01960149e86b829668
|