Skip to content
Myr

Fix double-bracketing of choices in synopsis (#3578)

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

authored by Rowlando13 · committed by GitHub · parents d9ff133, 762c97e · Browse files · 3 files+48 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+48 added−1 removed

  • tests:+40 added−0 removed
  • other:+8 added−1 removed

3 files+48 added−1 removed

modified:src/click/core.py+6 added-1 removed
3569 unmodified lines
3570 return self.metavar3571 var = self.type.get_metavar(param=self, ctx=ctx)3572 if not var:3573 var = self.name.upper()3574 # Types like ``Choice`` and ``DateTime`` already surround their metavar3575 # with square brackets to enumerate the allowed values. Reuse those3576 # outer brackets as the optional-argument indicator instead of wrapping3577 # the metavar in a second pair, which would produce ``[[a|b|c]]``.3578 already_bracketed = var.startswith("[") and var.endswith("]")3579 if self.deprecated:3580 var += "!"3576 if not self.required:3581 if not self.required and not already_bracketed:3582 var = f"[{var}]"3583 if self.nargs != 1:3584 var += "..."3585 return var
54 unmodified lines
modified:tests/test_basic.py+40 added
567 unmodified lines
568 result = runner.invoke(cli, ["--help"])569 assert result.output.startswith("Usage: cli [OPTIONS] {not-none|none}\n")570571572def test_choice_argument_optional_metavar(runner):573 """Optional Choice arguments reuse the type's brackets instead of doubling.574575 Without this the usage line for a ``Choice`` argument with ``nargs=-1`` or576 ``required=False`` rendered as ``[[a|b|c]]``: one pair from ``Choice`` to577 enumerate values, a second pair from ``Argument`` to mark it optional.578 """579580 @click.command()581 @click.argument("method", type=click.Choice(["foo", "bar", "baz"]), nargs=-1)582 def cli_variadic(method):583 pass584585 @click.command()586 @click.argument("method", type=click.Choice(["foo", "bar", "baz"]), required=False)587 def cli_optional(method):588 pass589590 variadic = runner.invoke(cli_variadic, ["--help"]).output591 assert "Usage: cli-variadic [OPTIONS] [foo|bar|baz]...\n" in variadic592 assert "[[foo|bar|baz]]" not in variadic593594 optional = runner.invoke(cli_optional, ["--help"]).output595 assert "Usage: cli-optional [OPTIONS] [foo|bar|baz]\n" in optional596 assert "[[foo|bar|baz]]" not in optional597598599def test_datetime_argument_optional_metavar(runner):600 """``DateTime`` arguments behave the same way as ``Choice``."""601602 @click.command()603 @click.argument("when", type=click.DateTime(formats=["%Y-%m-%d"]), required=False)604 def cli(when):605 pass606607 result = runner.invoke(cli, ["--help"])608 assert "Usage: cli [OPTIONS] [%Y-%m-%d]\n" in result.output609 assert "[[%Y-%m-%d]]" not in result.output610611612def test_datetime_option_default(runner):613 @click.command()614 @click.option("--start_date", type=click.DateTime())615 def cli(start_date):
166 unmodified lines
modified:CHANGES.md+2 added
16 unmodified lines
17- `echo_via_pager` and `get_pager_file` no longer close a borrowed stdout18 stream when no external pager runs, completing the partial19 `I/O operation on closed file` fix from {pr}`3482`. {issue}`3449`20 {pr}`3533`21- Fix CLI usage symopsis for optional arguments producing double square brackets22 `[[a|b|c]]...` whose type already brackets their metavar. {pr}`3578`2324## Version 8.4.12526Released 2026-05-21
1532 unmodified lines