Skip to content
Myr

Annotate required choices with braces

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

Display the metavar for Choice options with curly braces on required params, to reduce user confusion.

authored by Nicolas Simonds · committed by David Lord · parent 990ca8e · Browse files · 3 files+32 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+32 added−1 removed

  • tests:+21 added−0 removed
  • other:+11 added−1 removed

3 files+32 added−1 removed

modified:src/click/types.py+8 added-1 removed
155 unmodified lines
156 self.choices = choices157 self.case_sensitive = case_sensitive158159 def get_metavar(self, param):160 return f"[{'|'.join(self.choices)}]"160 choices_str = "|".join(self.choices)161162 # Use curly braces to indicate a required argument.163 if param.required and param.param_type_name == "argument":164 return f"{{{choices_str}}}"165166 # Use square braces to indicate an option or optional argument.167 return f"[{choices_str}]"168169 def get_missing_message(self, param):170 choice_str = ",\n\t".join(self.choices)171 return f"Choose from:\n\t{choice_str}"172
563 unmodified lines
modified:tests/test_basic.py+21 added
350 unmodified lines
351 result = runner.invoke(cli, ["--help"])352 assert "--method [foo|bar|baz]" in result.output353354355def test_choice_argument(runner):356 @click.command()357 @click.argument("method", type=click.Choice(["foo", "bar", "baz"]))358 def cli(method):359 click.echo(method)360361 result = runner.invoke(cli, ["foo"])362 assert not result.exception363 assert result.output == "foo\n"364365 result = runner.invoke(cli, ["meh"])366 assert result.exit_code == 2367 assert (368 "Invalid value for '{foo|bar|baz}': invalid choice: meh. "369 "(choose from foo, bar, baz)" in result.output370 )371372 result = runner.invoke(cli, ["--help"])373 assert "{foo|bar|baz}" in result.output374375376def test_datetime_option_default(runner):377 @click.command()378 @click.option("--start_date", type=click.DateTime())379 def cli(start_date):
201 unmodified lines
modified:CHANGES.rst+3 added
65 unmodified lines
66 ends. :pr:`1191`67- The result object returned by the test runner's ``invoke()`` method68 has a ``return_value`` attribute with the value returned by the69 invoked command. :pr:`1312`70- Required arguments with the ``Choice`` type show the choices in71 curly braces to indicate that one is required (``{a|b|c}``).72 :issue:`1272`737475Version 7.1.276-------------
635 unmodified lines