Skip to content
Myr

Show default string in prompt (#3328)

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

authored by Rowlando13 · committed by GitHub · parents 8c95c73, 8c452e0 · Browse files · 5 files+82 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+82 added−5 removed

  • tests:+66 added−0 removed
  • other:+16 added−5 removed

5 files+82 added−5 removed

modified:src/click/core.py+3 added-3 removed
3181 unmodified lines
3182 else:3183 default = bool(default)3184 return confirm(self.prompt, default)31853186 # If show_default is set to True/False, provide this to `prompt` as well. For3187 # non-bool values of `show_default`, we use `prompt`'s default behavior3186 # If show_default is given, provide this to `prompt` as well,3187 # otherwise we use `prompt`'s default behavior3188 prompt_kwargs: t.Any = {}3189 if isinstance(self.show_default, bool):3189 if self.show_default is not None:3190 prompt_kwargs["show_default"] = self.show_default31913192 return prompt(3193 self.prompt,
263 unmodified lines
modified:src/click/termui.py+10 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 .. versionchanged:: 8.3.3127 ``show_default`` can be a string to show a custom value instead128 of the actual default, matching the help text behavior.129130 .. versionchanged:: 8.3.1131 A space is no longer appended to the prompt.132133 .. 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):
1297 unmodified lines
modified:tests/test_termui.py+53 added
7 unmodified lines
89import click._termui_impl10from click._compat import WIN11from click._termui_impl import Editor12from click._utils import UNSET13from click.exceptions import BadParameter14from click.exceptions import MissingParameter1516
700 unmodified lines
717 result = runner.invoke(cmd, input="my-input", standalone_mode=False)718 assert "my-default-value" not in result.output719720721@pytest.mark.parametrize(722 ("show_default", "default", "user_input", "in_prompt", "not_in_prompt"),723 [724 # Regular string replaces the actual default in the prompt.725 ("custom", "actual", "\n", "(custom)", "actual"),726 # String with spaces.727 ("custom label", "actual", "\n", "(custom label)", "actual"),728 # Unicode characters.729 ("∞", "0", "\n", "(∞)", None),730 # Numeric default: custom string hides the number.731 ("unlimited", 42, "\n", "(unlimited)", "42"),732 # Explicit default=None: custom string still appears, must provide input.733 ("computed at runtime", None, "value\n", "(computed at runtime)", None),734 # No default kwarg at all (internal UNSET sentinel): same as None.735 ("computed at runtime", UNSET, "value\n", "(computed at runtime)", None),736 # Empty string is falsy: suppresses any default display.737 ("", "actual", "\n", None, "actual"),738 ],739 ids=[740 "simple-string",741 "string-with-spaces",742 "unicode",743 "numeric-default",744 "default-is-none",745 "default-is-unset",746 "empty-string-is-falsy",747 ],748)749def test_string_show_default_in_prompt(750 runner, show_default, default, user_input, in_prompt, not_in_prompt751):752 """When show_default is a string, the prompt should display that753 string in parentheses instead of the actual default value,754 matching the help text behavior. See pallets/click#2836."""755756 option_kwargs = {"show_default": show_default, "prompt": True}757 if default is not UNSET:758 option_kwargs["default"] = default759760 @click.command()761 @click.option("--arg1", **option_kwargs)762 def cmd(arg1):763 click.echo(arg1)764765 result = runner.invoke(cmd, input=user_input, standalone_mode=False)766 prompt_line = result.output.split("\n")[0]767 if in_prompt is not None:768 assert in_prompt in prompt_line769 if not_in_prompt is not None:770 assert not_in_prompt not in prompt_line771772773REPEAT = object()774"""Sentinel value to indicate that the prompt is expected to be repeated.775776I.e. the value provided by the user is not satisfactory and need to be re-prompted.
214 unmodified lines
modified:CHANGES.rst+3 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- Show custom ``show_default`` string in prompts, matching the existing25 help text behavior. :issue:`2836` :pr:`2837` :pr:`3165` :pr:`3262` :pr:`3280`26 :pr:`3328``27- Fix ``default=True`` with boolean ``flag_value`` always returning the28 ``flag_value`` instead of ``True``. The ``default=True`` to ``flag_value``29 substitution now only applies to non-boolean flags, where ``True`` acts as a30 sentinel meaning "activate this flag by default". For boolean flags,
1333 unmodified lines