Skip to main content

Typed Argument Parsing with Pydantic

Project description

Pydantic Argparse

Typed Argument Parsing with Pydantic


Help

See documentation for help.

Installation

Installation with pip is simple:

$ pip install pydanticV2-argparse

Example

import pydantic
import pydanticV2_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 = pydanticV2_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] --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)

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 --integer 42 --flag
string='hello' integer=42 flag=True second_flag=False third_flag=True

License

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

pydanticv2_argparse-1.0.5.tar.gz (13.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pydanticv2_argparse-1.0.5-py3-none-any.whl (14.6 kB view details)

Uploaded Python 3

File details

Details for the file pydanticv2_argparse-1.0.5.tar.gz.

File metadata

  • Download URL: pydanticv2_argparse-1.0.5.tar.gz
  • Upload date:
  • Size: 13.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.19

File hashes

Hashes for pydanticv2_argparse-1.0.5.tar.gz
Algorithm Hash digest
SHA256 ecd6c4de5d57289a37653230255eaff32874f82f87ee6a8264206387fd2bc85f
MD5 694c867cfb7e3c6ac4b53b15eda54344
BLAKE2b-256 fc4236306294afa60245d922b12dd38b5bc98f9c38e868270ddeee8ea0982133

See more details on using hashes here.

File details

Details for the file pydanticv2_argparse-1.0.5-py3-none-any.whl.

File metadata

File hashes

Hashes for pydanticv2_argparse-1.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 b65d687bc39a5eaac5a619e46c36712fe7578034d405c4887317bc9256de91ba
MD5 09561c69878137f3ec877fb6b535657b
BLAKE2b-256 27a5c432de9d88e0e98a4b58c65b716878098b0de291ba693e3ce30f7c62cb20

See more details on using hashes here.

Supported by

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