Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix metavar for Choice options when show_choices=False #2365

Merged
merged 5 commits into from
Nov 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ Unreleased
- When generating a command's name from a decorated function's name, the
suffixes ``_command``, ``_cmd``, ``_group``, and ``_grp`` are removed.
:issue:`2322`
- Show the ``types.ParamType.name`` for ``types.Choice`` options within
``--help`` message if ``show_choices=False`` is specified.
:issue:`2356`


Version 8.1.8
Expand Down
8 changes: 7 additions & 1 deletion src/click/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,13 @@ def to_info_dict(self) -> dict[str, t.Any]:
return info_dict

def get_metavar(self, param: Parameter) -> str:
choices_str = "|".join(self.choices)
if param.param_type_name == "option" and not param.show_choices: # type: ignore
choice_metavars = [
convert_type(type(choice)).name.upper() for choice in self.choices
]
choices_str = "|".join([*dict.fromkeys(choice_metavars)])
else:
choices_str = "|".join([str(i) for i in self.choices])

# Use curly braces to indicate a required argument.
if param.required and param.param_type_name == "argument":
Expand Down
36 changes: 36 additions & 0 deletions tests/test_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -926,3 +926,39 @@ def test_invalid_flag_combinations(runner, kwargs, message):
click.Option(["-a"], **kwargs)

assert message in str(e.value)


@pytest.mark.parametrize(
("choices", "metavars"),
[
pytest.param(["foo", "bar"], "[TEXT]", id="text choices"),
pytest.param([1, 2], "[INTEGER]", id="int choices"),
pytest.param([1.0, 2.0], "[FLOAT]", id="float choices"),
pytest.param([True, False], "[BOOLEAN]", id="bool choices"),
pytest.param(["foo", 1], "[TEXT|INTEGER]", id="text/int choices"),
],
)
def test_usage_show_choices(runner, choices, metavars):
"""When show_choices=False is set, the --help output
should print choice metavars instead of values.
"""

@click.command()
@click.option("-g", type=click.Choice(choices))
def cli_with_choices(g):
pass

@click.command()
@click.option(
"-g",
type=click.Choice(choices),
show_choices=False,
)
def cli_without_choices(g):
pass

result = runner.invoke(cli_with_choices, ["--help"])
assert f"[{'|'.join([str(i) for i in choices])}]" in result.output

result = runner.invoke(cli_without_choices, ["--help"])
assert metavars in result.output