Skip to content
Myr

Auto truncate short help instead of hiding it. Fixes #53

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

authored by Armin Ronacher · committed by Armin Ronacher · parent 45be1ca · Browse files · 3 files+49 added−6 removed
This commit

State

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

Diff stats

3 files+49 added−6 removed

  • tests:+22 added−0 removed
  • other:+27 added−6 removed

3 files+49 added−6 removed

modified:click/core.py+2 added-6 removed
2 unmodified lines
3import codecs4from itertools import chain56from .types import convert_type, BOOL7from .utils import make_str7from .utils import make_str, make_default_short_help8from .exceptions import UsageError, Abort9from .helpers import prompt, confirm, echo10from .formatting import HelpFormatter11
233 unmodified lines
245 self.help = help246 self.epilog = epilog247 self.options_metavar = options_metavar248 if short_help is None and help:249 short_help = help.split('.')[0].strip()250 if len(short_help) > 45:251 short_help = None252 elif short_help:253 short_help = short_help + '.'249 short_help = make_default_short_help(help)250 self.short_help = short_help251 if add_help_option:252 help_option()(self)253
760 unmodified lines
modified:click/utils.py+25 added
36 unmodified lines
37 return value.decode(sys.getfilesystemencoding())38 except UnicodeError:39 return value.decode('utf-8', 'replace')40 return text_type(value)414243def make_default_short_help(help, max_length=45):44 words = help.split()45 total_length = 046 result = []47 done = False4849 for word in words:50 if '.' in word:51 word = word.split('.', 1)[0] + '.'52 done = True53 new_length = result and 1 + len(word) or len(word)54 if total_length + new_length > max_length:55 result.append('...')56 done = True57 else:58 if result:59 result.append(' ')60 result.append(word)61 if done:62 break63 total_length += new_length6465 return ''.join(result)
modified:tests/test_commands.py+22 added
1# -*- coding: utf-8 -*-2import re3import click456def test_other_command_invoke(runner):
9 unmodified lines
1617 result = runner.invoke(cli, [])18 assert not result.exception19 assert result.output == '42\n'202122def test_auto_shorthelp(runner):23 @click.group()24 def cli():25 pass2627 @cli.command()28 def short():29 """This is a short text."""3031 @cli.command()32 def long():33 """This is a long text that is too long to show as short help34 and will be truncated instead."""3536 result = runner.invoke(cli, ['--help'])37 assert re.search(38 r'Commands:\n\s+'39 r'long\s+This is a long text that is too long to show\.\.\.\n\s+'40 r'short\s+This is a short text\.', result.output) is not None