pydantic-cli icon indicating copy to clipboard operation
pydantic-cli copied to clipboard

Fix _add_pydantic_field_to_parser using cli annotation as parser args

Open calgray opened this issue 2 months ago • 1 comments

This MR performs a significant change to resolve the issue of not being able to declare CLI positional arguments by passing annotated cli arguments directly to argparse.

In doing so, this means that no CLI annotation results in argparse doing the default behaviour of creating cli positional arguments instead of keyworded. Required --name=value arguments are however still supported, they just require an explicit Field(cli="--name") annotation.

Providing only a default value (no annotation) still creates a CLI keyworded argument (same behaviour as typer) and is controlled by the line:

args = () if is_required else (f"{long_prefix}{field_id}",)

May want to consider performing the exact same behaviour to typer in this case and auto-generate a kebab-case keyword argument.

calgray avatar Dec 10 '25 08:12 calgray

Early versions of pydantic-cli supported positional arguments.

Positional argument support was explicitly removed because there was too many friction points between the positional arguments and the json/preset feature.

The motivation for this decision is documented in the Readme.

https://github.com/mpkocher/pydantic-cli?tab=readme-ov-file#why-are-positional-arguments-not-supported

mpkocher avatar Dec 10 '25 17:12 mpkocher