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
Provenance
- Commit
09195f6- Tree
d8029e1- Author
Rowlando13 <67291205+Rowlando13@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent 1
d9ff133- Parent 2
762c97e
3 files changed+48 added−1 removed
modified:src/click/core.py+6 added-1 removed
3569 unmodified lines3570 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 var54 unmodified linesmodified:tests/test_basic.py+40 added
567 unmodified lines568 result = runner.invoke(cli, ["--help"])569 assert result.output.startswith("Usage: cli [OPTIONS] {not-none|none}\n")570 571 572+def test_choice_argument_optional_metavar(runner):573+ """Optional Choice arguments reuse the type's brackets instead of doubling.574+575+ 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+ """579+580+ @click.command()581+ @click.argument("method", type=click.Choice(["foo", "bar", "baz"]), nargs=-1)582+ def cli_variadic(method):583+ pass584+585+ @click.command()586+ @click.argument("method", type=click.Choice(["foo", "bar", "baz"]), required=False)587+ def cli_optional(method):588+ pass589+590+ variadic = runner.invoke(cli_variadic, ["--help"]).output591+ assert "Usage: cli-variadic [OPTIONS] [foo|bar|baz]...\n" in variadic592+ assert "[[foo|bar|baz]]" not in variadic593+594+ optional = runner.invoke(cli_optional, ["--help"]).output595+ assert "Usage: cli-optional [OPTIONS] [foo|bar|baz]\n" in optional596+ assert "[[foo|bar|baz]]" not in optional597+598+599+def test_datetime_argument_optional_metavar(runner):600+ """``DateTime`` arguments behave the same way as ``Choice``."""601+602+ @click.command()603+ @click.argument("when", type=click.DateTime(formats=["%Y-%m-%d"]), required=False)604+ def cli(when):605+ pass606+607+ result = runner.invoke(cli, ["--help"])608+ assert "Usage: cli [OPTIONS] [%Y-%m-%d]\n" in result.output609+ assert "[[%Y-%m-%d]]" not in result.output610+611+612 def test_datetime_option_default(runner):613 @click.command()614 @click.option("--start_date", type=click.DateTime())615 def cli(start_date):166 unmodified linesmodified:CHANGES.md+2 added
16 unmodified lines17 - `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`23 24 ## Version 8.4.125 26 Released 2026-05-211532 unmodified lines