Skip to content

Commit 446c139

Browse files
thejcannonkdeldycke
authored andcommitted
Support show_default string in prompts
When `show_default` is set to a string on an option with `prompt=True`, use that string in the prompt instead of the actual default value, matching the behavior of the help text. Squashed from #2837 by Josh Cannon (@thejcannon).
1 parent 1339fd3 commit 446c139

5 files changed

Lines changed: 39 additions & 5 deletions

File tree

CHANGES.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Unreleased
1313
``semver.Version``. :issue:`3298` :pr:`3299`
1414
- Fix pager test pollution under parallel execution by using pytest's
1515
``tmp_path`` fixture instead of a shared temporary file path. :pr:`3238`
16+
- Add support for custom ``show_default`` string in prompts. :pr:`460a142`
1617

1718
Version 8.3.2
1819
-------------

src/click/core.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3164,10 +3164,10 @@ def prompt_for_value(self, ctx: Context) -> t.Any:
31643164
default = bool(default)
31653165
return confirm(self.prompt, default)
31663166

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

31733173
return prompt(

src/click/termui.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,16 @@ def hidden_prompt_func(prompt: str) -> str:
6060
def _build_prompt(
6161
text: str,
6262
suffix: str,
63-
show_default: bool = False,
63+
show_default: bool | str = False,
6464
default: t.Any | None = None,
6565
show_choices: bool = True,
6666
type: ParamType | None = None,
6767
) -> str:
6868
prompt = text
6969
if type is not None and show_choices and isinstance(type, Choice):
7070
prompt += f" ({', '.join(map(str, type.choices))})"
71+
if isinstance(show_default, str):
72+
default = f"({show_default})"
7173
if default is not None and show_default:
7274
prompt = f"{prompt} [{_format_default(default)}]"
7375
return f"{prompt}{suffix}"
@@ -88,7 +90,7 @@ def prompt(
8890
type: ParamType | t.Any | None = None,
8991
value_proc: t.Callable[[str], t.Any] | None = None,
9092
prompt_suffix: str = ": ",
91-
show_default: bool = True,
93+
show_default: bool | str = True,
9294
err: bool = False,
9395
show_choices: bool = True,
9496
) -> t.Any:
@@ -112,13 +114,18 @@ def prompt(
112114
convert a value.
113115
:param prompt_suffix: a suffix that should be added to the prompt.
114116
:param show_default: shows or hides the default value in the prompt.
117+
If this value is a string, it shows that string
118+
in parentheses instead of the actual value.
115119
:param err: if set to true the file defaults to ``stderr`` instead of
116120
``stdout``, the same as with echo.
117121
:param show_choices: Show or hide choices if the passed type is a Choice.
118122
For example if type is a Choice of either day or week,
119123
show_choices is true and text is "Group by" then the
120124
prompt will be "Group by (day, week): ".
121125
126+
.. versionadded:: 8.2
127+
``show_default`` can be a custom string.
128+
122129
.. versionchanged:: 8.3.1
123130
A space is no longer appended to the prompt.
124131

tests/test_options.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1259,6 +1259,19 @@ def test_show_default_string(runner):
12591259
assert "[default: (unlimited)]" in message
12601260

12611261

1262+
def test_string_show_default_shows_custom_string_in_prompt(runner):
1263+
@click.command()
1264+
@click.option(
1265+
"--arg1", show_default="custom", prompt=True, default="my-default-value"
1266+
)
1267+
def cmd(arg1):
1268+
pass
1269+
1270+
result = runner.invoke(cmd, input="my-input", standalone_mode=False)
1271+
assert "(custom)" in result.output
1272+
assert "my-default-value" not in result.output
1273+
1274+
12621275
class _StrictEq:
12631276
"""Object whose ``__eq__`` raises on string comparison (like semver.Version)."""
12641277

tests/test_termui.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,19 @@ def cmd(arg1):
612612
assert "my-default-value" not in result.output
613613

614614

615+
def test_string_show_default_shows_custom_string_in_prompt(runner):
616+
@click.command()
617+
@click.option(
618+
"--arg1", show_default="custom", prompt=True, default="my-default-value"
619+
)
620+
def cmd(arg1):
621+
pass
622+
623+
result = runner.invoke(cmd, input="my-input", standalone_mode=False)
624+
assert "(custom)" in result.output
625+
assert "my-default-value" not in result.output
626+
627+
615628
REPEAT = object()
616629
"""Sentinel value to indicate that the prompt is expected to be repeated.
617630

0 commit comments

Comments
 (0)