Skip to main content

Typed Argument Parsing with Pydantic enhanced version

Project description

Argparse Dantic

Typed Argument Parsing with Pydantic Enhanced


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

Example

from argparse_dantic import ArgumentParser, BaseModel, Field


class Arguments(BaseModel):
    # Required Args
    string: str = Field(description="a required string", aliases=["-s"])
    integer: int = Field(description="a required integer", aliases=["-i"])
    flag: bool = Field(description="a required flag", aliases=["-f"])

    # Optional Args
    second_flag: bool = Field(False, description="an optional flag")
    third_flag: bool = Field(True, description="an optional flag")


def main() -> None:
    # Create Parser and Parse Args
    parser = 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] [-s STRING] [-i INTEGER] [-f | --flag | --no-flag]
                       [--second-flag] [--no-third-flag]

Example Description

required arguments:
  -s STRING, --string STRING
                        a required string
  -i INTEGER, --integer INTEGER
                        a required integer
  -f, --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 -i 42 -f
string='hello' integer=42 flag=True second_flag=False third_flag=True

Advanced Example

from argparse_dantic import ArgumentParser, BaseModel, Field, ActionNameBind

class GlobalModel(BaseModel):
    action_name: ActionNameBind # This is a special field that binds the action name to the model
    verbose: bool = Field(False, description="verbose output", global_=True)
    debug: bool = Field(False, description="debug output", global_=True)

class PubOptionsModel(BaseModel):
    target: str = Field(description="build target", aliases=["-t"])
    clean: bool = Field(False, description="clean build", aliases=["--c"])

class BuildCommandModel(GlobalModel, PubOptionsModel):
    build_type: str = Field(description="build type", aliases=["-bt"])

class InstallCommandModel(GlobalModel, PubOptionsModel):
    install_type: str = Field(description="install type", aliases=["-it"])

class BasicModel(GlobalModel):
    build: BuildCommandModel = Field(aliases=["bd"], description="build command")
    install: InstallCommandModel = Field(aliases=["ins"], description="install command")

def main() -> None:
    # Create Parser and Parse Args
    parser = ArgumentParser(
        model=BasicModel,
        prog="Example Program",
        description="Example Description",
        version="0.0.1",
        epilog="Example Epilog",
    )
    args = parser.parse_typed_args()

    # Print Args
    print(args)

    # Get Command Arguments Faster
    command_arguments = getattr(args, args.action_name)

    # Get Global Arguments Faster
    verbose = args.global_data.get("verbose")
    debug = args.global_data.get("debug")
    # In child models you can also access global data
    # verbose = command_arguments.global_data.get("verbose")
    # debug = command_arguments.global_data.get("debug")

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

argparse_dantic-0.0.0.tar.gz (53.9 kB view details)

Uploaded Source

Built Distribution

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

argparse_dantic-0.0.0-py3-none-any.whl (63.8 kB view details)

Uploaded Python 3

File details

Details for the file argparse_dantic-0.0.0.tar.gz.

File metadata

  • Download URL: argparse_dantic-0.0.0.tar.gz
  • Upload date:
  • Size: 53.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.8

File hashes

Hashes for argparse_dantic-0.0.0.tar.gz
Algorithm Hash digest
SHA256 c486d5c6f2d46f8fa25b8004784e46cdd5e739520bea250c141aecda874dfa73
MD5 5af58e6011534dd03d5050997480a446
BLAKE2b-256 267ed6f5333c5b813a96d011a87deef4f47ce73d3f739b2385d939f1db527252

See more details on using hashes here.

File details

Details for the file argparse_dantic-0.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for argparse_dantic-0.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3edaeb2008ebada58aae5a024bf9cde0cda6fd920b71ba66c8d714ac14b7576a
MD5 046e5e89765d0362a0f0a5824b2def98
BLAKE2b-256 dc4074b8ae14b522ec43719900472f9f552a7b784eb75d3eb01610b9af892ee8

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