Skip to content
Myr

Support show_default string in prompts

Before adoption: this commit came in with the history the project adopted, so no landing records it.

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 pallets/click#2837 by Josh Cannon (@thejcannon).

authored by Josh Cannon · committed by Kevin Deldycke · parent 878de46 · Browse files · 5 files+39 added−5 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

5 files+39 added−5 removed

  • tests:+26 added−0 removed
  • other:+13 added−5 removed

5 files+39 added−5 removed

modified:src/click/core.py+3 added-3 removed
3162 unmodified lines
3163 else:3164 default = bool(default)3165 return confirm(self.prompt, default)31663167 # If show_default is set to True/False, provide this to `prompt` as well. For3168 # non-bool values of `show_default`, we use `prompt`'s default behavior3167 # If show_default is given, provide this to `prompt` as well,3168 # otherwise we use `prompt`'s default behavior3169 prompt_kwargs: t.Any = {}3170 if isinstance(self.show_default, bool):3170 if self.show_default is not None:3171 prompt_kwargs["show_default"] = self.show_default31723173 return prompt(3174 self.prompt,
263 unmodified lines
modified:src/click/termui.py+9 added-2 removed
58 unmodified lines
5960def _build_prompt(61 text: str,62 suffix: str,63 show_default: bool = False,63 show_default: bool | str = False,64 default: t.Any | None = None,65 show_choices: bool = True,66 type: ParamType | None = None,67) -> str:68 prompt = text69 if type is not None and show_choices and isinstance(type, Choice):70 prompt += f" ({', '.join(map(str, type.choices))})"71 if isinstance(show_default, str):72 default = f"({show_default})"73 if default is not None and show_default:74 prompt = f"{prompt} [{_format_default(default)}]"75 return f"{prompt}{suffix}"76
12 unmodified lines
89 confirmation_prompt: bool | str = False,90 type: ParamType | t.Any | None = None,91 value_proc: t.Callable[[str], t.Any] | None = None,92 prompt_suffix: str = ": ",91 show_default: bool = True,93 show_default: bool | str = True,94 err: bool = False,95 show_choices: bool = True,96) -> t.Any:97 """Prompts a user for input. This is a convenience function that can
15 unmodified lines
113 is invoked instead of the type conversion to114 convert a value.115 :param prompt_suffix: a suffix that should be added to the prompt.116 :param show_default: shows or hides the default value in the prompt.117 If this value is a string, it shows that string118 in parentheses instead of the actual value.119 :param err: if set to true the file defaults to ``stderr`` instead of120 ``stdout``, the same as with echo.121 :param show_choices: Show or hide choices if the passed type is a Choice.122 For example if type is a Choice of either day or week,123 show_choices is true and text is "Group by" then the124 prompt will be "Group by (day, week): ".125126 .. versionadded:: 8.2127 ``show_default`` can be a custom string.128129 .. versionchanged:: 8.3.1130 A space is no longer appended to the prompt.131132 .. versionadded:: 8.0
758 unmodified lines
modified:tests/test_options.py+13 added
1257 unmodified lines
1258 message = opt.get_help_record(ctx)[1]1259 assert "[default: (unlimited)]" in message126012611262def 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 pass12691270 result = runner.invoke(cmd, input="my-input", standalone_mode=False)1271 assert "(custom)" in result.output1272 assert "my-default-value" not in result.output127312741275class _StrictEq:1276 """Object whose ``__eq__`` raises on string comparison (like semver.Version)."""12771278 def __eq__(self, other):
1262 unmodified lines
modified:tests/test_termui.py+13 added
715 unmodified lines
716 result = runner.invoke(cmd, input="my-input", standalone_mode=False)717 assert "my-default-value" not in result.output718719720def test_string_show_default_shows_custom_string_in_prompt(runner):721 @click.command()722 @click.option(723 "--arg1", show_default="custom", prompt=True, default="my-default-value"724 )725 def cmd(arg1):726 pass727728 result = runner.invoke(cmd, input="my-input", standalone_mode=False)729 assert "(custom)" in result.output730 assert "my-default-value" not in result.output731732733REPEAT = object()734"""Sentinel value to indicate that the prompt is expected to be repeated.735736I.e. the value provided by the user is not satisfactory and need to be re-prompted.
210 unmodified lines
modified:CHANGES.rst+1 added
19 unmodified lines
20- Add optional randomized parallel test execution using ``pytest-randomly`` and21 ``pytest-xdist`` to detect test pollution and race conditions. :pr:`3151`22- Add contributor documentation for running stress tests, randomized23 parallel tests, and Flask smoke tests. :pr:`3151` :pr:`3177`24- Add support for custom ``show_default`` string in prompts. :pr:`460a142`2526Version 8.3.227-------------28
1323 unmodified lines