Add `Option.get_help_spec` to return the option's help left part
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Follows up on: #2516, #2517
authored by Kevin Deldycke · committed by Kevin Deldycke · parent be13b15 · Browse files · 3 files·+42 added−4 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+42 added−4 removed
- tests:+24 added−0 removed
- other:+18 added−4 removed
Provenance
- Commit
271effb- Tree
59d15bb- Author
Kevin Deldycke <kevin@deldycke.com>- Committer
Kevin Deldycke <kevin@deldycke.com>- Parent
be13b15
3 files changed+42 added−4 removed
modified:src/click/core.py+15 added-4 removed
3361 unmodified lines3362 action=action,3363 nargs=self.nargs,3364 )3365 3366- def get_help_record(self, ctx: Context) -> tuple[str, str] | None:3367- if self.hidden:3368- return None3366+ def get_help_spec(self, ctx: Context) -> str:3367+ """Returns the left column of the option's help record: its spellings3368+ and metavar, like ``-v, --verbose`` or ``-c, --config TEXT``.3369 3370+ Unlike :meth:`get_help_record`, the spec is produced even when the3371+ option is :attr:`hidden`.3372+3373+ .. versionadded:: 8.5.13374+ """3375 any_prefix_is_slash = False3376 3377 def _write_opts(opts: cabc.Sequence[str]) -> str:3378 nonlocal any_prefix_is_slash12 unmodified lines3391 3392 if self.secondary_opts:3393 rv.append(_write_opts(self.secondary_opts))3394 3395+ return ("; " if any_prefix_is_slash else " / ").join(rv)3396+3397+ def get_help_record(self, ctx: Context) -> tuple[str, str] | None:3398+ if self.hidden:3399+ return None3400+3401 help = self.help or ""3402 3403 extra = self.get_help_extra(ctx)3404 extra_items = []11 unmodified lines3416 if extra_items:3417 extra_str = "; ".join(extra_items)3418 help = f"{help} [{extra_str}]" if help else f"[{extra_str}]"3419 3409- return ("; " if any_prefix_is_slash else " / ").join(rv), help3420+ return self.get_help_spec(ctx), help3421 3422 def get_help_extra(self, ctx: Context) -> types.OptionHelpExtra:3423 extra: types.OptionHelpExtra = {}3424 414 unmodified linesmodified:tests/test_options.py+24 added
70 unmodified lines71 ctx = click.Context(click.Command("cli"))72 assert opt.get_help_record(ctx)[1] == expected73 74 75+@pytest.mark.parametrize(76+ ("param_decls", "kwargs", "expected"),77+ [78+ (["-v", "--verbose"], {"is_flag": True}, "-v, --verbose"),79+ (["--config"], {}, "--config TEXT"),80+ (["--color/--no-color"], {}, "--color / --no-color"),81+ (["/debug;/no-debug"], {}, "/debug; /no-debug"),82+ ],83+)84+@pytest.mark.parametrize("hidden", [False, True])85+def test_help_spec(param_decls, kwargs, hidden, expected):86+ """A hidden option still produces its spec via ``get_help_spec()``, unlike87+ ``get_help_record()``.88+ """89+ opt = click.Option(param_decls, hidden=hidden, **kwargs)90+ ctx = click.Context(click.Command("cli"))91+ assert opt.get_help_spec(ctx) == expected92+93+ if hidden:94+ assert opt.get_help_record(ctx) is None95+ else:96+ assert opt.get_help_record(ctx)[0] == expected97+98+99 @pytest.mark.parametrize("deprecated", [True, "USE B INSTEAD"])100 def test_deprecated_warning(runner, deprecated):101 @click.command()102 @click.option(3553 unmodified linesmodified:CHANGES.md+3 added
4 unmodified lines5 - Fix `copy.deepcopy()` and `pickle` on a `Parameter`, `Option` or `Command`. {pr}`3805`6 - A `KeyboardInterrupt` arriving while `Command.main()` reports an abort or an error,7 or while it exits, no longer escapes as an unhandled traceback. The command still8 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 - Document which types are inferred from `default`, and what an unrecognized13 `type` callable does to a command-line value. {issue}`3036` {pr}`3808`14 15 ## Version 8.5.01657 unmodified lines