Skip to content
Myr

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

3 files+42 added−4 removed

modified:src/click/core.py+15 added-4 removed
3361 unmodified lines
3362 action=action,3363 nargs=self.nargs,3364 )33653366 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``.33693370 Unlike :meth:`get_help_record`, the spec is produced even when the3371 option is :attr:`hidden`.33723373 .. versionadded:: 8.5.13374 """3375 any_prefix_is_slash = False33763377 def _write_opts(opts: cabc.Sequence[str]) -> str:3378 nonlocal any_prefix_is_slash
12 unmodified lines
33913392 if self.secondary_opts:3393 rv.append(_write_opts(self.secondary_opts))33943395 return ("; " if any_prefix_is_slash else " / ").join(rv)33963397 def get_help_record(self, ctx: Context) -> tuple[str, str] | None:3398 if self.hidden:3399 return None34003401 help = self.help or ""34023403 extra = self.get_help_extra(ctx)3404 extra_items = []
11 unmodified lines
3416 if extra_items:3417 extra_str = "; ".join(extra_items)3418 help = f"{help} [{extra_str}]" if help else f"[{extra_str}]"34193409 return ("; " if any_prefix_is_slash else " / ").join(rv), help3420 return self.get_help_spec(ctx), help34213422 def get_help_extra(self, ctx: Context) -> types.OptionHelpExtra:3423 extra: types.OptionHelpExtra = {}3424
414 unmodified lines
modified:tests/test_options.py+24 added
70 unmodified lines
71 ctx = click.Context(click.Command("cli"))72 assert opt.get_help_record(ctx)[1] == expected737475@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])85def 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) == expected9293 if hidden:94 assert opt.get_help_record(ctx) is None95 else:96 assert opt.get_help_record(ctx)[0] == expected979899@pytest.mark.parametrize("deprecated", [True, "USE B INSTEAD"])100def test_deprecated_warning(runner, deprecated):101 @click.command()102 @click.option(
3553 unmodified lines
modified:CHANGES.md+3 added
4 unmodified lines
5- 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`1415## Version 8.5.0
1657 unmodified lines