Skip to content
Myr

Preserve declaration order of help option names

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

authored by Kevin Deldycke · committed by Kevin Deldycke · parent 00e592c · Browse files · 3 files+63 added−4 removed
This commit

State

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

Trailers

  • Co-authored-by: Jochen Sprickerhof <git@jochen.sprickerhof.de>

Diff stats

3 files+63 added−4 removed

  • tests:+50 added−0 removed
  • other:+13 added−4 removed

3 files+63 added−4 removed

modified:src/click/core.py+11 added-4 removed
1173 unmodified lines
11741175 return rv11761177 def get_help_option_names(self, ctx: Context) -> list[str]:1178 """Returns the names for the help option."""1179 all_names = set(ctx.help_option_names)1178 """Returns the names for the help option.11791180 Drops duplicates and names already reserved by another parameter. Order of1181 :attr:`Context.help_option_names` is preserved, so the result is stable.11821183 .. versionchanged:: 8.5.01184 Names keep their declaration order.1185 """1186 all_names = dict.fromkeys(ctx.help_option_names)1187 for param in self.params:1181 all_names.difference_update(param.opts)1182 all_names.difference_update(param.secondary_opts)1188 for name in (*param.opts, *param.secondary_opts):1189 all_names.pop(name, None)1190 return list(all_names)11911192 def get_help_option(self, ctx: Context) -> Option | None:1193 """Returns the help option object.
2606 unmodified lines
modified:tests/test_commands.py+50 added
417 unmodified lines
418 == expected_params419 )420421422@pytest.mark.parametrize(423 ("help_option_names", "params", "expected"),424 [425 # A single default name is returned unchanged.426 (["--help"], [], ["--help"]),427 # Declaration order is preserved verbatim, whatever it is.428 (["-h", "--help"], [], ["-h", "--help"]),429 (["--help", "-h"], [], ["--help", "-h"]),430 (["--help", "-h", "-?"], [], ["--help", "-h", "-?"]),431 (["-?", "--help", "-h"], [], ["-?", "--help", "-h"]),432 # Duplicate names collapse to their first occurrence, keeping order.433 (["--help", "--help"], [], ["--help"]),434 (["-h", "--help", "-h"], [], ["-h", "--help"]),435 # A name already claimed by another parameter's option is dropped.436 (["-h", "--help"], [["--help"]], ["-h"]),437 (["-h", "--help"], [["-h", "--verbose"]], ["--help"]),438 # Both options of a feature-switch flag are removed too.439 (["--shout", "--help"], [["--shout/--no-shout"]], ["--help"]),440 (["--no-shout", "--help"], [["--shout/--no-shout"]], ["--help"]),441 # Filtering every name out yields an empty list.442 (["-h", "--help"], [["-h"], ["--help"]], []),443 # Deduplication and conflict removal combine, order still preserved.444 (["-h", "--help", "-h", "--assist"], [["--assist"]], ["-h", "--help"]),445 ],446 ids=[447 "default",448 "order-short-long",449 "order-long-short",450 "order-three",451 "order-three-shuffled",452 "dedupe-adjacent",453 "dedupe-spread",454 "conflict-long",455 "conflict-short",456 "conflict-flag-on",457 "conflict-flag-off",458 "all-removed",459 "dedupe-and-conflict",460 ],461)462def test_get_help_option_names(help_option_names, params, expected):463 """Check help option names are deduplicated but keep stable order.464465 https://github.com/pallets/click/pull/3728466 """467 cli = click.Command("cli", params=[click.Option(decls) for decls in params])468 ctx = click.Context(cli, help_option_names=help_option_names)469 assert cli.get_help_option_names(ctx) == expected470471472def test_help_param_priority(runner):473 """Cover the edge-case in which the eagerness of help option was not474 respected, because it was internally generated multiple times.475
225 unmodified lines
modified:CHANGES.md+2 added
61 unmodified lines
62 `type`, `value_proc`, or a matching `default` instead of `Any`.63 {class}`ParamType` takes a second optional type parameter describing the64 input value it accepts (`ParamType[int, str]` for a type converting65 strings to integers), defaulting to `Any`. {pr}`3407`66- {meth}`Command.get_help_option_names` returns the help option names in the67 order they were declared. {pr}`3728`6869## Version 8.4.27071Released 2026-06-24
1561 unmodified lines