Skip to main content

Typed Argument Parsing with Pydantic

Project description

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")
    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(
        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

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

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

Details for the file pydantic_argparse-0.9.0.tar.gz.

File metadata

  • Download URL: pydantic_argparse-0.9.0.tar.gz
  • Upload date:
  • Size: 15.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.27.0

File hashes

Hashes for pydantic_argparse-0.9.0.tar.gz
Algorithm Hash digest
SHA256 01ed0996102301b1269124fd4dd2677d3909e6d930ab86085552c465c8f3aa79
MD5 ac743702a13ba63b2dae1d344ca990de
BLAKE2b-256 9d68f67377429bb0fb8718dc841beafd3eade66824fc90de87c31a4d5caa2f68

See more details on using hashes here.

File details

Details for the file pydantic_argparse-0.9.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pydantic_argparse-0.9.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d577b3b5e9628e18fea9d37bfbf12dde380fbc6277ac776a30e28106f2c306aa
MD5 b515a3b53948ee4ffaca20cddad82daa
BLAKE2b-256 31fbea4f0f4c67e02396035f0706dcb48b2f0168ab6a2e40e98449c2e83a3bbd

See more details on using hashes here.

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