summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Lord <davidism@gmail.com>2022-02-21 10:04:40 -0800
committerGitHub <noreply@github.com>2022-02-21 10:04:40 -0800
commitc01e2b8ed17d3ea65981bc62784363d0e38e3092 (patch)
treec4c10a7f1c3a08e6e41c2d9ee21a26769cc65372
parent4262661a0fffabe3803f1bd876b19244f587dafa (diff)
parent92b30e47cb013433190f40d75239c98c77aa9d2d (diff)
downloadclick-c01e2b8ed17d3ea65981bc62784363d0e38e3092.tar.gz
Merge pull request #2151 from stephenfin/store-untruncated-help
Store raw help string for commands
-rw-r--r--CHANGES.rst7
-rw-r--r--src/click/core.py37
-rw-r--r--tests/test_commands.py14
3 files changed, 32 insertions, 26 deletions
diff --git a/CHANGES.rst b/CHANGES.rst
index 35307d4..0bfa1d5 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -27,12 +27,15 @@ Version 8.1.0
- Parameter decorators and ``@group`` handles ``cls=None`` the same as
not passing ``cls``. ``@option`` handles ``help=None`` the same as
not passing ``help``. :issue:`#1959`
+- A flag option with ``required=True`` requires that the flag is
+ passed instead of choosing the implicit default value. :issue:`1978`
- Indentation in help text passed to ``Option`` and ``Command`` is
cleaned the same as using the ``@option`` and ``@command``
decorators does. A command's ``epilog`` and ``short_help`` are also
processed. :issue:`1985`
-- A flag option with ``required=True`` requires that the flag is
- passed instead of choosing the implicit default value. :issue:`1978`
+- Store unprocessed ``Command.help``, ``epilog`` and ``short_help``
+ strings. Processing is only done when formatting help text for
+ output. :issue:`2149`
Version 8.0.4
diff --git a/src/click/core.py b/src/click/core.py
index 1ab2b20..9ba738b 100644
--- a/src/click/core.py
+++ b/src/click/core.py
@@ -1156,9 +1156,9 @@ class Command(BaseCommand):
the command is deprecated.
.. versionchanged:: 8.1
- Indentation in ``help`` is cleaned here instead of only in the
- ``@command`` decorator. ``epilog`` and ``short_help`` are also
- cleaned.
+ ``help``, ``epilog``, and ``short_help`` are stored unprocessed,
+ all formatting is done when outputting help text, not at init,
+ and is done even if not using the ``@command`` decorator.
.. versionchanged:: 8.0
Added a ``repr`` showing the command name.
@@ -1193,22 +1193,6 @@ class Command(BaseCommand):
#: should show up in the help page and execute. Eager parameters
#: will automatically be handled before non eager ones.
self.params: t.List["Parameter"] = params or []
-
- if help:
- help = inspect.cleandoc(help)
-
- # Discard help text after a first form feed (page break)
- # character, to allow writing longer documentation in
- # docstrings that does not show in help output.
- if "\f" in help:
- help = help.partition("\f")[0]
-
- if epilog:
- epilog = inspect.cleandoc(epilog)
-
- if short_help:
- short_help = inspect.cleandoc(short_help)
-
self.help = help
self.epilog = epilog
self.options_metavar = options_metavar
@@ -1316,10 +1300,12 @@ class Command(BaseCommand):
"""Gets short help for the command or makes it by shortening the
long help string.
"""
- text = self.short_help or ""
-
- if not text and self.help:
+ if self.short_help:
+ text = inspect.cleandoc(self.short_help)
+ elif self.help:
text = make_default_short_help(self.help, limit)
+ else:
+ text = ""
if self.deprecated:
text = _("(Deprecated) {text}").format(text=text)
@@ -1345,12 +1331,13 @@ class Command(BaseCommand):
def format_help_text(self, ctx: Context, formatter: HelpFormatter) -> None:
"""Writes the help text to the formatter if it exists."""
- text = self.help or ""
+ text = self.help if self.help is not None else ""
if self.deprecated:
text = _("(Deprecated) {text}").format(text=text)
if text:
+ text = inspect.cleandoc(text).partition("\f")[0]
formatter.write_paragraph()
with formatter.indentation():
@@ -1371,9 +1358,11 @@ class Command(BaseCommand):
def format_epilog(self, ctx: Context, formatter: HelpFormatter) -> None:
"""Writes the epilog into the formatter if it exists."""
if self.epilog:
+ epilog = inspect.cleandoc(self.epilog)
formatter.write_paragraph()
+
with formatter.indentation():
- formatter.write_text(self.epilog)
+ formatter.write_text(epilog)
def parse_args(self, ctx: Context, args: t.List[str]) -> t.List[str]:
if not args and self.no_args_is_help and not ctx.resilient_parsing:
diff --git a/tests/test_commands.py b/tests/test_commands.py
index fa2773d..bf6a5db 100644
--- a/tests/test_commands.py
+++ b/tests/test_commands.py
@@ -95,6 +95,20 @@ def test_auto_shorthelp(runner):
)
+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():