[go] flag: align multi-line usage strings

132 views
Skip to first unread message

Tim Cooper (Gerrit)

unread,
Sep 27, 2017, 7:31:54 PM9/27/17
to Ian Lance Taylor, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Tim Cooper has uploaded this change for review.

View 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, 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.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: I4379c6b7590fdb93a2809a01046a0f6ae32c3e5d
Gerrit-Change-Number: 66711
Gerrit-PatchSet: 1
Gerrit-Owner: Tim Cooper <tim.c...@layeh.com>

Rob Pike (Gerrit)

unread,
Sep 28, 2017, 9:59:39 AM9/28/17
to Tim Cooper, goph...@pubsubhelper.golang.org, Rob Pike, golang-co...@googlegroups.com

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

View Change

    To view, visit change 66711. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-MessageType: comment
    Gerrit-Change-Id: I4379c6b7590fdb93a2809a01046a0f6ae32c3e5d
    Gerrit-Change-Number: 66711
    Gerrit-PatchSet: 1
    Gerrit-Owner: Tim Cooper <tim.c...@layeh.com>
    Gerrit-Reviewer: Rob Pike <r...@golang.org>
    Gerrit-Comment-Date: Thu, 28 Sep 2017 13:59:31 +0000
    Gerrit-HasComments: No
    Gerrit-HasLabels: Yes

    Rob Pike (Gerrit)

    unread,
    Sep 28, 2017, 10:00:01 AM9/28/17
    to Tim Cooper, goph...@pubsubhelper.golang.org, Russ Cox, Rob Pike, golang-co...@googlegroups.com

    Adding rsc@ to approve the import change.

    View Change

      To view, visit change 66711. To unsubscribe, or for help writing mail filters, visit settings.

      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-MessageType: comment
      Gerrit-Change-Id: I4379c6b7590fdb93a2809a01046a0f6ae32c3e5d
      Gerrit-Change-Number: 66711
      Gerrit-PatchSet: 1
      Gerrit-Owner: Tim Cooper <tim.c...@layeh.com>
      Gerrit-Reviewer: Rob Pike <r...@golang.org>
      Gerrit-Reviewer: Russ Cox <r...@golang.org>
      Gerrit-Comment-Date: Thu, 28 Sep 2017 13:59:55 +0000
      Gerrit-HasComments: No
      Gerrit-HasLabels: No

      Russ Cox (Gerrit)

      unread,
      Sep 28, 2017, 10:22:55 AM9/28/17
      to Tim Cooper, goph...@pubsubhelper.golang.org, Russ Cox, Rob Pike, golang-co...@googlegroups.com

      Importing strings is certainly OK.

      Patch set 1:Code-Review +1

      View Change

      1 comment:

      • File src/flag/flag.go:

        • 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.

      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-MessageType: comment
      Gerrit-Change-Id: I4379c6b7590fdb93a2809a01046a0f6ae32c3e5d
      Gerrit-Change-Number: 66711
      Gerrit-PatchSet: 1
      Gerrit-Owner: Tim Cooper <tim.c...@layeh.com>
      Gerrit-Reviewer: Rob Pike <r...@golang.org>
      Gerrit-Reviewer: Russ Cox <r...@golang.org>
      Gerrit-Comment-Date: Thu, 28 Sep 2017 14:22:51 +0000
      Gerrit-HasComments: Yes
      Gerrit-HasLabels: Yes

      Tim Cooper (Gerrit)

      unread,
      Sep 28, 2017, 3:52:16 PM9/28/17
      to Russ Cox, Rob Pike, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

      Tim Cooper uploaded patch set #2 to this change.

      View 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.

      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-MessageType: newpatchset
      Gerrit-Change-Id: I4379c6b7590fdb93a2809a01046a0f6ae32c3e5d
      Gerrit-Change-Number: 66711
      Gerrit-PatchSet: 2

      Tim Cooper (Gerrit)

      unread,
      Sep 28, 2017, 3:54:00 PM9/28/17
      to goph...@pubsubhelper.golang.org, Russ Cox, Rob Pike, golang-co...@googlegroups.com

      View Change

      1 comment:

      To view, visit change 66711. To unsubscribe, or for help writing mail filters, visit settings.

      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-MessageType: comment
      Gerrit-Change-Id: I4379c6b7590fdb93a2809a01046a0f6ae32c3e5d
      Gerrit-Change-Number: 66711
      Gerrit-PatchSet: 2
      Gerrit-Owner: Tim Cooper <tim.c...@layeh.com>
      Gerrit-Reviewer: Rob Pike <r...@golang.org>
      Gerrit-Reviewer: Russ Cox <r...@golang.org>
      Gerrit-Reviewer: Tim Cooper <tim.c...@layeh.com>
      Gerrit-Comment-Date: Thu, 28 Sep 2017 19:53:58 +0000
      Gerrit-HasComments: Yes
      Gerrit-HasLabels: No

      Russ Cox (Gerrit)

      unread,
      Sep 28, 2017, 4:31:15 PM9/28/17
      to Tim Cooper, goph...@pubsubhelper.golang.org, Russ Cox, Rob Pike, golang-co...@googlegroups.com

      Looks great, thanks. If the trybots are happy I'll submit it.

      Patch set 2:Run-TryBot +1Code-Review +1

      View Change

        To view, visit change 66711. To unsubscribe, or for help writing mail filters, visit settings.

        Gerrit-Project: go
        Gerrit-Branch: master
        Gerrit-MessageType: comment
        Gerrit-Change-Id: I4379c6b7590fdb93a2809a01046a0f6ae32c3e5d
        Gerrit-Change-Number: 66711
        Gerrit-PatchSet: 2
        Gerrit-Owner: Tim Cooper <tim.c...@layeh.com>
        Gerrit-Reviewer: Rob Pike <r...@golang.org>
        Gerrit-Reviewer: Russ Cox <r...@golang.org>
        Gerrit-Reviewer: Tim Cooper <tim.c...@layeh.com>
        Gerrit-Comment-Date: Thu, 28 Sep 2017 20:31:12 +0000
        Gerrit-HasComments: No
        Gerrit-HasLabels: Yes

        Gobot Gobot (Gerrit)

        unread,
        Sep 28, 2017, 4:31:29 PM9/28/17
        to Tim Cooper, goph...@pubsubhelper.golang.org, Russ Cox, Rob Pike, golang-co...@googlegroups.com

        TryBots beginning. Status page: https://farmer.golang.org/try?commit=559fdc14

        View Change

          To view, visit change 66711. To unsubscribe, or for help writing mail filters, visit settings.

          Gerrit-Project: go
          Gerrit-Branch: master
          Gerrit-MessageType: comment
          Gerrit-Change-Id: I4379c6b7590fdb93a2809a01046a0f6ae32c3e5d
          Gerrit-Change-Number: 66711
          Gerrit-PatchSet: 2
          Gerrit-Owner: Tim Cooper <tim.c...@layeh.com>
          Gerrit-Reviewer: Rob Pike <r...@golang.org>
          Gerrit-Reviewer: Russ Cox <r...@golang.org>
          Gerrit-Reviewer: Tim Cooper <tim.c...@layeh.com>
          Gerrit-CC: Gobot Gobot <go...@golang.org>
          Gerrit-Comment-Date: Thu, 28 Sep 2017 20:31:24 +0000
          Gerrit-HasComments: No
          Gerrit-HasLabels: No

          Gobot Gobot (Gerrit)

          unread,
          Sep 28, 2017, 4:44:34 PM9/28/17
          to Tim Cooper, goph...@pubsubhelper.golang.org, Russ Cox, Rob Pike, golang-co...@googlegroups.com

          TryBots are happy.

          Patch set 2:TryBot-Result +1

          View Change

            To view, visit change 66711. To unsubscribe, or for help writing mail filters, visit settings.

            Gerrit-Project: go
            Gerrit-Branch: master
            Gerrit-MessageType: comment
            Gerrit-Change-Id: I4379c6b7590fdb93a2809a01046a0f6ae32c3e5d
            Gerrit-Change-Number: 66711
            Gerrit-PatchSet: 2
            Gerrit-Owner: Tim Cooper <tim.c...@layeh.com>
            Gerrit-Reviewer: Gobot Gobot <go...@golang.org>
            Gerrit-Reviewer: Rob Pike <r...@golang.org>
            Gerrit-Reviewer: Russ Cox <r...@golang.org>
            Gerrit-Reviewer: Tim Cooper <tim.c...@layeh.com>
            Gerrit-Comment-Date: Thu, 28 Sep 2017 20:42:37 +0000
            Gerrit-HasComments: No
            Gerrit-HasLabels: Yes

            Russ Cox (Gerrit)

            unread,
            Sep 28, 2017, 4:45:34 PM9/28/17
            to Tim Cooper, goph...@pubsubhelper.golang.org, Russ Cox, Gobot Gobot, Rob Pike, golang-co...@googlegroups.com

            Patch set 2:Code-Review +2

            View Change

              To view, visit change 66711. To unsubscribe, or for help writing mail filters, visit settings.

              Gerrit-Project: go
              Gerrit-Branch: master
              Gerrit-MessageType: comment
              Gerrit-Change-Id: I4379c6b7590fdb93a2809a01046a0f6ae32c3e5d
              Gerrit-Change-Number: 66711
              Gerrit-PatchSet: 2
              Gerrit-Owner: Tim Cooper <tim.c...@layeh.com>
              Gerrit-Reviewer: Gobot Gobot <go...@golang.org>
              Gerrit-Reviewer: Rob Pike <r...@golang.org>
              Gerrit-Reviewer: Russ Cox <r...@golang.org>
              Gerrit-Reviewer: Tim Cooper <tim.c...@layeh.com>
              Gerrit-Comment-Date: Thu, 28 Sep 2017 20:45:29 +0000
              Gerrit-HasComments: No
              Gerrit-HasLabels: Yes

              Russ Cox (Gerrit)

              unread,
              Sep 28, 2017, 4:45:36 PM9/28/17
              to Russ Cox, Tim Cooper, goph...@pubsubhelper.golang.org, golang-...@googlegroups.com, Gobot Gobot, Rob Pike, golang-co...@googlegroups.com

              Russ Cox merged this change.

              View Change

              Approvals: Russ Cox: Looks good to me, approved; Run TryBots Gobot Gobot: TryBots succeeded
              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)
              Gerrit-MessageType: merged
              Gerrit-Change-Id: I4379c6b7590fdb93a2809a01046a0f6ae32c3e5d
              Gerrit-Change-Number: 66711
              Gerrit-PatchSet: 3
              Reply all
              Reply to author
              Forward
              0 new messages