Skip to main content

argdantic

Typed command line interfaces with argparse and pydantic.

test passing coverage pypi version python versions

license documentation

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.
  • Multiple sources: arguments can be provided from multiple sources, including environment variables, JSON, TOML and YAML files.

Quickstart

Installation

Installing argdantic can be done from source, or simply using pip. The only required dependency is, of course, pydantic, while the remaining can be selected depending on your needs:

recommended choice: install everything
this includes orjson, pyyaml, tomli, python-dotenv
user@pc:~$ pip install argdantic[all]

env, json, toml or yaml dependencies
user@pc:~$ pip install argdantic[env|json|toml|yaml]

minimum requirement, only pydantic included
user@pc:~$ pip install argdantic

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.

Using Models

Plain arguments and pydantic models can be mixed together:

from argdantic import ArgParser
from pydantic import BaseModel

parser = ArgParser()


class Item(BaseModel):
    name: str
    price: float


@parser.command()
def buy(item: Item, quantity: int):
    print(f"Bought {quantity} X {item.name} at ${item.price:.2f}.")

if __name__ == "__main__":
    parser()

This will produce the following help:

usage: cli.py [-h] --item.name TEXT --item.price FLOAT --quantity INT

optional arguments:
  -h, --help          show this help message and exit
  --item.name TEXT
  --item.price FLOAT
  --quantity INT

Arguments From Different Sources

argdantic supports several inputs:

  • .env files, environment variables, and secrets thanks to pydantic.
  • JSON files, using either the standard json library, or orjson if available.
  • YAML files, using the pyyaml library.
  • TOML files, using the lightweight tomli library.

Sources can be imported and added to each command independently, as such:

from argdantic import ArgParser
from argdantic.sources import EnvSettingsSource, JsonSettingsSource

parser = ArgParser()


@parser.command(
    sources=[
        EnvSettingsSource(env_file=".env", env_file_encoding="utf-8"),
        JsonSettingsSource(path="settings.json"),
    ]
)
def sell(item: str, quantity: int, value: float):
    print(f"Selling: {item} x {quantity}, {value:.2f}$")


if __name__ == "__main__":
    parser()

This is just a brief introduction to the library, more examples and details can be found in the documentation.

Contributing

Contributions are welcome! You can open a new issue to report bugs, or suggest new features. If you're brave enough, pull requests are also welcome.

Release files for argdantic 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for argdantic 1.1.0
File Size Uploaded
argdantic-1.1.0.tar.gz 44.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for argdantic 1.1.0
File Interpreter ABI Platform
argdantic-1.1.0-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 69.5 kB

Release files / argdantic-1.1.0.tar.gz

Download URL argdantic-1.1.0.tar.gz
Size 44.9 kB
Tags Source
SHA-256 checksum
How to use checksums
6778204f926cb7d23aa1eaedf34d374a4d137977c8699843d7e1d2684df571ed
BLAKE2b-256 checksum
How to use checksums
f5eb399b8660b2ec1fa4fd6286bd55ae94b85f0eae905afc4f19d1f3a878be8b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.31.0

Release files / argdantic-1.1.0-py2.py3-none-any.whl

Download URL argdantic-1.1.0-py2.py3-none-any.whl
Size 24.6 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
0ddabe399bf9577fcd73c02aa484c0b66be95b1d1d53ed536c7738b5fe5c3726
BLAKE2b-256 checksum
How to use checksums
ae039ef2d2c9b0163e8108c124584abd7ba17565d847e6278608f2b1b9a5cf22
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.31.0

Release history Release notifications | RSS feed

1.3.3

2 release files

1.3.2

2 release files

1.3.1

2 release files

1.3.0

2 release files

1.2.0

2 release files

This release

1.1.0 This release

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.3

2 release files

0.2.0

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page