Skip to content
Myr

Always list all positional arguments (#3860)

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

authored by Rowlando13 · committed by GitHub · parents 7068985, 05f6fd0 · Browse files · 5 files+124 added−13 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+124 added−13 removed

  • tests:+93 added−0 removed
  • other:+31 added−13 removed

5 files+124 added−13 removed

modified:docs/arguments.md+2 added-1 removed
modified:docs/documentation.md+4 added
116 unmodified lines
117the `Positional arguments` section of the help page. You can still document118arguments in the command docstring, especially when you want to describe them119in the main help text by name.120121The section shows up as soon as one argument of the command has a `help`. It122then lists every argument, so it always matches the usage line. Arguments with123no `help` are listed with an empty description.124125A brief example:126127```{eval-rst}128.. click:example::
219 unmodified lines
modified:src/click/core.py+20 added-12 removed
1308 unmodified lines
1309 with formatter.section(_("Options")):1310 formatter.write_dl(opts)13111312 def format_arguments(self, ctx: Context, formatter: HelpFormatter) -> None:1313 """Writes the arguments that have a help record into the formatter."""1314 args = []1315 for param in self.get_params(ctx):1316 rv = param.get_help_record(ctx)1317 if rv is not None and isinstance(param, Argument):1318 args.append(rv)1313 """Writes all arguments into the formatter, if at least one is documented.13141320 if args:1315 An argument with no help gets an empty description, the same way an option1316 with no help does. That keeps the section an exhaustive list of the1317 positional arguments, matching the usage line.1318 """1319 args = [param for param in self.get_params(ctx) if isinstance(param, Argument)]13201321 if any(arg.help is not None for arg in args):1322 with formatter.section(_("Positional arguments")):1322 formatter.write_dl(args)1323 formatter.write_dl([arg.get_help_record(ctx) for arg in args])13241325 def format_epilog(self, ctx: Context, formatter: HelpFormatter) -> None:1326 """Writes the epilog into the formatter if it exists."""1327 if self.epilog:
2468 unmodified lines
37963797 def get_usage_pieces(self, ctx: Context) -> list[str]:3798 return [self.make_metavar(ctx)]37993799 def get_help_record(self, ctx: Context) -> tuple[str, str] | None:3800 if self.help is None:3801 return None3800 def get_help_record(self, ctx: Context) -> tuple[str, str]:3801 """Returns the argument's help row: its metavar and its help text.38023803 return self.make_metavar(ctx), self.help3803 Unlike :meth:`Option.get_help_record`, this never returns ``None``. An3804 argument cannot be hidden, so an undocumented one still gets a row, with3805 an empty description.38063807 .. versionchanged:: 8.5.13808 Always returns a tuple. It used to return ``None`` when ``help`` was3809 not set.3810 """3811 return self.make_metavar(ctx), self.help or ""38123813 def get_error_hint(self, ctx: Context | None) -> str:3814 if ctx is not None:3815 return f"'{self.make_metavar(ctx)}'"3816 return f"'{self.human_readable_name}'"
27 unmodified lines
modified:tests/test_arguments.py+93 added
322 unmodified lines
323 assert "Options:" in result.output324 assert "number of greetings" in result.output325326327def test_argument_help_lists_undocumented_arguments(runner):328 """One documented argument makes the section list all of them."""329330 @click.command()331 @click.argument("src", help="Source path")332 @click.argument("dst")333 @click.argument("extra", nargs=-1)334 def cli(src, dst, extra):335 pass336337 result = runner.invoke(cli, ["--help"])338 assert result.exit_code == 0, result.output339 lines = result.output.splitlines()340 start = lines.index("Positional arguments:")341 assert lines[start : start + 4] == [342 "Positional arguments:",343 " SRC Source path",344 " DST",345 " [EXTRA]...",346 ]347348349def test_argument_help_undocumented_arguments_only_no_section(runner):350 """Arguments alone produce no section: at least one needs a help."""351352 @click.command()353 @click.argument("src")354 @click.argument("dst")355 def cli(src, dst):356 pass357358 result = runner.invoke(cli, ["--help"])359 assert result.exit_code == 0, result.output360 assert "Positional arguments:" not in result.output361362363def test_argument_help_deprecated_without_help_lists_all(runner):364 """A deprecation label counts as help and brings every argument into view."""365366 @click.command()367 @click.argument("src", required=False, deprecated=True)368 @click.argument("dst")369 def cli(src, dst):370 pass371372 result = runner.invoke(cli, ["--help"])373 assert result.exit_code == 0, result.output374 lines = result.output.splitlines()375 start = lines.index("Positional arguments:")376 assert lines[start : start + 3] == [377 "Positional arguments:",378 " [SRC!] (DEPRECATED)",379 " DST",380 ]381382383def test_argument_help_empty_string_lists_all(runner):384 """An explicit empty help opens the section, the same way an empty option help385 keeps its option listed.386 """387388 @click.command()389 @click.argument("src", help="")390 @click.argument("dst")391 def cli(src, dst):392 pass393394 result = runner.invoke(cli, ["--help"])395 assert result.exit_code == 0, result.output396 lines = result.output.splitlines()397 start = lines.index("Positional arguments:")398 assert lines[start : start + 3] == [399 "Positional arguments:",400 " SRC",401 " DST",402 ]403404405@pytest.mark.parametrize(406 ("help_text", "expected"),407 [408 pytest.param(None, ("SRC", ""), id="None"),409 pytest.param("", ("SRC", ""), id="empty"),410 pytest.param("Source path", ("SRC", "Source path"), id="documented"),411 ],412)413def test_argument_get_help_record_never_none(help_text, expected):414 """Every argument gets a row, even an undocumented one."""415 arg = click.Argument(["src"], help=help_text)416 ctx = click.Context(click.Command("cli"))417 assert arg.get_help_record(ctx) == expected418419420def test_argument_help_optional_metavar(runner):421 @click.command()422 @click.argument("name", required=False, default="", help="The name to print")423 def cli(name):
410 unmodified lines
modified:CHANGES.md+5 added
7 unmodified lines
8 exits with the intended code; only the message may be lost. {issue}`3802`9- Add {meth}`Option.get_help_spec`, which returns the option's left help10 column even when the option is hidden. {meth}`Option.get_help_record` still11 returns `None` for hidden options, so help screens are unchanged. {pr}`3821`12- The `Positional arguments` help section lists every argument of the command,13 not only the documented ones. An argument with no `help` gets an empty14 description, the same way an option with no `help` does. {pr}`3860`15- {meth}`Argument.get_help_record` always returns a tuple. It used to return16 `None` for an argument with no `help`. {pr}`3860`17- Document which types are inferred from `default`, and what an unrecognized18 `type` callable does to a command-line value. {issue}`3036` {pr}`3808`19- `path_type` narrows the converted value's type for `convert` and `prompt`.20 {issue}`3822` {pr}`3858`
1665 unmodified lines