argdantic: Typed CLI Interfaces with Pydantic

1.3.3 · active · verified Fri Apr 17

Argdantic is a Python library that allows you to define command-line interfaces (CLIs) using Pydantic models for argument definition and type validation, combined with `argparse` for parsing. It aims to provide a modern, type-hinted approach to CLI development, leveraging Pydantic's strong data validation and `argparse`'s robust parsing capabilities. The current version is 1.3.3 and it actively maintained.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define a simple command-line application using `argdantic`. It showcases creating an `Argdantic` instance, defining a command with type-hinted arguments and Pydantic's `Field` for help text, and running the application.

from argdantic import Arqdantic
from pydantic import Field
import sys

# Instantiate the Arqdantic application
app = Arqdantic()

# Define a command using a decorator
@app.command()
def greet(
    name: str = Field('World', help='The name to greet.'),
    count: int = Field(1, help='Number of times to greet.')
):
    """Greets the given name multiple times."""
    for _ in range(count):
        print(f"Hello, {name}!")

if __name__ == "__main__":
    # Example of how to run from the CLI:
    # python your_script.py greet --name Alice --count 3
    # For testing within the script, you can temporarily modify sys.argv:
    # sys.argv = ['your_script.py', 'greet', '--name', 'Alice', '--count', '3']
    app() # This parses sys.argv and executes the command

view raw JSON →