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.0.1.tar.gz
(14.4 kB
view hashes)
Built Distribution
Close
Hashes for argdantic-0.0.1-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 255edc34fda3d4671489d03af574727fafd9500acc832c20096e834222e2998b |
|
MD5 | af22a42e815e3dc4bd5cc1985ae6baab |
|
BLAKE2b-256 | db9cfd871a9ca2b3d3a30cf7e3b0d59fd0377a2bac569d71b3b7b77d101d171f |