Skip to content

Commit

Permalink
Hide default value when show_default is False (#2509)
Browse files Browse the repository at this point in the history
Co-authored-by: Andreas Backx <andreas@backx.org>
  • Loading branch information
pfhayes and AndreasBackx authored Nov 2, 2024
1 parent 1a4d8c1 commit a6e0d29
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ Unreleased
- Show the ``types.ParamType.name`` for ``types.Choice`` options within
``--help`` message if ``show_choices=False`` is specified.
:issue:`2356`
- Do not display default values in prompts when ``Option.show_default`` is
``False``. :pr:`2509`


Version 8.1.8
Expand Down
7 changes: 7 additions & 0 deletions src/click/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2795,6 +2795,12 @@ def prompt_for_value(self, ctx: Context) -> t.Any:
if self.is_bool_flag:
return confirm(self.prompt, default)

# If show_default is set to True/False, provide this to `prompt` as well. For
# non-bool values of `show_default`, we use `prompt`'s default behavior
prompt_kwargs: t.Any = {}
if isinstance(self.show_default, bool):
prompt_kwargs["show_default"] = self.show_default

return prompt(
self.prompt,
default=default,
Expand All @@ -2803,6 +2809,7 @@ def prompt_for_value(self, ctx: Context) -> t.Any:
show_choices=self.show_choices,
confirmation_prompt=self.confirmation_prompt,
value_proc=lambda x: self.process_value(ctx, x),
**prompt_kwargs,
)

def resolve_envvar_value(self, ctx: Context) -> str | None:
Expand Down
12 changes: 12 additions & 0 deletions tests/test_termui.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,3 +447,15 @@ def cli(password):

if prompt == "Confirm Password":
assert "Confirm Password: " in result.output


def test_false_show_default_cause_no_default_display_in_prompt(runner):
@click.command()
@click.option("--arg1", show_default=False, prompt=True, default="my-default-value")
def cmd(arg1):
pass

# Confirm that the default value is not included in the output when `show_default`
# is False
result = runner.invoke(cmd, input="my-input", standalone_mode=False)
assert "my-default-value" not in result.output

0 comments on commit a6e0d29

Please sign in to comment.