From 3f4eb622805f2402b676e87c3e018a122650d594 Mon Sep 17 00:00:00 2001 From: Paul Date: Sun, 7 Dec 2025 21:52:11 +0530 Subject: [PATCH] Support show_default string in prompts Fixes #2836 When show_default is set to a string on an Option with prompt=True, the string was only used in the help text but not in the actual prompt. Now the custom string is also shown in the prompt. Changes: - Update _build_prompt() to accept str | bool for show_default - When show_default is a string, display it in parentheses like the help text - Update prompt() signature to accept str | bool for show_default - Pass show_default to prompt() regardless of type (was only passing bool) Before: Help: --name TEXT [default: (show_default)] Prompt: Name [default]: After: Help: --name TEXT [default: (show_default)] Prompt: Name [(show_default)]: --- src/click/core.py | 5 ++--- src/click/termui.py | 14 ++++++++++---- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/click/core.py b/src/click/core.py index 6adc65ccd6..adf72488db 100644 --- a/src/click/core.py +++ b/src/click/core.py @@ -3145,10 +3145,9 @@ def prompt_for_value(self, ctx: Context) -> t.Any: default = bool(default) 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/string, provide this to `prompt` as well. 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( diff --git a/src/click/termui.py b/src/click/termui.py index 2e98a0771c..8992d068d8 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,8 +68,12 @@ 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 default is not None and show_default: - prompt = f"{prompt} [{_format_default(default)}]" + if show_default: + if isinstance(show_default, str): + # Use the custom string for display + prompt = f"{prompt} [({show_default})]" + elif default is not None: + prompt = f"{prompt} [{_format_default(default)}]" return f"{prompt}{suffix}" @@ -88,7 +92,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 +116,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. + Can also be a string to show a custom value instead of the actual + default. :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.