Skip to content

Commit

Permalink
fix: Do not wrap single choice metavar in braces
Browse files Browse the repository at this point in the history
  • Loading branch information
mrmups authored and AndreasBackx committed Nov 2, 2024
1 parent 96bf716 commit b2642d4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
16 changes: 13 additions & 3 deletions src/click/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,13 +259,23 @@ def to_info_dict(self) -> dict[str, t.Any]:
return info_dict

def get_metavar(self, param: Parameter) -> str:
# Use choice ParamTypes if choices are hidden.
if param.param_type_name == "option" and not param.show_choices: # type: ignore
choice_metavars = [
_choices = [
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])
_choices = [str(i) for i in self.choices]

# Dedupe choices
_choices = [*dict.fromkeys(_choices)]

# Create choices_str
choices_str = "|".join(_choices)

# Use no braces if single choice
if len(_choices) < 2:
return choices_str

# Use curly braces to indicate a required argument.
if param.required and param.param_type_name == "argument":
Expand Down
8 changes: 4 additions & 4 deletions tests/test_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -931,10 +931,10 @@ def test_invalid_flag_combinations(runner, kwargs, message):
@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", "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"),
],
)
Expand Down

0 comments on commit b2642d4

Please sign in to comment.