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

When prompting, use show_default as the default if its a string #2837

Open
wants to merge 9 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Version 8.2.0

Unreleased

- Add support for custom ``show_default`` string in prompts. :pr:`460a142`
- Drop support for Python 3.7. :pr:`2588`
- Use modern packaging metadata with ``pyproject.toml`` instead of ``setup.cfg``.
:pr:`326`
Expand Down
6 changes: 3 additions & 3 deletions src/click/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2927,10 +2927,10 @@ 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
# If show_default is given, provide this to `prompt` as well,
# otherwise we use `prompt`'s default behavior
prompt_kwargs: t.Any = {}
if isinstance(self.show_default, bool):
if self.show_default is not None:
prompt_kwargs["show_default"] = self.show_default

return prompt(
Expand Down
11 changes: 9 additions & 2 deletions src/click/termui.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,16 @@ def hidden_prompt_func(prompt: str) -> str:
def _build_prompt(
text: str,
suffix: str,
show_default: bool = False,
show_default: bool | str = False,
default: t.Any | None = None,
show_choices: bool = True,
type: ParamType | None = None,
) -> str:
prompt = text
if type is not None and show_choices and isinstance(type, Choice):
prompt += f" ({', '.join(map(str, type.choices))})"
if isinstance(show_default, str):
default = f"({show_default})"
if default is not None and show_default:
prompt = f"{prompt} [{_format_default(default)}]"
return f"{prompt}{suffix}"
Expand All @@ -88,7 +90,7 @@ def prompt(
type: ParamType | t.Any | None = None,
value_proc: t.Callable[[str], t.Any] | None = None,
prompt_suffix: str = ": ",
show_default: bool = True,
show_default: bool | str = True,
err: bool = False,
show_choices: bool = True,
) -> t.Any:
Expand All @@ -112,13 +114,18 @@ def prompt(
convert a value.
:param prompt_suffix: a suffix that should be added to the prompt.
:param show_default: shows or hides the default value in the prompt.
If this value is a string, it shows that string
in parentheses instead of the actual value.
:param err: if set to true the file defaults to ``stderr`` instead of
``stdout``, the same as with echo.
:param show_choices: Show or hide choices if the passed type is a Choice.
For example if type is a Choice of either day or week,
show_choices is true and text is "Group by" then the
prompt will be "Group by (day, week): ".

.. versionadded:: 8.2
``show_default`` can be a custom string.

.. versionadded:: 8.0
``confirmation_prompt`` can be a custom string.

Expand Down
13 changes: 13 additions & 0 deletions tests/test_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,19 @@ def test_show_default_string(runner):
assert "[default: (unlimited)]" in message


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

result = runner.invoke(cmd, input="my-input", standalone_mode=False)
assert "(custom)" in result.output
assert "my-default-value" not in result.output


def test_show_default_with_empty_string(runner):
"""When show_default is True and default is set to an empty string."""
opt = click.Option(["--limit"], default="", show_default=True)
Expand Down
13 changes: 13 additions & 0 deletions tests/test_termui.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,3 +485,16 @@ def cmd(arg1):
# is False
result = runner.invoke(cmd, input="my-input", standalone_mode=False)
assert "my-default-value" not in result.output


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

result = runner.invoke(cmd, input="my-input", standalone_mode=False)
assert "(custom)" in result.output
assert "my-default-value" not in result.output
Loading