Mark commands as optional in the synopsis
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Closes #3059
authored by Kevin Deldycke · committed by Kevin Deldycke · parent 8240d25 · Browse files · 5 files·+72 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
5 files+72 added−1 removed
- tests:+53 added−0 removed
- other:+19 added−1 removed
Provenance
- Commit
665736b- Tree
1b5b412- Author
Kevin Deldycke <kevin@deldycke.com>- Committer
Kevin Deldycke <kevin@deldycke.com>- Parent
8240d25
5 files changed+72 added−1 removed
modified:docs/documentation.md+8 added
308 unmodified lines309 invoke(hello, args=['--help'])310 311 ```312 313+```{admonition} Optional elements are bracketed314+:class: note315+316+ The usage line follows a consistent convention: an element is optional when it is enclosed in square brackets and required when it appears outside them. A required argument appears as `FOO` and an optional one as `[FOO]`, with a variadic argument adding a trailing `...` (so `[FOO]...`). A single pair of brackets can group several tokens, so a chained group renders an extra command and its arguments as one optional, repeatable unit: `[COMMAND2 [ARGS]...]...`. A {class}`Group` that can run without naming a subcommand (`invoke_without_command=True`) also brackets the leading command, showing `[COMMAND]` instead of `COMMAND`. The `[OPTIONS]` placeholder is always bracketed, since adding options is itself optional.317+318+ This mirrors the conventional utility-argument syntax for optional arguments and operands ([POSIX §12.1](https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html), [`man-pages(7)`](https://man7.org/linux/man-pages/man7/man-pages.7.html)), where a bracketed expression followed by `...` denotes zero or more occurrences. A subcommand name is a positional operand, so Click brackets it under the same rule.319+```320+321 ## Help Parameter Customization322 323 Help parameters are automatically added by Click for any command. The default is `--help` but can be overridden by the context setting {attr}`~Context.help_option_names`. Click also performs automatic conflict resolution on the default help parameter, so if a command itself implements a parameter named `help` then the default help will not be run.324 17 unmodified linesmodified:src/click/core.py+8 added-1 removed
1621 unmodified lines1622 self.no_args_is_help = no_args_is_help1623 self.invoke_without_command = invoke_without_command1624 1625 if subcommand_metavar is None:1626+ # When the group can run without a subcommand, the leading command1627+ # token is optional, so wrap it in brackets to reflect that.1628 if chain:1629+ if invoke_without_command:1630+ subcommand_metavar = "[COMMAND1] [ARGS]... [COMMAND2 [ARGS]...]..."1631+ else:1627- subcommand_metavar = "COMMAND1 [ARGS]... [COMMAND2 [ARGS]...]..."1632+ subcommand_metavar = "COMMAND1 [ARGS]... [COMMAND2 [ARGS]...]..."1633+ elif invoke_without_command:1634+ subcommand_metavar = "[COMMAND] [ARGS]..."1635 else:1636 subcommand_metavar = "COMMAND [ARGS]..."1637 1638 self.subcommand_metavar = subcommand_metavar1912 unmodified linesmodified:tests/test_arguments.py+26 added
304 unmodified lines305 assert result.exit_code == 0, result.output306 assert "[F!]" in result.output307 308 309+@pytest.mark.parametrize(310+ ("kwargs", "expected"),311+ [312+ ({}, "FOO"),313+ ({"required": True}, "FOO"),314+ ({"required": False}, "[FOO]"),315+ ({"default": "x"}, "[FOO]"),316+ ({"nargs": -1}, "[FOO]..."),317+ ({"nargs": -1, "required": True}, "FOO..."),318+ ({"nargs": 2}, "FOO..."),319+ ({"nargs": 2, "required": False}, "[FOO]..."),320+ ],321+)322+def test_argument_metavar_marks_optional(runner, kwargs, expected):323+ """An argument is bracketed in the usage line only when it is optional."""324+325+ @click.command()326+ @click.argument("foo", **kwargs)327+ def cli(foo):328+ pass329+330+ result = runner.invoke(cli, ["--help"])331+ assert result.exit_code == 0332+ assert result.output.splitlines()[0] == f"Usage: cli [OPTIONS] {expected}"333+334+335 @pytest.mark.parametrize("deprecated", [True, "USE B INSTEAD"])336 def test_deprecated_warning(runner, deprecated):337 @click.command()338 @click.argument(317 unmodified linesmodified:tests/test_commands.py+27 added
297 unmodified lines298 assert not result.exception299 assert result.output == "no subcommand, use default\nin subcommand\n"300 301 302+@pytest.mark.parametrize(303+ ("chain", "invoke_without_command", "metavar"),304+ [305+ (False, False, "COMMAND [ARGS]..."),306+ (False, True, "[COMMAND] [ARGS]..."),307+ (True, False, "COMMAND1 [ARGS]... [COMMAND2 [ARGS]...]..."),308+ (True, True, "[COMMAND1] [ARGS]... [COMMAND2 [ARGS]...]..."),309+ ],310+)311+def test_subcommand_metavar_marks_optional(312+ runner, chain, invoke_without_command, metavar313+):314+ """The leading subcommand token is bracketed only when it is optional."""315+316+ @click.group(chain=chain, invoke_without_command=invoke_without_command)317+ def cli():318+ pass319+320+ @cli.command()321+ def sub():322+ pass323+324+ result = runner.invoke(cli, ["--help"])325+ assert result.exit_code == 0326+ assert result.output.splitlines()[0] == f"Usage: cli [OPTIONS] {metavar}"327+328+329 def test_aliased_command_canonical_name(runner):330 class AliasedGroup(click.Group):331 def get_command(self, ctx, cmd_name):332 return push318 unmodified linesmodified:CHANGES.rst+3 added
9 unmodified lines10 format while still supporting multi-line help. :issue:`3502`11 :issue:`3043` :pr:`3504` :pr:`3508`12 - Deprecated commands and options with empty or missing help text no longer13 render a stray leading space before the ``(DEPRECATED)`` label. :pr:`3509`14+- A :class:`Group` with ``invoke_without_command=True`` marks its subcommand as15+ optional in the usage help, showing ``[COMMAND]`` instead of ``COMMAND``.16+ :issue:`3059` :pr:`3507`17 18 19 Version 8.4.120 -------------1476 unmodified lines