Use formatter width and max command name width to autotrim short help before display.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Oleksandr Shulgin · committed by Zachery Bir · parent 895bb96 · Browse files · 2 files·+8 added−27 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+8 added−27 removed
- tests:+2 added−8 removed
- other:+6 added−19 removed
Provenance
- Commit
37233e2- Tree
ee1fc77- Author
Oleksandr Shulgin <oleksandr.shulgin@zalando.de>- Committer
Zachery Bir <zbir@zacbir.net>- Parent
895bb96
2 files changed+8 added−27 removed
modified:click/core.py+6 added-19 removed
779 unmodified lines780 self.params = params or []781 self.help = help782 self.epilog = epilog783 self.options_metavar = options_metavar784- if short_help is None and help:785- if (context_settings is not None and786- 'short_help_width' in context_settings):787- short_width = context_settings.get('short_help_width')788- short_help = make_default_short_help(help, short_width)789- else:790- short_help = make_default_short_help(help)784 self.short_help = short_help785 self.add_help_option = add_help_option786 self.hidden = hidden787 self.deprecated = deprecated235 unmodified lines1023 def format_commands(self, ctx, formatter):1024 """Extra format methods for multi methods that adds all the commands1025 after the options.1026 """1034- rows = []1035- for subcommand in self.list_commands(ctx):1036- cmd = self.get_command(ctx, subcommand)1037- # What is this, the tool lied about a command. Ignore it1038- if cmd is None:1039- continue1040- if cmd.hidden:1041- continue1027+ commands = filter(lambda cmd: cmd[0] is not None or cmd[1].hidden,1028+ [(c, self.get_command(ctx, c)) for c in self.list_commands(ctx)])1029 1043- help = cmd.short_help or ''1044- if cmd.deprecated:1045- help += DEPRECATED_HELP_NOTICE1046- rows.append((subcommand, help))1030+ limit = formatter.width - 6 - max(len(cmd[0]) for cmd in commands)1031 1032+ rows = [(cmd[0], cmd[1].short_help or cmd[1].help and make_default_short_help(cmd[1].help, limit) or '')1033+ for cmd in commands]1034+1035 if rows:1036 with formatter.section('Commands'):1037 formatter.write_dl(rows)1038 776 unmodified linesmodified:tests/test_commands.py+2 added-8 removed
57 unmodified lines58 def long():59 """This is a long text that is too long to show as short help60 and will be truncated instead."""61 62- @cli.command(context_settings={'short_help_width': 20})63- def width():64- """This is a long text that is too long to show as short help65- and will be truncated instead."""66-62 result = runner.invoke(cli, ['--help'])63 assert re.search(64 r'Commands:\n\s+'70- r'long\s+This is a long text that is too long to show\.\.\.\n\s+'65+ r'long\s+This is a long text that is too long to show as short help\.\.\.\n\s+'66 r'short\s+This is a short text\.\n\s+'72- r'special-chars\s+Login and store the token in ~/.netrc\.\s+'73- r'width\s+This is a long text\.\.\.\n\s*',67+ r'special-chars\s+Login and store the token in ~/.netrc\.\s*',68 result.output) is not None69 70 71 def test_default_maps(runner):212 unmodified lines