Skip to content

Commit 8a1ab4a

Browse files
committed
Add Option.get_help_spec to return the option's help left part
Follows up on: #2516, #2517
1 parent 00f257b commit 8a1ab4a

3 files changed

Lines changed: 42 additions & 4 deletions

File tree

CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ Unreleased
66
- A `KeyboardInterrupt` arriving while `Command.main()` reports an abort or an error,
77
or while it exits, no longer escapes as an unhandled traceback. The command still
88
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 help
10+
column even when the option is hidden. {meth}`Option.get_help_record` still
11+
returns `None` for hidden options, so help screens are unchanged. {pr}`3821`
912

1013
## Version 8.5.0
1114

src/click/core.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3363,10 +3363,15 @@ def add_to_parser(self, parser: _OptionParser, ctx: Context) -> None:
33633363
nargs=self.nargs,
33643364
)
33653365

3366-
def get_help_record(self, ctx: Context) -> tuple[str, str] | None:
3367-
if self.hidden:
3368-
return None
3366+
def get_help_spec(self, ctx: Context) -> str:
3367+
"""Returns the left column of the option's help record: its spellings
3368+
and metavar, like ``-v, --verbose`` or ``-c, --config TEXT``.
33693369
3370+
Unlike :meth:`get_help_record`, the spec is produced even when the
3371+
option is :attr:`hidden`.
3372+
3373+
.. versionadded:: 8.5.1
3374+
"""
33703375
any_prefix_is_slash = False
33713376

33723377
def _write_opts(opts: cabc.Sequence[str]) -> str:
@@ -3387,6 +3392,12 @@ def _write_opts(opts: cabc.Sequence[str]) -> str:
33873392
if self.secondary_opts:
33883393
rv.append(_write_opts(self.secondary_opts))
33893394

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 None
3400+
33903401
help = self.help or ""
33913402

33923403
extra = self.get_help_extra(ctx)
@@ -3406,7 +3417,7 @@ def _write_opts(opts: cabc.Sequence[str]) -> str:
34063417
extra_str = "; ".join(extra_items)
34073418
help = f"{help} [{extra_str}]" if help else f"[{extra_str}]"
34083419

3409-
return ("; " if any_prefix_is_slash else " / ").join(rv), help
3420+
return self.get_help_spec(ctx), help
34103421

34113422
def get_help_extra(self, ctx: Context) -> types.OptionHelpExtra:
34123423
extra: types.OptionHelpExtra = {}

tests/test_options.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,30 @@ def test_deprecated_empty_help_no_leading_space(help_text, deprecated, expected)
7272
assert opt.get_help_record(ctx)[1] == expected
7373

7474

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()``, unlike
87+
``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) == expected
92+
93+
if hidden:
94+
assert opt.get_help_record(ctx) is None
95+
else:
96+
assert opt.get_help_record(ctx)[0] == expected
97+
98+
7599
@pytest.mark.parametrize("deprecated", [True, "USE B INSTEAD"])
76100
def test_deprecated_warning(runner, deprecated):
77101
@click.command()

0 commit comments

Comments
 (0)