Skip to content
Myr

Fix short help abbreviation

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

authored by Kevin Deldycke · committed by Kevin Deldycke · parent 6d5bac5 · Browse files · 3 files+49 added−34 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−34 removed

  • tests:+26 added−2 removed
  • other:+23 added−32 removed

3 files+49 added−34 removed

modified:src/click/utils.py+20 added-32 removed
62 unmodified lines
63 """Returns a condensed version of help string.6465 :meta private:66 """67 # Consider only the first paragraph.68 paragraph_end = help.find("\n\n")67 # Consider only the first paragraph and collapse newlines, tabs and spaces.68 words = help.partition("\n\n")[0].split()6970 if paragraph_end != -1:71 help = help[:paragraph_end]70 # The first paragraph started with a "no rewrap" marker, ignore it.71 if words and words[0] == "\b":72 words = words[1:]7373 # Collapse newlines, tabs, and spaces.74 words = help.split()7574 if not words:75 return ""7679 # The first paragraph started with a "no rewrap" marker, ignore it.80 if words[0] == "\b":81 words = words[1:]8283 total_length = 077 last_index = len(words) - 17879 # A period ends a sentence when it closes the text, or when the next word80 # does not start in lowercase. A lowercase word continues the sentence, so81 # the period belongs to an abbreviation such as "vs.".82 for i, word in enumerate(words):87 total_length += len(word) + (i > 0)8889 if total_length > max_length: # too long, truncate83 if word.endswith(".") and (i == last_index or not words[i + 1][0].islower()):84 words = words[: i + 1]85 break8692 if word[-1] == ".": # sentence end, truncate without "..."93 return " ".join(words[: i + 1])87 text = " ".join(words)8895 if total_length == max_length and i != last_index:96 break # not at sentence end, truncate with "..."97 else:98 return " ".join(words) # no truncation needed89 if len(text) <= max_length:90 return text91100 # Account for the length of the suffix.101 total_length += len("...")92 # The suffix alone does not fit, and shorten() rejects such a width.93 if max_length < len("..."):94 return "..."95103 # remove words until the length is short enough104 while i > 0:105 total_length -= len(words[i]) + (i > 0)96 # Imported late to keep the import footprint small.97 import textwrap98107 if total_length <= max_length:108 break99 # Do not split hyphenated words.100 return textwrap.shorten(text, max_length, placeholder="...", break_on_hyphens=False)101110 i -= 1102112 return " ".join(words[:i]) + "..."113114103class _LazyFile:104 """A lazy file works like a regular file but it does not fully open105 the file but it does perform some basic checks early to see if the106 filename parameter does make sense. This is useful for safely opening
570 unmodified lines
modified:tests/test_utils/test_make_default_short_help.py+26 added-2 removed
6 unmodified lines
7 ("value", "max_length", "expect"),8 [9 pytest.param("", 10, "", id="empty"),10 pytest.param("123 567 90", 10, "123 567 90", id="equal length, no dot"),11 pytest.param("123 567 9. aaaa bbb", 10, "123 567 9.", id="sentence < max"),11 pytest.param("123 567 9. Aaaa bbb", 10, "123 567 9.", id="sentence < max"),12 pytest.param("123 567 9.", 10, "123 567 9.", id="dot ends text"),13 pytest.param("123 567\n\n 9. aaaa bbb", 10, "123 567", id="paragraph < max"),14 pytest.param("123 567 90123.", 10, "123 567...", id="truncate"),15 pytest.param("123 5678 xxxxxx", 10, "123...", id="length includes suffix"),16 pytest.param(
1 unmodified line
18 20,19 "token in ~/.netrc...",20 id="ignore dot in word",21 ),22 pytest.param(23 "Weigh apples vs. pears.",24 30,25 "Weigh apples vs. pears.",26 id="abbreviation inside sentence",27 ),28 pytest.param(29 "Weigh apples vs. pears and plums.",30 20,31 "Weigh apples vs....",32 id="abbreviation inside truncated sentence",33 ),34 pytest.param("123 567 9. aaaa bbb", 10, "123 567...", id="lowercase after dot"),35 pytest.param(36 "Pick a fruit. 3 remain.", 20, "Pick a fruit.", id="digit after dot"37 ),38 pytest.param("well-behaved.", 8, "...", id="never split a hyphenated word"),39 pytest.param("supercalifragilistic", 10, "...", id="never split a long word"),40 pytest.param("ab cd", 3, "...", id="width fits the suffix only"),41 pytest.param("ab cd", 2, "...", id="width below the suffix"),42 pytest.param("ab", 2, "ab", id="text shorter than the suffix"),43 pytest.param("ab cd", -1, "...", id="negative width"),44 ],45)46@pytest.mark.parametrize(47 "alter",
4 unmodified lines
52 ),53 ],54)55def test_make_default_short_help(value, max_length, alter, expect):33 assert len(expect) <= max_length56 # A width too narrow for the suffix still returns the whole suffix.57 assert len(expect) <= max(max_length, len("..."))5859 if alter:60 value = alter(value)61
2 unmodified lines
modified:CHANGES.md+3 added
14 unmodified lines
15 {issue}`3822` {pr}`3858`16- {class}`Parameter` accepts a new `help` argument and {class}`Option` and {class}`Argument`17 inherits it. {meth}`Parameter.to_info_dict` now reports `help` for every kind of parameter.18 {pr}`3861`19- A command's short help no longer stops at an abbreviation such as `vs.` or `e.g.`20 inside the first sentence. A period ends the sentence only when it closes the text21 or the next word does not start in lowercase. {pr}`3865`2223## Version 8.5.02425Released 2026-08-24
1655 unmodified lines