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

Example

import 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.8.0.tar.gz (16.5 kB view details)

Uploaded Source

Built Distribution

pydantic_argparse-0.8.0-py3-none-any.whl (25.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pydantic_argparse-0.8.0.tar.gz
  • Upload date:
  • Size: 16.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.0 CPython/3.11.2 Darwin/22.3.0

File hashes

Hashes for pydantic_argparse-0.8.0.tar.gz
Algorithm Hash digest
SHA256 97ec4febc7fc0575b77c5fd4f2c7d3b95c2dbc6c6b606adfb83b2b9a7e88d933
MD5 f46afc885daf4ab8cdeb9a72299af3f3
BLAKE2b-256 30a5ae32e92cc5293322d89e6d40dc59e69e2eaa0ea8fff4281be86a26b791d0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pydantic_argparse-0.8.0-py3-none-any.whl
  • Upload date:
  • Size: 25.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.0 CPython/3.11.2 Darwin/22.3.0

File hashes

Hashes for pydantic_argparse-0.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 246e30000ad50495a95b7c0622eb35846655a12fa7500bc5502adba2a1527e62
MD5 928095c357e0851fe0d134da7fc4870d
BLAKE2b-256 8f2c73ee9b4f4c0a66e8b5c0ecfd28b789178133fdee55a9315345ac3bc922e8

See more details on using hashes here.

Supported by

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