Skip to content

Instantly share code, notes, and snippets.

@Red-Eyed
Last active November 12, 2025 05:38
Show Gist options
  • Select an option

  • Save Red-Eyed/1d9f7d540c79b589eda4c809efb64c7d to your computer and use it in GitHub Desktop.

Select an option

Save Red-Eyed/1d9f7d540c79b589eda4c809efb64c7d to your computer and use it in GitHub Desktop.
pydantic-settings CLI
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()
@Red-Eyed
Copy link
Author

Red-Eyed commented Nov 12, 2025

Example of how to use pydantic as replacement of argparse
keywords: pydantic, python, argparse, settings, configuration, environment variables, cli, pydantic-cli, pydantic-argparse

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment