From a89852708d3436c298cb58f395d7359df3989380 Mon Sep 17 00:00:00 2001 From: Josh Cannon <3956745+thejcannon@users.noreply.github.com> Date: Mon, 6 Jan 2025 16:03:09 -0600 Subject: [PATCH 1/9] Use `show_default` if its a string --- src/click/core.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/click/core.py b/src/click/core.py index e783729c7e..06c2752dba 100644 --- a/src/click/core.py +++ b/src/click/core.py @@ -2927,11 +2927,12 @@ 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 set to True/False, provide this to `prompt` as well. prompt_kwargs: t.Any = {} if isinstance(self.show_default, bool): prompt_kwargs["show_default"] = self.show_default + elif isinstance(self.show_default, str): + default=show_default return prompt( self.prompt, From 02192a03ae8f3098aff133cfab08540e63b906f8 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci-lite[bot]" <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Date: Mon, 6 Jan 2025 22:09:43 +0000 Subject: [PATCH 2/9] [pre-commit.ci lite] apply automatic fixes --- src/click/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/click/core.py b/src/click/core.py index 06c2752dba..0b74914936 100644 --- a/src/click/core.py +++ b/src/click/core.py @@ -2932,7 +2932,7 @@ def prompt_for_value(self, ctx: Context) -> t.Any: if isinstance(self.show_default, bool): prompt_kwargs["show_default"] = self.show_default elif isinstance(self.show_default, str): - default=show_default + default = show_default return prompt( self.prompt, From 1fb06a2b7be013add96d8d469e3ada7f4c88b541 Mon Sep 17 00:00:00 2001 From: Josh Cannon <3956745+thejcannon@users.noreply.github.com> Date: Mon, 6 Jan 2025 16:14:26 -0600 Subject: [PATCH 3/9] Update core.py --- src/click/core.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/click/core.py b/src/click/core.py index 0b74914936..2d000a8220 100644 --- a/src/click/core.py +++ b/src/click/core.py @@ -2927,12 +2927,11 @@ 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. + # 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 - elif isinstance(self.show_default, str): - default = show_default return prompt( self.prompt, From f6e5792cfe445e595d21f808cd4e9cdca4eb2036 Mon Sep 17 00:00:00 2001 From: Josh Cannon <3956745+thejcannon@users.noreply.github.com> Date: Mon, 6 Jan 2025 16:19:25 -0600 Subject: [PATCH 4/9] Update termui.py --- src/click/termui.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/click/termui.py b/src/click/termui.py index dcbb22216c..16cb6d25f1 100644 --- a/src/click/termui.py +++ b/src/click/termui.py @@ -60,7 +60,7 @@ 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, @@ -68,6 +68,8 @@ def _build_prompt( 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}" @@ -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: @@ -112,6 +114,8 @@ 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. From b82da0984aed9c0828ea07c83335e40c71ef40d4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci-lite[bot]" <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Date: Mon, 6 Jan 2025 22:19:55 +0000 Subject: [PATCH 5/9] [pre-commit.ci lite] apply automatic fixes --- src/click/termui.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/click/termui.py b/src/click/termui.py index 16cb6d25f1..431e2ca4b2 100644 --- a/src/click/termui.py +++ b/src/click/termui.py @@ -114,7 +114,7 @@ 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 + 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. From 195ebbaa499410d1fc5e37bdcdfa1750111d48d4 Mon Sep 17 00:00:00 2001 From: Josh Cannon <3956745+thejcannon@users.noreply.github.com> Date: Mon, 6 Jan 2025 16:23:15 -0600 Subject: [PATCH 6/9] Update termui.py --- src/click/termui.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/click/termui.py b/src/click/termui.py index 431e2ca4b2..4a06be23ca 100644 --- a/src/click/termui.py +++ b/src/click/termui.py @@ -123,6 +123,9 @@ def prompt( 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. From 460a14254c5ad932e3ebec86e215036562ed3ac5 Mon Sep 17 00:00:00 2001 From: Joshua Cannon <3956745+thejcannon@users.noreply.github.com> Date: Mon, 6 Jan 2025 16:34:03 -0600 Subject: [PATCH 7/9] add tests --- tests/test_options.py | 11 +++++++++++ tests/test_termui.py | 11 +++++++++++ 2 files changed, 22 insertions(+) diff --git a/tests/test_options.py b/tests/test_options.py index b7267c1821..d4a015bf78 100644 --- a/tests/test_options.py +++ b/tests/test_options.py @@ -856,6 +856,17 @@ 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) diff --git a/tests/test_termui.py b/tests/test_termui.py index ad9d0a66c2..81606fda8a 100644 --- a/tests/test_termui.py +++ b/tests/test_termui.py @@ -485,3 +485,14 @@ 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 From 6f2f16423a2dcd3f9177ef59ce4688ad45ee186c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci-lite[bot]" <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Date: Mon, 6 Jan 2025 22:34:54 +0000 Subject: [PATCH 8/9] [pre-commit.ci lite] apply automatic fixes --- tests/test_options.py | 4 +++- tests/test_termui.py | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/test_options.py b/tests/test_options.py index d4a015bf78..678184dc59 100644 --- a/tests/test_options.py +++ b/tests/test_options.py @@ -858,7 +858,9 @@ def test_show_default_string(runner): 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") + @click.option( + "--arg1", show_default="custom", prompt=True, default="my-default-value" + ) def cmd(arg1): pass diff --git a/tests/test_termui.py b/tests/test_termui.py index 81606fda8a..04ac99460c 100644 --- a/tests/test_termui.py +++ b/tests/test_termui.py @@ -489,7 +489,9 @@ def cmd(arg1): 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") + @click.option( + "--arg1", show_default="custom", prompt=True, default="my-default-value" + ) def cmd(arg1): pass From 304f5fac6d477515fe563b69083380eb1351b0dc Mon Sep 17 00:00:00 2001 From: Joshua Cannon <3956745+thejcannon@users.noreply.github.com> Date: Mon, 6 Jan 2025 20:02:25 -0600 Subject: [PATCH 9/9] CHANGES --- CHANGES.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.rst b/CHANGES.rst index 057d700df3..f3d398354b 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -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`