Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -696,15 +696,23 @@ func UnquoteUsage(flag *Flag) (name string, usage string) {
// Splits the string `s` on whitespace into an initial substring up to
// `i` runes in length and the remainder. Will go `slop` over `i` if
// that encompasses the entire string (which allows the caller to
// avoid short orphan words on the final line).
// avoid short orphan words on the final line). If the next word is
// wider than `i`, it is returned on its own and wrapping continues
// after it.
func wrapN(i, slop int, s string) (string, string) {
if i+slop > len(s) {
return s, ""
}

w := strings.LastIndexAny(s[:i], " \t\n")
if w <= 0 {
return s, ""
// No break in the first i bytes. Emit the over-wide word
// alone and keep wrapping whatever follows.
next := strings.IndexAny(s, " \t\n")
if next <= 0 {
return s, ""
}
return s[:next], s[next+1:]
}
nlPos := strings.LastIndex(s[:i], "\n")
if nlPos > 0 && nlPos < w {
Expand Down
16 changes: 16 additions & 0 deletions printusage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,19 @@ func TestPrintUsage_2(t *testing.T) {
t.Errorf("Expected \n%s \nActual \n%s", expectedOutput2, res)
}
}

// A word wider than the description column must sit on its own line.
// Wrapping has to resume for the words after it (#501).
func TestFlagUsagesWrappedContinuesAfterUnbreakableWord(t *testing.T) {
f := NewFlagSet("example", ContinueOnError)
f.String("mount", "", "a mount specification e.g. 'type=bind,source=/opt,destination=/hostopt'. The rest of this description is never wrapped.")
got := f.FlagUsagesWrapped(60)
want := ` --mount string a mount specification e.g.
'type=bind,source=/opt,destination=/hostopt'.
The rest of this description is
never wrapped.
`
if got != want {
t.Errorf("FlagUsagesWrapped(60)\nwant:\n%q\ngot:\n%q", want, got)
}
}
Loading