Skip to content
Myr

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

5 files+72 added−1 removed

modified:docs/documentation.md+8 added
308 unmodified lines
309 invoke(hello, args=['--help'])310311```312313```{admonition} Optional elements are bracketed314:class: note315316 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.317318 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```320321## Help Parameter Customization322323Help 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 lines
modified:src/click/core.py+8 added-1 removed
1621 unmodified lines
1622 self.no_args_is_help = no_args_is_help1623 self.invoke_without_command = invoke_without_command16241625 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]..."16371638 self.subcommand_metavar = subcommand_metavar
1912 unmodified lines
modified:tests/test_arguments.py+26 added
304 unmodified lines
305 assert result.exit_code == 0, result.output306 assert "[F!]" in result.output307308309@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)322def test_argument_metavar_marks_optional(runner, kwargs, expected):323 """An argument is bracketed in the usage line only when it is optional."""324325 @click.command()326 @click.argument("foo", **kwargs)327 def cli(foo):328 pass329330 result = runner.invoke(cli, ["--help"])331 assert result.exit_code == 0332 assert result.output.splitlines()[0] == f"Usage: cli [OPTIONS] {expected}"333334335@pytest.mark.parametrize("deprecated", [True, "USE B INSTEAD"])336def test_deprecated_warning(runner, deprecated):337 @click.command()338 @click.argument(
317 unmodified lines
modified:tests/test_commands.py+27 added
297 unmodified lines
298 assert not result.exception299 assert result.output == "no subcommand, use default\nin subcommand\n"300301302@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)311def test_subcommand_metavar_marks_optional(312 runner, chain, invoke_without_command, metavar313):314 """The leading subcommand token is bracketed only when it is optional."""315316 @click.group(chain=chain, invoke_without_command=invoke_without_command)317 def cli():318 pass319320 @cli.command()321 def sub():322 pass323324 result = runner.invoke(cli, ["--help"])325 assert result.exit_code == 0326 assert result.output.splitlines()[0] == f"Usage: cli [OPTIONS] {metavar}"327328329def test_aliased_command_canonical_name(runner):330 class AliasedGroup(click.Group):331 def get_command(self, ctx, cmd_name):332 return push
318 unmodified lines
modified:CHANGES.rst+3 added
9 unmodified lines
10 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`171819Version 8.4.120-------------
1476 unmodified lines