Skip to content
Myr

Store raw help string for commands

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Some tools, such as sphinx-click [1], may wish to access the full help string for a command including any text after the form feed character (which indicates truncation [2]). Make this possible by storing the help string without truncation and instead truncate when we use this string (i.e. truncate on load, not on store). [1] https://github.com/click-contrib/sphinx-click/issues/56 [2] https://click.palletsprojects.com/en/latest/documentation/#truncating-help-texts

authored by Stephen Finucane · committed by David Lord · parent 4262661 · Browse files · 3 files+27 added−7 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Signed-off-by: Stephen Finucane <stephen@that.guru>

Diff stats

3 files+27 added−7 removed

  • tests:+14 added−0 removed
  • other:+13 added−7 removed

3 files+27 added−7 removed

modified:src/click/core.py+11 added-7 removed
1154 unmodified lines
1155 :param deprecated: issues a message indicating that1156 the command is deprecated.11571158 .. versionchanged:: 8.11159 ``help`` is stored untruncated, the formatter applied1160 truncation later.11611162 .. versionchanged:: 8.11163 Indentation in ``help`` is cleaned here instead of only in the1164 ``@command`` decorator. ``epilog`` and ``short_help`` are also1165 cleaned.1166
33 unmodified lines
12001201 if help:1202 help = inspect.cleandoc(help)12031200 # Discard help text after a first form feed (page break)1201 # character, to allow writing longer documentation in1202 # docstrings that does not show in help output.1203 if "\f" in help:1204 help = help.partition("\f")[0]12051204 if epilog:1205 epilog = inspect.cleandoc(epilog)12061207 if short_help:
11 unmodified lines
1219 def to_info_dict(self, ctx: Context) -> t.Dict[str, t.Any]:1220 info_dict = super().to_info_dict(ctx)1221 info_dict.update(1222 params=[param.to_info_dict() for param in self.get_params(ctx)],1225 help=self.help,1223 # truncate help text to the content preceding the first form feed,1224 # if any1225 help=self.help.split("\f", 1)[0] if self.help else self.help,1226 epilog=self.epilog,1227 short_help=self.short_help,1228 hidden=self.hidden,1229 deprecated=self.deprecated,
116 unmodified lines
1346 def format_help_text(self, ctx: Context, formatter: HelpFormatter) -> None:1347 """Writes the help text to the formatter if it exists."""1348 text = self.help or ""13491350 # truncate the help text to the content preceding the first form feed,1351 # if any1352 text = text.split("\f", 1)[0]13531354 if self.deprecated:1355 text = _("(Deprecated) {text}").format(text=text)13561357 if text:
1621 unmodified lines
modified:tests/test_commands.py+14 added
93 unmodified lines
94 is not None95 )969798def test_help_truncation(runner):99 @click.command()100 def cli():101 """This is a command with truncated help.102 \f103104 This text should be truncated.105 """106107 result = runner.invoke(cli, ["--help"])108 assert result.exit_code == 0109 assert "This is a command with truncated help." in result.output110111112def test_no_args_is_help(runner):113 @click.command(no_args_is_help=True)114 def cli():115 pass
239 unmodified lines
modified:CHANGES.rst+2 added
31 unmodified lines
32 decorators does. A command's ``epilog`` and ``short_help`` are also33 processed. :issue:`1985`34- A flag option with ``required=True`` requires that the flag is35 passed instead of choosing the implicit default value. :issue:`1978`36- Store untruncated help string in ``Command.help``, and perform37 truncation when formatting. :issue:`2149`383940Version 8.0.441-------------
948 unmodified lines