Commit 0828ec1e authored by Tim Cooper's avatar Tim Cooper Committed by Russ Cox

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 <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarRuss Cox <rsc@golang.org>
parent ded2c65d
...@@ -71,6 +71,7 @@ import ( ...@@ -71,6 +71,7 @@ import (
"reflect" "reflect"
"sort" "sort"
"strconv" "strconv"
"strings"
"time" "time"
) )
...@@ -448,9 +449,9 @@ func UnquoteUsage(flag *Flag) (name string, usage string) { ...@@ -448,9 +449,9 @@ func UnquoteUsage(flag *Flag) (name string, usage string) {
return return
} }
// PrintDefaults prints to standard error the default values of all // PrintDefaults prints, to standard error unless configured otherwise, the
// defined command-line flags in the set. See the documentation for // default values of all defined command-line flags in the set. See the
// the global function PrintDefaults for more information. // documentation for the global function PrintDefaults for more information.
func (f *FlagSet) PrintDefaults() { func (f *FlagSet) PrintDefaults() {
f.VisitAll(func(flag *Flag) { f.VisitAll(func(flag *Flag) {
s := fmt.Sprintf(" -%s", flag.Name) // Two spaces before -; see next two comments. s := fmt.Sprintf(" -%s", flag.Name) // Two spaces before -; see next two comments.
...@@ -467,7 +468,8 @@ func (f *FlagSet) PrintDefaults() { ...@@ -467,7 +468,8 @@ func (f *FlagSet) PrintDefaults() {
// for both 4- and 8-space tab stops. // for both 4- and 8-space tab stops.
s += "\n \t" s += "\n \t"
} }
s += usage s += strings.Replace(usage, "\n", "\n \t", -1)
if !isZeroValue(flag, flag.DefValue) { if !isZeroValue(flag, flag.DefValue) {
if _, ok := flag.Value.(*stringValue); ok { if _, ok := flag.Value.(*stringValue); ok {
// put quotes on the value // put quotes on the value
......
...@@ -389,8 +389,14 @@ const defaultOutput = ` -A for bootstrapping, allow 'any' type ...@@ -389,8 +389,14 @@ const defaultOutput = ` -A for bootstrapping, allow 'any' type
a non-zero number (default 2.7) a non-zero number (default 2.7)
-G float -G float
a float that defaults to zero a float that defaults to zero
-M string
a multiline
help
string
-N int -N int
a non-zero int (default 27) a non-zero int (default 27)
-O a flag
multiline help string (default true)
-Z int -Z int
an int that defaults to zero an int that defaults to zero
-maxT timeout -maxT timeout
...@@ -407,7 +413,9 @@ func TestPrintDefaults(t *testing.T) { ...@@ -407,7 +413,9 @@ func TestPrintDefaults(t *testing.T) {
fs.String("D", "", "set relative `path` for local imports") fs.String("D", "", "set relative `path` for local imports")
fs.Float64("F", 2.7, "a non-zero `number`") fs.Float64("F", 2.7, "a non-zero `number`")
fs.Float64("G", 0, "a float that defaults to zero") 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.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.Int("Z", 0, "an int that defaults to zero")
fs.Duration("maxT", 0, "set `timeout` for dial") fs.Duration("maxT", 0, "set `timeout` for dial")
fs.PrintDefaults() fs.PrintDefaults()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment