Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion pydantic_settings/sources/providers/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -1301,7 +1301,12 @@ def _help_format(
default = f'(default: {field_info.default if enum_name is None else enum_name})'
elif field_info.default_factory is not None:
default = f'(default factory: {self._metavar_format(field_info.default_factory)})'
_help += f' {default}' if _help else default

if _CliToggleFlag in field_info.metadata:
if not _help:
_help = '(toggle)'
else:
_help += f' {default}' if _help else default
return _help.replace('%', '%%') if issubclass(type(self._root_parser), ArgumentParser) else _help

def _is_field_suppressed(self, field_info: FieldInfo) -> bool:
Expand Down
10 changes: 7 additions & 3 deletions tests/test_source_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,8 @@ class SubModel(BaseModel):

class Settings(BaseSettings, cli_parse_args=True, cli_prog_name='example.py'):
flag: bool = True
toggle: CliToggleFlag[bool] = True
toggle_description: CliToggleFlag[bool] = Field(False, description='Bool Toggle')
sub_model: SubModel = SubModel(flag=False)
opt_model: DeepSubModel | None = Field(None, description='Group Doc')
fact_model: SubModel = Field(default_factory=lambda: SubModel(flag=True))
Expand All @@ -601,9 +603,9 @@ class Settings(BaseSettings, cli_parse_args=True, cli_prog_name='example.py'):
Settings()
assert (
capsys.readouterr().out
== f"""usage: example.py [-h] [--flag bool] [--sub_model [JSON]]
[--sub_model.flag bool] [--sub_model.deep [JSON]]
[--sub_model.deep.flag bool]
== f"""usage: example.py [-h] [--flag bool] [--no-toggle] [--toggle_description]
[--sub_model [JSON]] [--sub_model.flag bool]
[--sub_model.deep [JSON]] [--sub_model.deep.flag bool]
[--sub_model.deep.deeper [{{JSON,null}}]]
[--sub_model.deep.deeper.flag bool]
[--opt_model [{{JSON,null}}]] [--opt_model.flag bool]
Expand All @@ -617,6 +619,8 @@ class Settings(BaseSettings, cli_parse_args=True, cli_prog_name='example.py'):
{ARGPARSE_OPTIONS_TEXT}:
-h, --help show this help message and exit
--flag bool (default: True)
--no-toggle (toggle)
--toggle_description Bool Toggle

sub_model options:
--sub_model [JSON] set sub_model from JSON string (default: {{}})
Expand Down