Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hide default value when show_default is False #2509

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Unreleased

- Use modern packaging metadata with ``pyproject.toml`` instead of ``setup.cfg``.
:pr:`326`
- Do not display default values in prompts when `show_default=False` on options


Version 8.1.4
Expand Down
7 changes: 7 additions & 0 deletions src/click/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2857,6 +2857,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 type(self.show_default) is bool:
prompt_kwargs["show_default"] = self.show_default

return prompt(
self.prompt,
default=default,
Expand All @@ -2865,6 +2871,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) -> t.Optional[str]:
Expand Down
12 changes: 12 additions & 0 deletions tests/test_termui.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,3 +446,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)
saroad2 marked this conversation as resolved.
Show resolved Hide resolved
assert "my-default-value" not in result.output