ã¯ããã«
å¹ãä¸ãã ç¬éã好ã⪠nikkieã§ãã
pydantic-settingsã§CLIã®ç´ æ¯ãã®ç¶ããä»åã¯ãµãã³ãã³ãã¾ããã触ãã¾ãã
ç®æ¬¡
pydantic-settingsã§CLIãä½ããï¼
ãã¼ã¹ã©ã¤ãã©ãªPydanticãè¨å®ã«ãé©ç¨ããpydantic-settingsã
ãªãã¨è¨å®ã ãã§ãªããCLIã¢ããªã±ã¼ã·ã§ã³ãä½ãã¡ããã¾ãï¼
pydantic-settingsã§ä½ãCLIã®ããã¯ããªãã·ã§ã³å¼æ°ãç°å¢å¤æ°ãããæå®ã§ãããã¨ã
æ¨æºã©ã¤ãã©ãªã®argparseã§ãããå®ç¾ããã®ã¯ãããã骨ãæããããªã®ã§ãããpydantic-settingsã§ã¯ããã©ã«ãã§ãµãã¼ãããã¦ãããã§ãï¼
argparseããããã®æ°ã®ç§ã§ãããããã¯ä¹ãæãã決æããã®ã«ååãããçç±ã§ããï¼ãªã®ã§ç´ æ¯ãããã¦ãã¾ãï¼
ååã®ç´ æ¯ãããããªãã·ã§ã³å¼æ°ã»ä½ç½®å¼æ°ã®æ±ããåãã£ã¦ãã¾ãã
pydantic_settings.BaseSettings
ãç¶æ¿ããã¯ã©ã¹ã«ã¦
- ãªãã·ã§ã³å¼æ°ï¼
string: str = Field(validation_alias=AliasChoices("s", "string"))
-s
ã--string
ã§æå®ã§ãã- ç°å¢å¤æ°
S
ãSTRING
ã§ãæå®ã§ããï¼ç´ æ´ãããããï¼ï¼ðï¼ - ããã©ã«ãå¤ãæããããã¨ãå¯è½ï¼
Field("ããã©ã«ãå¤", validation_alias=...)
ï¼
- ä½ç½®å¼æ°ï¼
string: CliPositionalArg[str]
- ä½ç½®å¼æ°ã¯ç°å¢å¤æ°ããã¯æå®ã§ãã¾ããï¼ä½ç½®ã§æå®ãã¾ãããï¼
- ä½ç½®å¼æ°ã«ããã©ã«ãå¤ã¯æãããããªã模æ§1
ãµãã³ãã³ã
Rustã®clapã§ç´ æ¯ãããã®ã¨åæ§ãªCLIãä»åä½ãã¾ãã2ã
% uv run --quiet cli.py position ã¨ããªã¼ 5 ã¨ããªã¼ã¨ããªã¼ã¨ããªã¼ã¨ããªã¼ã¨ããªã¼ % OPTION_S=ã¨ããªã¼ uv run --quiet cli.py option --num 5 ã¨ããªã¼ã¨ããªã¼ã¨ããªã¼ã¨ããªã¼ã¨ããªã¼
åä½ç°å¢
- uv 0.4.27 (Homebrew 2024-10-25)
- Python 3.13.0
- pydantic-settings 2.7.0
- Pydantic 2.10.4
ããã¥ã¡ã³ããSubcommands and Positional Argumentsããã
https://docs.pydantic.dev/latest/concepts/pydantic_settings/#subcommands-and-positional-arguments
- ãµãã³ãã³ãã«ä½¿ãã®ã¯
CliSubCommand
ï¼ã«ããã¢ããã¼ã·ã§ã³ï¼
class Settings(BaseSettings): position: CliSubCommand[Position] option: CliSubCommand[Option]
- ãµãã³ãã³ãã®å®ä½ã¯
- Pydanticã®
BaseModel
ãç¶æ¿ããã¯ã©ã¹ - ã¾ãã¯ã
pydantic.dataclasses.dataclass
ã§ãã³ã¬ã¼ãããã¯ã©ã¹ - ï¼IMOï¼ãµãã³ãã³ãã«ã¯
BaseSettings
ã¯ç¶æ¿ããªããã§ããï¼ï¼
- Pydanticã®
- BaseSettingsãç¶æ¿ããã¯ã©ã¹ï¼ã³ãã³ãã®å¤§å
ï¼ã§ã¯ã
CliApp.get_subcommand()
ã使ã- ããã©ã«ãã§ã¯ããµãã³ãã³ãã®ã¤ã³ã¹ã¿ã³ã¹ã®
cli_cmd()
ã¡ã½ãããå¼ã³åºã - https://docs.pydantic.dev/latest/api/pydantic_settings/#pydantic_settings.CliApp.run_subcommand
- ããã©ã«ãã§ã¯ããµãã³ãã³ãã®ã¤ã³ã¹ã¿ã³ã¹ã®
- è¨å®ã®ã¨ã3ã¨åæ§ã«ãç°å¢å¤æ°ã¯ã
BaseSettings
ç¶æ¿ã¯ã©ã¹ã®å±æ§åã+ãåºåãæåã+ããµãã³ãã³ãã®ã¨ã¤ãªã¢ã¹ãã§ãã- åºåãæåã¯
env_nested_delimiter
ã§ã¢ã³ãã¼ã¹ã³ã¢ãæå® - åæããã¨ã
OPTION_S
ã»OPTION_STRING
ã»OPTION_N
ã»OPTION_NUM
- åºåãæåã¯
- ï¼ååè¨äºããï¼
CliApp.run(Settings)
ã§Settings
ã¤ã³ã¹ã¿ã³ã¹ã®cli_cmd()
ã¡ã½ãããå¼ã³åºãã¦ã¾ã
ãµãã³ãã³ããã§ãããï¼ð
ãªããNoteã«å¶ç´ã®è¨è¿°ã
it does not allow for multiple subparsers with each subparser having its own set of subcommands.
ç´é¢ããããã§ã¯ãªãã®ã§ãã³ã¨ãã¦ã¯ããªãã®ã§ãããargparseã¿ããã«ãµãã³ãã³ãã»ãµããµãã³ãã³ãã¨ãã¹ããããããªãã£ã¦ãã¨ã§ãããï¼
BaseSettings
ãç¶æ¿ããã¯ã©ã¹ãCliSubCommand
ã¢ããã¼ã·ã§ã³ã§ããªãããã§ããã
argparseã§ã¯ä½¿ããã¨ããã£ãã®ã§ã¡ãã£ã¨çããããããªãã§ããããµããµãã³ãã³ããå¿
è¦ã«ãªã£ãã¨ãã«èããããã¨æãã¾ãï¼ãµãã³ãã³ãã ãã«åããã¦ãã³ãã³ãã®è¨è¨ãè¦ç´ããã¨ã«ãªãããï¼
çµããã«
pydantic-settingsã§ä½ãCLIã§ããµãã³ãã³ãã®ä½ãæ¹ãå®å
¨ã«ç解ãã¾ããï¼
clapã®ç´ æ¯ãã§ãæ¸ãã¾ããããä½ç½®å¼æ°ã»ãªãã·ã§ã³å¼æ°ã»ãµãã³ãã³ããæ¼ãããã®ã§ãargparseã§æ¸ãã¦ããCLIã®8å²ãããã¯ãpydantic-settingsã§ãæ¸ãããã§ãï¼â»é¦¬é¹¿ã®å±±æï¼ã
Pydanticã§ãããæ¸ãã¦ã¦åãããããå½ããã¾ãããç°å¢å¤æ°ãããªãã·ã§ã³ãæå®ã§ããã®ã¯åºã使ãããCLIã¯åãã¦ããå°è±¡ã®ãã便å©æ©è½ã§ãpydantic-settingsã¯æ¨ãããã©ã¤ãã©ãªã«ãªãã¾ããã
åã天æï¼
- argparseï¼ãRustã®clapï¼ã§ã¯ã§ãã¦ãã¾ãããä½ç½®å¼æ°ã®ããã©ã«ãå¤ãããã»ã©ä½¿ã£ã¦ããªãç§ã¨ãã¦ã¯ããã¾ãçãã¯ãªãã§ã↩
-
uvãinline script metadataãèªãã§ããã¨ããåºåï¼ãReading inline script metadataãï¼ãããªããã
--quiet
ãè¦ã¤ãã¾ãããhttps://docs.astral.sh/uv/reference/cli/#uv-run↩ - è¨å®ã®ç´ æ¯ãã®æ§åï¼pydantic-settings素振りの記:ネストした設定 〜BaseModel継承クラスが全ての属性でデフォルト値を持つならば、インスタンス化してBaseSettings側のデフォルト値とする〜 - nikkie-ftnextの日記↩