Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't output spurious lines if the truncated help text is actually empty #2369

Merged
merged 1 commit into from
Jun 30, 2023
Merged
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
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Unreleased
- Improve type annotations for pyright type checker. :issue:`2268`
- Improve responsiveness of ``click.clear()``. :issue:`2284`
- Improve command name detection when using Shiv or PEX. :issue:`2332`
- Avoid showing empty lines if command help text is empty. :issue:`2368`


Version 8.1.3
Expand Down
7 changes: 5 additions & 2 deletions src/click/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1360,13 +1360,16 @@ def format_help(self, ctx: Context, formatter: HelpFormatter) -> None:

def format_help_text(self, ctx: Context, formatter: HelpFormatter) -> None:
"""Writes the help text to the formatter if it exists."""
text = self.help if self.help is not None else ""
if self.help is not None:
# truncate the help text to the first form feed
text = inspect.cleandoc(self.help).partition("\f")[0]
else:
text = ""

if self.deprecated:
text = _("(Deprecated) {text}").format(text=text)

if text:
text = inspect.cleandoc(text).partition("\f")[0]
formatter.write_paragraph()

with formatter.indentation():
Expand Down
14 changes: 0 additions & 14 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,20 +95,6 @@ def long():
)


def test_help_truncation(runner):
@click.command()
def cli():
"""This is a command with truncated help.
\f

This text should be truncated.
"""

result = runner.invoke(cli, ["--help"])
assert result.exit_code == 0
assert "This is a command with truncated help." in result.output


def test_no_args_is_help(runner):
@click.command(no_args_is_help=True)
def cli():
Expand Down
20 changes: 20 additions & 0 deletions tests/test_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,26 @@ def cli(ctx):
]


def test_truncating_docstring_no_help(runner):
@click.command()
@click.pass_context
def cli(ctx):
"""
\f

This text should be truncated.
"""

result = runner.invoke(cli, ["--help"], terminal_width=60)
assert not result.exception
assert result.output.splitlines() == [
"Usage: cli [OPTIONS]",
"",
"Options:",
" --help Show this message and exit.",
]


def test_removing_multiline_marker(runner):
@click.group()
def cli():
Expand Down