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 pydantic2-argparse

Example

import pydantic
import pydantic2_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 = pydantic2_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

pydantic2_argparse-0.9.2.tar.gz (16.6 kB view details)

Uploaded Source

Built Distribution

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

pydantic2_argparse-0.9.2-py3-none-any.whl (25.2 kB view details)

Uploaded Python 3

File details

Details for the file pydantic2_argparse-0.9.2.tar.gz.

File metadata

  • Download URL: pydantic2_argparse-0.9.2.tar.gz
  • Upload date:
  • Size: 16.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.6 Darwin/23.1.0

File hashes

Hashes for pydantic2_argparse-0.9.2.tar.gz
Algorithm Hash digest
SHA256 76c8ad548986e39fed85afda94d54364c3175d3595065530430e6e94af33275d
MD5 4369e036837d0e20f59d1ca278c99ad8
BLAKE2b-256 ad67b546cb734a8dacb0a5dd280a43f5d66a6c25f160ee3b30eff3e9c4b92c6c

See more details on using hashes here.

File details

Details for the file pydantic2_argparse-0.9.2-py3-none-any.whl.

File metadata

  • Download URL: pydantic2_argparse-0.9.2-py3-none-any.whl
  • Upload date:
  • Size: 25.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.6 Darwin/23.1.0

File hashes

Hashes for pydantic2_argparse-0.9.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e375ce3fbbf7fe4c45c93bfbf2c230570a7cb4136f4f72cca6e43a5e44af628f
MD5 f96044bfa08673d7f986196cdaaa8130
BLAKE2b-256 08f28161869c6a990c6e19ff0306823ad028277a852eb51e27cf3495eff07880

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