Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Unreleased
tabs in option help text are now escaped, keeping the original completion
format while still supporting multi-line help. :issue:`3502`
:issue:`3043` :pr:`3504` :pr:`3508`
- Deprecated commands and options with empty or missing help text no longer
render a stray leading space before the ``(DEPRECATED)`` label. :pr:`3509`


Version 8.4.1
Expand Down
5 changes: 3 additions & 2 deletions src/click/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1189,7 +1189,8 @@ def format_help_text(self, ctx: Context, formatter: HelpFormatter) -> None:
text = ""

if self.deprecated:
text = f"{_(text)} {_format_deprecated_label(self.deprecated)}"
label = _format_deprecated_label(self.deprecated)
text = f"{_(text)} {label}" if text else label

if text:
formatter.write_paragraph()
Expand Down Expand Up @@ -2826,7 +2827,7 @@ def __init__(

if deprecated:
label = _format_deprecated_label(deprecated)
help = f"{help} {label}" if help is not None else label
help = f"{help} {label}" if help else label

self.prompt = prompt_text
self.confirmation_prompt = confirmation_prompt
Expand Down
16 changes: 16 additions & 0 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,22 @@ def cli():
assert deprecated in result.output


@pytest.mark.parametrize("deprecated", [True, "USE OTHER COMMAND INSTEAD"])
@pytest.mark.parametrize("doc", ["", None])
def test_deprecated_empty_help_no_leading_space(runner, doc, deprecated):
"""A command with empty or missing help text must render the deprecation
label at the normal indentation, without a stray leading space.
"""

@click.command(deprecated=deprecated, help=doc)
def cli():
pass

out = runner.invoke(cli, ["--help"]).output
assert "\n (DEPRECATED" in out
assert "\n (DEPRECATED" not in out


@pytest.mark.parametrize("deprecated", [True, "USE OTHER COMMAND INSTEAD"])
def test_deprecated_in_invocation(runner, deprecated):
@click.command(deprecated=deprecated)
Expand Down
14 changes: 14 additions & 0 deletions tests/test_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,20 @@ def cmd(foo):
assert deprecated in result.output


@pytest.mark.parametrize(
("deprecated", "expected"),
[(True, "(DEPRECATED)"), ("USE B INSTEAD", "(DEPRECATED: USE B INSTEAD)")],
)
@pytest.mark.parametrize("help_text", ["", None])
def test_deprecated_empty_help_no_leading_space(help_text, deprecated, expected):
"""An option with empty or missing help text must not gain a stray leading
space before the deprecation label.
"""
opt = click.Option(["--foo"], help=help_text, deprecated=deprecated)
ctx = click.Context(click.Command("cli"))
assert opt.get_help_record(ctx)[1] == expected


@pytest.mark.parametrize("deprecated", [True, "USE B INSTEAD"])
def test_deprecated_warning(runner, deprecated):
@click.command()
Expand Down
Loading