Skip to main content

Typed command line interfaces with argparse and pydantic

Project description

argdantic

Typed command line interfaces with argparse and pydantic.

test passing coverage pypi version python versions

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


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)

Uploaded Source

Built Distribution

argdantic-0.0.1-py2.py3-none-any.whl (10.0 kB view hashes)

Uploaded Python 2 Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page