pydantic / pydantic-settings

Settings management using pydantic
https://docs.pydantic.dev/latest/usage/pydantic_settings/
MIT License
555 stars 55 forks source link

Feature Request: Add short args to CLI #337

Closed mpkocher closed 1 month ago

mpkocher commented 1 month ago

"Short" args (or aliases) are useful in commanding tools. For example --name could have an alias of -n.

Acceptance Criteria

Example:

from pydantic import Field, AliasChoices
from pydantic_settings import BaseSettings

class Settings(BaseSettings, cli_parse_args=True):
    name: str = Field(validation_alias=AliasChoices('n', 'name'))
    age: int

Yields:

usage: example.py [-h] [--n str] [--age int]

options:
  -h, --help           show this help message and exit
  --n str, --name str  (required)
  --age int            (required)

It should return:

usage: example.py [-h] [--name str] [--age int]

options:
  -h, --help           show this help message and exit
  -n str, --name str  (required)
  --age int            (required)
kschwab commented 1 month ago

Thanks @mpkocher, that's a great suggestion to use single string aliases implicitly as short flags... I'll take a peek when I get a chance, but that should be pretty straightforward to add.