Skip to content

Commit

Permalink
Merge pull request #1008 from zacbir/short-help
Browse files Browse the repository at this point in the history
Recreate #446
  • Loading branch information
zacbir authored May 15, 2018
2 parents 895bb96 + 7927546 commit ea213fc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 23 deletions.
29 changes: 14 additions & 15 deletions click/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -781,13 +781,6 @@ def __init__(self, name, context_settings=None, callback=None,
self.help = help
self.epilog = epilog
self.options_metavar = options_metavar
if short_help is None and help:
if (context_settings is not None and
'short_help_width' in context_settings):
short_width = context_settings.get('short_help_width')
short_help = make_default_short_help(help, short_width)
else:
short_help = make_default_short_help(help)
self.short_help = short_help
self.add_help_option = add_help_option
self.hidden = hidden
Expand Down Expand Up @@ -1031,7 +1024,7 @@ def format_commands(self, ctx, formatter):
"""Extra format methods for multi methods that adds all the commands
after the options.
"""
rows = []
commands = []
for subcommand in self.list_commands(ctx):
cmd = self.get_command(ctx, subcommand)
# What is this, the tool lied about a command. Ignore it
Expand All @@ -1040,14 +1033,20 @@ def format_commands(self, ctx, formatter):
if cmd.hidden:
continue

help = cmd.short_help or ''
if cmd.deprecated:
help += DEPRECATED_HELP_NOTICE
rows.append((subcommand, help))
commands.append((subcommand, cmd))

# allow for 3 times the default spacing
if len(commands):
limit = formatter.width - 6 - max(len(cmd[0]) for cmd in commands)

rows = []
for subcommand, cmd in commands:
help = cmd.short_help or cmd.help and make_default_short_help(cmd.help, limit)
rows.append((subcommand, help or ''))

if rows:
with formatter.section('Commands'):
formatter.write_dl(rows)
if rows:
with formatter.section('Commands'):
formatter.write_dl(rows)

def parse_args(self, ctx, args):
if not args and self.no_args_is_help and not ctx.resilient_parsing:
Expand Down
10 changes: 2 additions & 8 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,12 @@ def long():
"""This is a long text that is too long to show as short help
and will be truncated instead."""

@cli.command(context_settings={'short_help_width': 20})
def width():
"""This is a long text that is too long to show as short help
and will be truncated instead."""

result = runner.invoke(cli, ['--help'])
assert re.search(
r'Commands:\n\s+'
r'long\s+This is a long text that is too long to show\.\.\.\n\s+'
r'long\s+This is a long text that is too long to show as short help\.\.\.\n\s+'
r'short\s+This is a short text\.\n\s+'
r'special-chars\s+Login and store the token in ~/.netrc\.\s+'
r'width\s+This is a long text\.\.\.\n\s*',
r'special-chars\s+Login and store the token in ~/.netrc\.\s*',
result.output) is not None


Expand Down

0 comments on commit ea213fc

Please sign in to comment.