From 92b30e47cb013433190f40d75239c98c77aa9d2d Mon Sep 17 00:00:00 2001 From: David Lord Date: Mon, 21 Feb 2022 10:02:12 -0800 Subject: defer processing Command.help, epilog, and short_help --- CHANGES.rst | 9 +++++---- src/click/core.py | 43 ++++++++++++++----------------------------- 2 files changed, 19 insertions(+), 33 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index ebf14e6..0bfa1d5 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -27,14 +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 untruncated help string in ``Command.help``, and perform - truncation when formatting. :issue:`2149` +- 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 95a4761..9ba738b 100644 --- a/src/click/core.py +++ b/src/click/core.py @@ -1156,13 +1156,9 @@ class Command(BaseCommand): the command is deprecated. .. versionchanged:: 8.1 - ``help`` is stored untruncated, the formatter applied - truncation later. - - .. 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. @@ -1197,16 +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) - - 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 @@ -1220,9 +1206,7 @@ class Command(BaseCommand): info_dict = super().to_info_dict(ctx) info_dict.update( params=[param.to_info_dict() for param in self.get_params(ctx)], - # truncate help text to the content preceding the first form feed, - # if any - help=self.help.split("\f", 1)[0] if self.help else self.help, + help=self.help, epilog=self.epilog, short_help=self.short_help, hidden=self.hidden, @@ -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,16 +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 "" - - # truncate the help text to the content preceding the first form feed, - # if any - text = text.split("\f", 1)[0] + 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(): @@ -1375,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: -- cgit v1.2.1