Tim Cooper has uploaded this change for review.
flag: align multi-line usage strings
Previously, a multi-line flag usage string would not be indented with the
rest of the usage strings. This made long usage strings difficult to read.
For example, the usage for flag.String("A", "", "1\n2\n3") would be printed
as:
-A 1
2
3
But will now be printed as:
-A 1
2
3
Also fixes a slight error in the FlagSet.PrintDefaults documentation.
Fixes #20799
Change-Id: I4379c6b7590fdb93a2809a01046a0f6ae32c3e5d
---
M src/flag/flag.go
M src/flag/flag_test.go
2 files changed, 25 insertions(+), 12 deletions(-)
diff --git a/src/flag/flag.go b/src/flag/flag.go
index a1a8dc7..e8923c3 100644
--- a/src/flag/flag.go
+++ b/src/flag/flag.go
@@ -71,6 +71,7 @@
"reflect"
"sort"
"strconv"
+ "strings"
"time"
)
@@ -448,9 +449,9 @@
return
}
-// PrintDefaults prints to standard error the default values of all
-// defined command-line flags in the set. See the documentation for
-// the global function PrintDefaults for more information.
+// PrintDefaults prints, to standard error unless configured otherwise, the
+// default values of all defined command-line flags in the set. See the
+// documentation for the global function PrintDefaults for more information.
func (f *FlagSet) PrintDefaults() {
f.VisitAll(func(flag *Flag) {
s := fmt.Sprintf(" -%s", flag.Name) // Two spaces before -; see next two comments.
@@ -458,16 +459,20 @@
if len(name) > 0 {
s += " " + name
}
- // Boolean flags of one ASCII letter are so common we
- // treat them specially, putting their usage on the same line.
- if len(s) <= 4 { // space, space, '-', 'x'.
- s += "\t"
- } else {
- // Four spaces before the tab triggers good alignment
- // for both 4- and 8-space tab stops.
- s += "\n \t"
+ lines := strings.Split(usage, "\n")
+ for i, line := range lines {
+ // Boolean flags of one ASCII letter are so common we
+ // treat them specially, putting their usage on the same line.
+ if i == 0 && len(s) <= 4 { // space, space, '-', 'x'.
+ s += "\t"
+ } else {
+ // Four spaces before the tab triggers good alignment
+ // for both 4- and 8-space tab stops.
+ s += "\n \t"
+ }
+ s += line
}
- s += usage
+
if !isZeroValue(flag, flag.DefValue) {
if _, ok := flag.Value.(*stringValue); ok {
// put quotes on the value
diff --git a/src/flag/flag_test.go b/src/flag/flag_test.go
index 20d09c4..4c6db96 100644
--- a/src/flag/flag_test.go
+++ b/src/flag/flag_test.go
@@ -389,8 +389,14 @@
a non-zero number (default 2.7)
-G float
a float that defaults to zero
+ -M string
+ a multiline
+ help
+ string
-N int
a non-zero int (default 27)
+ -O a flag
+ multiline help string (default true)
-Z int
an int that defaults to zero
-maxT timeout
@@ -407,7 +413,9 @@
fs.String("D", "", "set relative `path` for local imports")
fs.Float64("F", 2.7, "a non-zero `number`")
fs.Float64("G", 0, "a float that defaults to zero")
+ fs.String("M", "", "a multiline\nhelp\nstring")
fs.Int("N", 27, "a non-zero int")
+ fs.Bool("O", true, "a flag\nmultiline help string")
fs.Int("Z", 0, "an int that defaults to zero")
fs.Duration("maxT", 0, "set `timeout` for dial")
fs.PrintDefaults()
To view, visit change 66711. To unsubscribe, or for help writing mail filters, visit settings.
Change looks good but I'm a little nervous about adding an import to a core package. Asking rsc to approve. It would be pretty easy to avoid the import but I don't know if it matters.
Patch set 1:Code-Review +1
Adding rsc@ to approve the import change.
Importing strings is certainly OK.
Patch set 1:Code-Review +1
1 comment:
Patch Set #1, Line 462: lines := strings.Split(usage, "\n")
This works, but I think it would be a bit clearer to restore the original version and make the 1-line change replacing
s += usage
with
s += strings.Replace(s, "\n", "\n \t", -1)
I had a hard time following the combination of the loop and the first-line logic.
To view, visit change 66711. To unsubscribe, or for help writing mail filters, visit settings.
Tim Cooper uploaded patch set #2 to this change.
flag: align multi-line usage strings
Previously, a multi-line flag usage string would not be indented with the
rest of the usage strings. This made long usage strings difficult to read.
For example, the usage for flag.String("A", "", "1\n2\n3") would be printed
as:
-A 1
2
3
But will now be printed as:
-A 1
2
3
Also fixes a slight error in the FlagSet.PrintDefaults documentation.
Fixes #20799
Change-Id: I4379c6b7590fdb93a2809a01046a0f6ae32c3e5d
---
M src/flag/flag.go
M src/flag/flag_test.go
2 files changed, 14 insertions(+), 4 deletions(-)
To view, visit change 66711. To unsubscribe, or for help writing mail filters, visit settings.
1 comment:
Patch Set #1, Line 462: // Boolean flags of one ASCII letter are so common we
This works, but I think it would be a bit clearer to restore the original version and make the 1-lin […]
Done
To view, visit change 66711. To unsubscribe, or for help writing mail filters, visit settings.
Looks great, thanks. If the trybots are happy I'll submit it.
Patch set 2:Run-TryBot +1Code-Review +1
TryBots beginning. Status page: https://farmer.golang.org/try?commit=559fdc14
TryBots are happy.
Patch set 2:TryBot-Result +1
Patch set 2:Code-Review +2
Russ Cox merged this change.
flag: align multi-line usage strings
Previously, a multi-line flag usage string would not be indented with the
rest of the usage strings. This made long usage strings difficult to read.
For example, the usage for flag.String("A", "", "1\n2\n3") would be printed
as:
-A 1
2
3
But will now be printed as:
-A 1
2
3
Also fixes a slight error in the FlagSet.PrintDefaults documentation.
Fixes #20799
Change-Id: I4379c6b7590fdb93a2809a01046a0f6ae32c3e5d
Reviewed-on: https://go-review.googlesource.com/66711
Run-TryBot: Russ Cox <r...@golang.org>
TryBot-Result: Gobot Gobot <go...@golang.org>
Reviewed-by: Russ Cox <r...@golang.org>
---
M src/flag/flag.go
M src/flag/flag_test.go
2 files changed, 14 insertions(+), 4 deletions(-)
diff --git a/src/flag/flag.go b/src/flag/flag.go
index a1a8dc7..5544a25 100644
--- a/src/flag/flag.go
+++ b/src/flag/flag.go
@@ -71,6 +71,7 @@
"reflect"
"sort"
"strconv"
+ "strings"
"time"
)
@@ -448,9 +449,9 @@
return
}
-// PrintDefaults prints to standard error the default values of all
-// defined command-line flags in the set. See the documentation for
-// the global function PrintDefaults for more information.
+// PrintDefaults prints, to standard error unless configured otherwise, the
+// default values of all defined command-line flags in the set. See the
+// documentation for the global function PrintDefaults for more information.
func (f *FlagSet) PrintDefaults() {
f.VisitAll(func(flag *Flag) {
s := fmt.Sprintf(" -%s", flag.Name) // Two spaces before -; see next two comments.
@@ -467,7 +468,8 @@
// for both 4- and 8-space tab stops.
s += "\n \t"
}
- s += usage
+ s += strings.Replace(usage, "\n", "\n \t", -1)