Skip to content

Commit

Permalink
✨ Add callable support to show_default
Browse files Browse the repository at this point in the history
Based on tiangolo#355.
  • Loading branch information
alexreg committed Feb 28, 2023
1 parent 5b8370a commit 40acc2c
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 26 deletions.
Expand Up @@ -18,7 +18,7 @@ def test_help():
assert columns_match(
result.output,
"[NAME]",
"Who to greet [default: (Deadpoolio the amazing's name)]",
"Who to greet [default: Deadpoolio the amazing's name]",
)


Expand Down
36 changes: 17 additions & 19 deletions typer_cloup/core.py
@@ -1,4 +1,3 @@
import inspect
import os
import sys
from gettext import gettext as _
Expand Down Expand Up @@ -73,7 +72,7 @@ def __init__(
# TyperParameterMixin
convertor: Optional[Any] = None,
# TyperArgument
show_default: Union[bool, str] = True,
show_default: Union[bool, str, Callable[[], Union[bool, str]]] = True,
show_choices: bool = True,
show_envvar: bool = True,
allow_from_autoenv: bool = False,
Expand Down Expand Up @@ -163,13 +162,16 @@ def get_help_record(self, ctx: click.Context) -> Tuple[str, str]:
finally:
ctx.resilient_parsing = resilient

show_default_is_str = isinstance(self.show_default, str)
show_default = (
self.show_default() if callable(self.show_default) else self.show_default
)
show_default_is_str = isinstance(show_default, str)

if show_default_is_str or (
default_value is not None and (self.show_default or ctx.show_default)
default_value is not None and (show_default or ctx.show_default)
):
if show_default_is_str:
default_string = f"({self.show_default})"
default_string = str(show_default)
elif isinstance(default_value, (list, tuple)):
default_string = ", ".join(str(d) for d in default_value)
elif callable(default_value):
Expand Down Expand Up @@ -241,7 +243,7 @@ def __init__(
# TyperParameterMixin
convertor: Optional[Any] = None,
# click.Option
show_default: Union[bool, str] = False,
show_default: Union[bool, str, Callable[[], Union[bool, str]]] = False,
prompt: Union[bool, str] = False,
confirmation_prompt: Union[bool, str] = False,
prompt_required: bool = True,
Expand Down Expand Up @@ -373,23 +375,19 @@ def _write_opts(opts: Sequence[str]) -> str:
finally:
ctx.resilient_parsing = resilient

show_default = False
show_default_is_str = False

if self.show_default is not None:
if isinstance(self.show_default, str):
show_default_is_str = show_default = True
else:
show_default = self.show_default
elif ctx.show_default is not None: # pragma: no cover
show_default = ctx.show_default
show_default = (
self.show_default() if callable(self.show_default) else self.show_default
)
show_default_is_str = isinstance(show_default, str)

if show_default_is_str or (show_default and (default_value is not None)):
if show_default_is_str or (
default_value is not None and (show_default or ctx.show_default)
):
if show_default_is_str:
default_string = f"({self.show_default})"
default_string = str(self.show_default)
elif isinstance(default_value, (list, tuple)):
default_string = ", ".join(str(d) for d in default_value)
elif inspect.isfunction(default_value):
elif callable(default_value):
default_string = _("(dynamic)")
elif self.is_bool_flag and self.secondary_opts:
# For boolean flags that have distinct True/False opts,
Expand Down
2 changes: 1 addition & 1 deletion typer_cloup/main.py
Expand Up @@ -574,7 +574,7 @@ def get_params_convertors_ctx_param_name_from_function(
params[param_name] = click_param
callback_obj = cast(Any, callback)
if hasattr(callback_obj, "__opt_groups"):
for (opt_group, opt_group_params) in reversed(callback_obj.__opt_groups):
for opt_group, opt_group_params in reversed(callback_obj.__opt_groups):
for param_name in opt_group_params:
option = params.get(param_name, None)
if option is None:
Expand Down
6 changes: 3 additions & 3 deletions typer_cloup/models.py
Expand Up @@ -268,7 +268,7 @@ def __init__(
]
] = None,
# TyperArgument
show_default: Union[bool, str] = True,
show_default: Union[bool, str, Callable[[], Union[bool, str]]] = True,
show_choices: bool = True,
show_envvar: bool = True,
allow_from_autoenv: bool = False,
Expand Down Expand Up @@ -363,7 +363,7 @@ def __init__(
]
] = None,
# Option
show_default: Union[bool, str] = True,
show_default: Union[bool, str, Callable[[], Union[bool, str]]] = True,
prompt: Union[bool, str] = False,
confirmation_prompt: bool = False,
prompt_required: bool = True,
Expand Down Expand Up @@ -476,7 +476,7 @@ def __init__(
]
] = None,
# TyperArgument
show_default: Union[bool, str] = True,
show_default: Union[bool, str, Callable[[], Union[bool, str]]] = True,
show_choices: bool = True,
show_envvar: bool = True,
allow_from_autoenv: bool = False,
Expand Down
4 changes: 2 additions & 2 deletions typer_cloup/params.py
Expand Up @@ -30,7 +30,7 @@ def Option(
]
] = None,
# Option
show_default: Union[bool, str] = True,
show_default: Union[bool, str, Callable[[], Union[bool, str]]] = True,
prompt: Union[bool, str] = False,
confirmation_prompt: bool = False,
prompt_required: bool = True,
Expand Down Expand Up @@ -143,7 +143,7 @@ def Argument(
]
] = None,
# TyperArgument
show_default: Union[bool, str] = True,
show_default: Union[bool, str, Callable[[], Union[bool, str]]] = True,
show_choices: bool = True,
show_envvar: bool = True,
allow_from_autoenv: bool = False,
Expand Down

0 comments on commit 40acc2c

Please sign in to comment.