Skip to content

Commit 29bacc7

Browse files
authored
Fix deprecated label formatting (#3509)
2 parents 648d7c4 + 82f377c commit 29bacc7

4 files changed

Lines changed: 35 additions & 2 deletions

File tree

CHANGES.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Unreleased
99
tabs in option help text are now escaped, keeping the original completion
1010
format while still supporting multi-line help. :issue:`3502`
1111
:issue:`3043` :pr:`3504` :pr:`3508`
12+
- Deprecated commands and options with empty or missing help text no longer
13+
render a stray leading space before the ``(DEPRECATED)`` label. :pr:`3509`
1214

1315

1416
Version 8.4.1

src/click/core.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,7 +1189,8 @@ def format_help_text(self, ctx: Context, formatter: HelpFormatter) -> None:
11891189
text = ""
11901190

11911191
if self.deprecated:
1192-
text = f"{_(text)} {_format_deprecated_label(self.deprecated)}"
1192+
label = _format_deprecated_label(self.deprecated)
1193+
text = f"{_(text)} {label}" if text else label
11931194

11941195
if text:
11951196
formatter.write_paragraph()
@@ -2826,7 +2827,7 @@ def __init__(
28262827

28272828
if deprecated:
28282829
label = _format_deprecated_label(deprecated)
2829-
help = f"{help} {label}" if help is not None else label
2830+
help = f"{help} {label}" if help else label
28302831

28312832
self.prompt = prompt_text
28322833
self.confirmation_prompt = confirmation_prompt

tests/test_commands.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,22 @@ def cli():
491491
assert deprecated in result.output
492492

493493

494+
@pytest.mark.parametrize("deprecated", [True, "USE OTHER COMMAND INSTEAD"])
495+
@pytest.mark.parametrize("doc", ["", None])
496+
def test_deprecated_empty_help_no_leading_space(runner, doc, deprecated):
497+
"""A command with empty or missing help text must render the deprecation
498+
label at the normal indentation, without a stray leading space.
499+
"""
500+
501+
@click.command(deprecated=deprecated, help=doc)
502+
def cli():
503+
pass
504+
505+
out = runner.invoke(cli, ["--help"]).output
506+
assert "\n (DEPRECATED" in out
507+
assert "\n (DEPRECATED" not in out
508+
509+
494510
@pytest.mark.parametrize("deprecated", [True, "USE OTHER COMMAND INSTEAD"])
495511
def test_deprecated_in_invocation(runner, deprecated):
496512
@click.command(deprecated=deprecated)

tests/test_options.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,20 @@ def cmd(foo):
5858
assert deprecated in result.output
5959

6060

61+
@pytest.mark.parametrize(
62+
("deprecated", "expected"),
63+
[(True, "(DEPRECATED)"), ("USE B INSTEAD", "(DEPRECATED: USE B INSTEAD)")],
64+
)
65+
@pytest.mark.parametrize("help_text", ["", None])
66+
def test_deprecated_empty_help_no_leading_space(help_text, deprecated, expected):
67+
"""An option with empty or missing help text must not gain a stray leading
68+
space before the deprecation label.
69+
"""
70+
opt = click.Option(["--foo"], help=help_text, deprecated=deprecated)
71+
ctx = click.Context(click.Command("cli"))
72+
assert opt.get_help_record(ctx)[1] == expected
73+
74+
6175
@pytest.mark.parametrize("deprecated", [True, "USE B INSTEAD"])
6276
def test_deprecated_warning(runner, deprecated):
6377
@click.command()

0 commit comments

Comments
 (0)