From 321ede78e3fc432ff2f2ad5fb4babc4b45d82ed9 Mon Sep 17 00:00:00 2001 From: Kyle Lemons Date: Tue, 6 Aug 2013 16:48:19 +1000 Subject: [PATCH] flag: document the zero value of FlagSet R=golang-dev, r CC=golang-dev https://golang.org/cl/12403043 --- src/pkg/flag/flag.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/pkg/flag/flag.go b/src/pkg/flag/flag.go index bde055d3bd..18841e3a91 100644 --- a/src/pkg/flag/flag.go +++ b/src/pkg/flag/flag.go @@ -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 } -- 2.48.1