Last active
November 12, 2025 05:38
-
-
Save Red-Eyed/1d9f7d540c79b589eda4c809efb64c7d to your computer and use it in GitHub Desktop.
pydantic-settings CLI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from pydantic import Field, FilePath | |
| from pydantic_settings import BaseSettings, CliApp, SettingsConfigDict, SettingsError | |
| class CLISettings( | |
| BaseSettings, | |
| ): | |
| # model config | |
| model_config = SettingsConfigDict( | |
| cli_parse_args=True, | |
| cli_enforce_required=True, | |
| cli_exit_on_error=False, | |
| ) | |
| # parameters | |
| input_file: FilePath | |
| output_file: FilePath | None = None | |
| tricky_param: str = Field(description="This is description of tricky parameter") | |
| def main(): | |
| try: | |
| args = CliApp.run(CLISettings) | |
| except SettingsError as e: | |
| print(e) | |
| else: | |
| print(args) | |
| if __name__ == "__main__": | |
| main() |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example of how to use pydantic as replacement of argparse
keywords: pydantic, python, argparse, settings, configuration, environment variables, cli, pydantic-cli, pydantic-argparse