Typed command line interfaces with argparse and pydantic
Project description
argdantic
Typed command line interfaces with argparse
and pydantic
.
Features
argdantic
provides a thin boilerplate layer to provide a modern CLI experience, including:
- Typed arguments: arguments require full typing by default, enforcing clarity and help your editor provide better support (linting, hinting).
- Nested models: exploit
pydantic
models to scale from simple primitives to complex nested configurations with little effort. - Nested commands: combine commands and build complex hierarchies to build complex interfaces.
- Validation by default: thanks to
pydantic
, field validation is provided by default, with the desired complexity.
A Simple Example
Creating a CLI with argdantic
can be as simple as:
from argdantic import ArgParser
# 1. create a CLI instance
parser = ArgParser()
# 2. decorate the function to be called
@parser.command()
def buy(name: str, quantity: int, price: float):
print(f"Bought {quantity} {name} at ${price:.2f}.")
# 3. Use your CLI by simply calling it
if __name__ == "__main__":
parser()
Then, in a terminal, the help
command can provide the usual information:
$ python cli.py --help
> usage: buy [-h] --name TEXT --quantity INT --price FLOAT
>
> optional arguments:
> -h, --help show this help message and exit
> --name TEXT
> --quantity INT
> --price FLOAT
This gives us the required arguments for the execution:
$ python cli.py --name apples --quantity 10 --price 3.4
> Bought 10 apples at $3.40.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
argdantic-0.1.0.tar.gz
(21.5 kB
view hashes)
Built Distribution
Close
Hashes for argdantic-0.1.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 74f3c1fe92f6a5888dc0fe0f3d74f75a8937f3f51fc5487db3a658de00da0c03 |
|
MD5 | 287b0852845817a77d829218e610f0a3 |
|
BLAKE2b-256 | adbf71b2576a6917865657a231faab71ad70cf2694c840d2cff77c8582312016 |