Skip to content
Myr

Fix metavar for Choice options when show_choices=False (#2365)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by mrmups · committed by GitHub · parent 02046e7 · Browse files · 3 files+46 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+46 added−1 removed

  • tests:+36 added−0 removed
  • other:+10 added−1 removed

3 files+46 added−1 removed

modified:src/click/types.py+7 added-1 removed
257 unmodified lines
258 info_dict["case_sensitive"] = self.case_sensitive259 return info_dict260261 def get_metavar(self, param: Parameter) -> str:262 choices_str = "|".join(self.choices)262 if param.param_type_name == "option" and not param.show_choices: # type: ignore263 choice_metavars = [264 convert_type(type(choice)).name.upper() for choice in self.choices265 ]266 choices_str = "|".join([*dict.fromkeys(choice_metavars)])267 else:268 choices_str = "|".join([str(i) for i in self.choices])269270 # Use curly braces to indicate a required argument.271 if param.required and param.param_type_name == "argument":272 return f"{{{choices_str}}}"
825 unmodified lines
modified:tests/test_options.py+36 added
924 unmodified lines
925 with pytest.raises(TypeError) as e:926 click.Option(["-a"], **kwargs)927928 assert message in str(e.value)929930931@pytest.mark.parametrize(932 ("choices", "metavars"),933 [934 pytest.param(["foo", "bar"], "[TEXT]", id="text choices"),935 pytest.param([1, 2], "[INTEGER]", id="int choices"),936 pytest.param([1.0, 2.0], "[FLOAT]", id="float choices"),937 pytest.param([True, False], "[BOOLEAN]", id="bool choices"),938 pytest.param(["foo", 1], "[TEXT|INTEGER]", id="text/int choices"),939 ],940)941def test_usage_show_choices(runner, choices, metavars):942 """When show_choices=False is set, the --help output943 should print choice metavars instead of values.944 """945946 @click.command()947 @click.option("-g", type=click.Choice(choices))948 def cli_with_choices(g):949 pass950951 @click.command()952 @click.option(953 "-g",954 type=click.Choice(choices),955 show_choices=False,956 )957 def cli_without_choices(g):958 pass959960 result = runner.invoke(cli_with_choices, ["--help"])961 assert f"[{'|'.join([str(i) for i in choices])}]" in result.output962963 result = runner.invoke(cli_without_choices, ["--help"])964 assert metavars in result.output
modified:CHANGES.rst+3 added
29 unmodified lines
30 ``from __future__ import annotations``. :pr:`2270`31- When generating a command's name from a decorated function's name, the32 suffixes ``_command``, ``_cmd``, ``_group``, and ``_grp`` are removed.33 :issue:`2322`34- Show the ``types.ParamType.name`` for ``types.Choice`` options within35 ``--help`` message if ``show_choices=False`` is specified.36 :issue:`2356`373839Version 8.1.840-------------
1116 unmodified lines