Simple Example¶
In this first example, we use a single model and default values:
from typing import Annotated
import click
from pydantic import BaseModel, Field
from pydanclick import from_pydantic
class TrainingConfig(BaseModel):
    """Simple training config.
    Attributes:
        epochs: number of epochs
        lr: learning rate
        early_stopping: whether to stop training when validation loss stops decreasing
    """
    epochs: int
    lr: Annotated[float, Field(gt=0)] = 1e-4
    early_stopping: bool = False
@click.command()
@from_pydantic(TrainingConfig)
def cli(training_config: TrainingConfig):
    """A simple example with a few parameters and default behavior."""
    # Here, we receive an already validated Pydantic object.
    click.echo(training_config.model_dump_json(indent=2))
if __name__ == "__main__":
    cli()
python examples/simple.py --help
Usage: simple.py [OPTIONS]
  A simple example with a few parameters and default behavior.
Options:
  --epochs INTEGER                number of epochs  [required]
  --lr FLOAT RANGE                learning rate  [x>0]
  --early-stopping / --no-early-stopping
                                  whether to stop training when validation
                                  loss stops decreasing
  --help                          Show this message and exit.
You can notice that:
- fields without default values are marked as 
required - constraints are properly recognized by Click
 - boolean fields are converted to boolean flags, as recommended by Click