Skip to content

Commit

Permalink
Add multiline wrapping support (spf13#155)
Browse files Browse the repository at this point in the history
* Add multiline wrapping support

With reference to golang/go#20799, pflag now will respect newline in usage string and do wrap accordingly. Also add test cases for testing

* Break at \n only if \n pos<wrap
  • Loading branch information
victpork authored and eparis committed Mar 28, 2018
1 parent 45e82a3 commit ad68c28
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 6 deletions.
15 changes: 9 additions & 6 deletions flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,11 +586,14 @@ func wrapN(i, slop int, s string) (string, string) {
return s, ""
}

w := strings.LastIndexAny(s[:i], " \t")
w := strings.LastIndexAny(s[:i], " \t\n")
if w <= 0 {
return s, ""
}

nlPos := strings.LastIndex(s[:i], "\n")
if nlPos > 0 && nlPos < w {
return s[:nlPos], s[nlPos+1:]
}
return s[:w], s[w+1:]
}

Expand All @@ -599,7 +602,7 @@ func wrapN(i, slop int, s string) (string, string) {
// caller). Pass `w` == 0 to do no wrapping
func wrap(i, w int, s string) string {
if w == 0 {
return s
return strings.Replace(s, "\n", "\n"+strings.Repeat(" ", i), -1)
}

// space between indent i and end of line width w into which
Expand All @@ -617,7 +620,7 @@ func wrap(i, w int, s string) string {
}
// If still not enough space then don't even try to wrap.
if wrap < 24 {
return s
return strings.Replace(s, "\n", r, -1)
}

// Try to avoid short orphan words on the final line, by
Expand All @@ -629,14 +632,14 @@ func wrap(i, w int, s string) string {
// Handle first line, which is indented by the caller (or the
// special case above)
l, s = wrapN(wrap, slop, s)
r = r + l
r = r + strings.Replace(l, "\n", "\n"+strings.Repeat(" ", i), -1)

// Now wrap the rest
for s != "" {
var t string

t, s = wrapN(wrap, slop, s)
r = r + "\n" + strings.Repeat(" ", i) + t
r = r + "\n" + strings.Repeat(" ", i) + strings.Replace(t, "\n", "\n"+strings.Repeat(" ", i), -1)
}

return r
Expand Down
74 changes: 74 additions & 0 deletions printusage_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package pflag

import (
"bytes"
"io"
"testing"
)

const expectedOutput = ` --long-form Some description
--long-form2 Some description
with multiline
-s, --long-name Some description
-t, --long-name2 Some description with
multiline
`

func setUpPFlagSet(buf io.Writer) *FlagSet {
f := NewFlagSet("test", ExitOnError)
f.Bool("long-form", false, "Some description")
f.Bool("long-form2", false, "Some description\n with multiline")
f.BoolP("long-name", "s", false, "Some description")
f.BoolP("long-name2", "t", false, "Some description with\n multiline")
f.SetOutput(buf)
return f
}

func TestPrintUsage(t *testing.T) {
buf := bytes.Buffer{}
f := setUpPFlagSet(&buf)
f.PrintDefaults()
res := buf.String()
if res != expectedOutput {
t.Errorf("Expected \n%s \nActual \n%s", expectedOutput, res)
}
}

func setUpPFlagSet2(buf io.Writer) *FlagSet {
f := NewFlagSet("test", ExitOnError)
f.Bool("long-form", false, "Some description")
f.Bool("long-form2", false, "Some description\n with multiline")
f.BoolP("long-name", "s", false, "Some description")
f.BoolP("long-name2", "t", false, "Some description with\n multiline")
f.StringP("some-very-long-arg", "l", "test", "Some very long description having break the limit")
f.StringP("other-very-long-arg", "o", "long-default-value", "Some very long description having break the limit")
f.String("some-very-long-arg2", "very long default value", "Some very long description\nwith line break\nmultiple")
f.SetOutput(buf)
return f
}

const expectedOutput2 = ` --long-form Some description
--long-form2 Some description
with multiline
-s, --long-name Some description
-t, --long-name2 Some description with
multiline
-o, --other-very-long-arg string Some very long description having
break the limit (default
"long-default-value")
-l, --some-very-long-arg string Some very long description having
break the limit (default "test")
--some-very-long-arg2 string Some very long description
with line break
multiple (default "very long default
value")
`

func TestPrintUsage_2(t *testing.T) {
buf := bytes.Buffer{}
f := setUpPFlagSet2(&buf)
res := f.FlagUsagesWrapped(80)
if res != expectedOutput2 {
t.Errorf("Expected \n%q \nActual \n%q", expectedOutput2, res)
}
}

0 comments on commit ad68c28

Please sign in to comment.