aboutsummaryrefslogtreecommitdiff
path: root/flag.go
diff options
context:
space:
mode:
authormkishere <[email protected]>2018-03-28 23:43:57 +0800
committerEric Paris <[email protected]>2018-03-28 08:43:57 -0700
commitad68c28ee799163e627e77fcc6e8ecaa866e3535 (patch)
treedbe07a2cee11b82a9ed2f27dfb3b16a38810ef21 /flag.go
parent45e82a3a9c6b20fe3159b3dc2eb28d80a3b2ee67 (diff)
Add multiline wrapping support (#155)
* 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
Diffstat (limited to 'flag.go')
-rw-r--r--flag.go15
1 files changed, 9 insertions, 6 deletions
diff --git a/flag.go b/flag.go
index 28538c0..7f460b5 100644
--- a/flag.go
+++ b/flag.go
@@ -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:]
}
@@ -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
@@ -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
@@ -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