Skip to main content

Pydantic Argparse

Typed Argument Parsing with Pydantic


Help

See documentation for help.

Requirements

Requires Python 3.8+, and is compatible with the Pydantic v1 API.

Installation

Installation with pip is simple:

$ pip install pydantic-argparse

Example

import pydantic.v1 as pydantic

import pydantic_argparse


class Arguments(pydantic.BaseModel):
    # Required Args
    string: str = pydantic.Field(description="a required string", aliases=["-s"])
    integer: int = pydantic.Field(description="a required integer", aliases=["-i"])
    flag: bool = pydantic.Field(description="a required flag", aliases=["-f"])

    # Optional Args
    second_flag: bool = pydantic.Field(False, description="an optional flag")
    third_flag: bool = pydantic.Field(True, description="an optional flag")


def main() -> None:
    # Create Parser and Parse Args
    parser = pydantic_argparse.ArgumentParser(
        model=Arguments,
        prog="Example Program",
        description="Example Description",
        version="0.0.1",
        epilog="Example Epilog",
    )
    args = parser.parse_typed_args()

    # Print Args
    print(args)


if __name__ == "__main__":
    main()
$ python3 example.py --help
usage: Example Program [-h] [-v] [-s STRING] [-i INTEGER] [-f | --flag | --no-flag]
                       [--second-flag] [--no-third-flag]

Example Description

required arguments:
  -s STRING, --string STRING
                        a required string
  -i INTEGER, --integer INTEGER
                        a required integer
  -f, --flag, --no-flag
                        a required flag

optional arguments:
  --second-flag         an optional flag (default: False)
  --no-third-flag       an optional flag (default: True)

help:
  -h, --help            show this help message and exit
  -v, --version         show program's version number and exit

Example Epilog
$ python3 example.py --string hello -i 42 -f
string='hello' integer=42 flag=True second_flag=False third_flag=True

License

This project is licensed under the terms of the MIT license.

Metadata

Release files for pydantic-argparse 0.10.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 pydantic-argparse 0.10.0
File Size Uploaded
pydantic_argparse-0.10.0.tar.gz 15.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pydantic-argparse 0.10.0
File Interpreter ABI Platform
pydantic_argparse-0.10.0-py3-none-any.whl Python 3 none any Details

Total release size: 41.2 kB

Release files / pydantic_argparse-0.10.0.tar.gz

Download URL pydantic_argparse-0.10.0.tar.gz
Size 15.9 kB
Tags Source
SHA-256 checksum
How to use checksums
d57eb0a84c8f0af6605376157d3f445cfd786700f2e596ba9d48d15d557185eb
BLAKE2b-256 checksum
How to use checksums
94eae63d587294c20d3b83e9c312b5d577c9ec28962ee8490839ca9996672849
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-httpx/0.28.1

Release files / pydantic_argparse-0.10.0-py3-none-any.whl

Download URL pydantic_argparse-0.10.0-py3-none-any.whl
Size 25.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e317f001208d77a5600ece6f7ac78d768d8221a7d64a958980705e9630c2e299
BLAKE2b-256 checksum
How to use checksums
be149ee71e3a183f76ff93e46b36157d6ddbf29ec2547b7d2c57931cd5d3aecc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-httpx/0.28.1

Release history Release notifications | RSS feed

This release

0.10.0 This release

2 release files

0.9.0

2 release files

0.8.0

2 release files

0.7.0

2 release files

0.6.1

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

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