Skip to content
Myr

List every argument in the `Positional arguments` help section

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

Cloup already renders the section this way: https://github.com/janluke/cloup/issues/210

authored by Kevin Deldycke · committed by Kevin Deldycke · parent 86949d1 · Browse files · 5 files+99 added−9 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+99 added−9 removed

  • tests:+78 added−0 removed
  • other:+21 added−9 removed

5 files+99 added−9 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+12 added-8 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 records = [1323 arg.get_help_record(ctx) or (arg.make_metavar(ctx), "") for arg in args1324 ]1325 with formatter.section(_("Positional arguments")):1322 formatter.write_dl(args)1326 formatter.write_dl(records)13271328 def format_epilog(self, ctx: Context, formatter: HelpFormatter) -> None:1329 """Writes the epilog into the formatter if it exists."""1330 if self.epilog:
2512 unmodified lines
modified:tests/test_arguments.py+78 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 ]403404405def test_argument_help_optional_metavar(runner):406 @click.command()407 @click.argument("name", required=False, default="", help="The name to print")408 def cli(name):
410 unmodified lines
modified:CHANGES.md+3 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- Document which types are inferred from `default`, and what an unrecognized16 `type` callable does to a command-line value. {issue}`3036` {pr}`3808`17- `path_type` narrows the converted value's type for `convert` and `prompt`.18 {issue}`3822` {pr}`3858`
1659 unmodified lines