Typed Argument Parsing with Pydantic

0.10.0 · active · verified Wed Apr 15

pydantic-argparse is a Python package that provides declarative typed argument parsing by leveraging Pydantic models. It builds on the standard `argparse` module, offering a simple, opinionated, and type-hinted API for command-line interfaces. The library supports nesting Pydantic models for sub-command functionality and utilizes Pydantic's robust validation system. The current version is 0.10.0, released in February 2025, indicating an active development and release cadence.

Warnings

Install

Imports

Quickstart

Define your command-line arguments using a Pydantic `BaseModel`. Then, create an instance of `pydantic_argparse.ArgumentParser` with your model and call `parse_typed_args()` to get a validated Pydantic model instance. This example uses `pydantic.v1` as shown in the official documentation.

import pydantic.v1 as pydantic
from pydantic import Field
from pydantic_argparse import ArgumentParser

class Arguments(pydantic.BaseModel):
    """Simple Command-Line Arguments."""
    # 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:
    """Simple Main Function."""
    parser = ArgumentParser(
        model=Arguments,
        prog="Example Program",
        description="Example Description",
        version="0.0.1",
        epilog="Example Epilog",
    )
    args = parser.parse_typed_args()
    print(args)

if __name__ == "__main__":
    main()

view raw JSON →