]> Cypherpunks repositories - gostls13.git/commitdiff
flag: document the zero value of FlagSet
authorKyle Lemons <kyle@kylelemons.net>
Tue, 6 Aug 2013 06:48:19 +0000 (16:48 +1000)
committerRob Pike <r@golang.org>
Tue, 6 Aug 2013 06:48:19 +0000 (16:48 +1000)
R=golang-dev, r
CC=golang-dev
https://golang.org/cl/12403043

src/pkg/flag/flag.go

index bde055d3bd4a3d98f5de6da4870873330694b230..18841e3a91cbf7cfa7b4b165e366a62db73e9c04 100644 (file)
@@ -256,7 +256,8 @@ const (
        PanicOnError
 )
 
-// A FlagSet represents a set of defined flags.
+// A FlagSet represents a set of defined flags.  The zero value of a FlagSet
+// has no name and has ContinueOnError error handling.
 type FlagSet struct {
        // Usage is the function called when an error occurs while parsing flags.
        // The field is a function (not a method) that may be changed to point to
@@ -391,7 +392,11 @@ func PrintDefaults() {
 
 // defaultUsage is the default function to print a usage message.
 func defaultUsage(f *FlagSet) {
-       fmt.Fprintf(f.out(), "Usage of %s:\n", f.name)
+       if f.name == "" {
+               fmt.Fprintf(f.out(), "Usage:\n")
+       } else {
+               fmt.Fprintf(f.out(), "Usage of %s:\n", f.name)
+       }
        f.PrintDefaults()
 }
 
@@ -658,7 +663,12 @@ func (f *FlagSet) Var(value Value, name string, usage string) {
        flag := &Flag{name, usage, value, value.String()}
        _, alreadythere := f.formal[name]
        if alreadythere {
-               msg := fmt.Sprintf("%s flag redefined: %s", f.name, name)
+               var msg string
+               if f.name == "" {
+                       msg = fmt.Sprintf("flag redefined: %s", name)
+               } else {
+                       msg = fmt.Sprintf("%s flag redefined: %s", f.name, name)
+               }
                fmt.Fprintln(f.out(), msg)
                panic(msg) // Happens only if flags are declared with identical names
        }