diff --git a/typer/rich_utils.py b/typer/rich_utils.py index d85043238c..4999911f02 100644 --- a/typer/rich_utils.py +++ b/typer/rich_utils.py @@ -361,31 +361,64 @@ def _print_options_panel( opt_short_strs = [] secondary_opt_long_strs = [] secondary_opt_short_strs = [] + + # Determine if this parameter has a custom metavar + metavar_str = param.make_metavar(ctx=ctx) + + # For Arguments, check if metavar is a custom value or just the default + # Default metavar is either PARAM_NAME or [PARAM_NAME] for optional args + is_default_metavar = False + if isinstance(param, click.Argument) and param.name: + expected_upper = param.name.upper() + # Check if metavar is the default (with or without brackets) + if metavar_str == expected_upper or metavar_str == f"[{expected_upper}]": + is_default_metavar = True + + has_custom_metavar = ( + isinstance(param, click.Argument) and not is_default_metavar + ) + + # Process opts for opt_str in param.opts: if "--" in opt_str: opt_long_strs.append(opt_str) + elif has_custom_metavar: + # Arguments with custom metavar: use metavar as name + opt_short_strs.append(metavar_str) else: opt_short_strs.append(opt_str) + for opt_str in param.secondary_opts: if "--" in opt_str: secondary_opt_long_strs.append(opt_str) else: secondary_opt_short_strs.append(opt_str) - # Column for a metavar, if we have one + # Column for displaying type or metavar metavar = Text(style=STYLE_METAVAR, overflow="fold") - metavar_str = param.make_metavar(ctx=ctx) - # Do it ourselves if this is a positional argument - if ( - isinstance(param, click.Argument) - and param.name - and metavar_str == param.name.upper() - ): - metavar_str = param.type.name.upper() - # Skip booleans and choices (handled above) - if metavar_str != "BOOLEAN": - metavar.append(metavar_str) + # For Arguments with custom metavar: show type in metavar column + # For other parameters: use original logic + if isinstance(param, click.Argument) and has_custom_metavar: + # Arguments with custom metavar: show the type, not metavar + type_str = param.type.name.upper() + if type_str != "BOOLEAN": + metavar.append(type_str) + else: + # Original logic for Options and Arguments without custom metavar + # For Arguments without custom metavar, metavar_str might be the uppercase name + if ( + isinstance(param, click.Argument) + and param.name + and ( + metavar_str == param.name.upper() + or metavar_str == f"[{param.name.upper()}]" + ) + ): + metavar_str = param.type.name.upper() + + if metavar_str != "BOOLEAN": + metavar.append(metavar_str) # Range - from # https://github.com/pallets/click/blob/c63c70dabd3f86ca68678b4f00951f78f52d0270/src/click/core.py#L2698-L2706 # noqa: E501