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 2 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
Expand Up @@ -7,6 +7,7 @@ Unreleased

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- Do not display values in prompts when `show_default=False` on Options
- Do not display default values in prompts when `show_default=False` on options

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.



Version 8.1.4
Expand Down
1 change: 1 addition & 0 deletions src/click/core.py
Expand Up @@ -2865,6 +2865,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),
show_default=(False if self.show_default is False else True),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not simply: show_default=self.show_default?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this class, self.show_default is a Union[bool, str, None], but termui.prompt accepts only bool. Passing through show_default directly gives a type error, and also appears to do the wrong things for values of type filenam

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. Thanks for the explanation.

Would you mind writing it down as a comment in the code? I think it would help others who read this line of code for the first time.

)

def resolve_envvar_value(self, ctx: Context) -> t.Optional[str]:
Expand Down
11 changes: 11 additions & 0 deletions tests/test_termui.py
Expand Up @@ -446,3 +446,14 @@ 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