Skip to main content

Typed Argument Parsing with Pydantic

Project description

Pydantic Argparse

Typed Argument Parsing with Pydantic


See documentation for help.


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


Installation with pip is simple:

$ pip install pydantic-argparse


import pydantic.v1 as pydantic
import pydantic_argparse

class Arguments(pydantic.BaseModel):
    # Required Args
    string: str = pydantic.Field(description="a required string")
    integer: int = pydantic.Field(description="a required integer")
    flag: bool = pydantic.Field(description="a required flag")

    # 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(
        prog="Example Program",
        description="Example Description",
        epilog="Example Epilog",
    args = parser.parse_typed_args()

    # Print Args

if __name__ == "__main__":
$ python3 --help
usage: Example Program [-h] [-v] --string STRING --integer INTEGER --flag |
                       --no-flag [--second-flag] [--no-third-flag]

Example Description

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

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

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

Example Epilog
$ python3 --string hello --integer 42 --flag
string='hello' integer=42 flag=True second_flag=False third_flag=True


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

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

pydantic_argparse-0.9.0.tar.gz (15.7 kB view hashes)

Uploaded Source

Built Distribution

pydantic_argparse-0.9.0-py3-none-any.whl (25.9 kB view hashes)

Uploaded 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