Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
janluke opened this issue Apr 14, 2021 · 0 comments · Fixed by #1850
Closed

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

janluke opened this issue Apr 14, 2021 · 0 comments · Fixed by #1850

Comments

@janluke
Copy link
Contributor

janluke commented Apr 14, 2021

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.

davidism added a commit that referenced this issue Apr 14, 2021
Fix and refactor make_default_short_help (issue #1849)
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 29, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant