Skip to content

Bug: make_default_short_help can return strings as long as max_length + 3 #1849

@janluke

Description

@janluke

The bug

The current algorithm doesn't take into account the length of "..." properly:

from click.utils import make_default_short_help

out = make_default_short_help('1234 67890 xxxxxx', max_length=10)
print(out)
print('Length:', len(out))

Prints:

1234 67890...
Length: 13

Expected output: 1234...

Environment

  • Click version: 7.2 and master

Solution

I'm going to open a PR.

Related issue (or: why this bug has never been reported)

The reason this bug has not been reported is because another "issue" compensates for it, making sure it never manifests itself in the terminal. The following code is from Group.format_commands:

click/src/click/core.py

Lines 1501 to 1508 in 0dc6d74

# 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.get_short_help_str(limit)
rows.append((subcommand, help))

Group.format_commands needs to know col_spacing to calculate the width of the 2nd column (i.e. the short help limit). But it:

  • doesn't set col_spacing in formatter.write_dl and,
  • it assumes that the formatter won't necessarily use the default col_spacing value (note that the only way to change it is to subclass HelpFormatter and override write_dl; not a very clean use of inheritance).

As a consequence, it makes the conservative choice of "allowing for 3 times the default col_spacing" subtracting 6 to limit (i.e. max_length). Because col_spacing will always be 2, the limit will always be 4 characters smaller than needed, compensating for the eventual excess of 3 characters due to the bug reported in this issue.

Let me know if I misunderstood something.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions