diff options
| -rw-r--r-- | example_test.go | 24 | ||||
| -rw-r--r-- | flag.go | 1 |
2 files changed, 17 insertions, 8 deletions
diff --git a/example_test.go b/example_test.go index 9be7a49..507f1ec 100644 --- a/example_test.go +++ b/example_test.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// These examples demonstrate more intricate uses of the flag package. +// These examples demonstrate more intricate uses of the pflag package. package pflag_test import ( @@ -11,14 +11,14 @@ import ( "strings" "time" - flag "github.com/spf13/pflag" + "github.com/spf13/pflag" ) // Example 1: A single string flag called "species" with default value "gopher". -var species = flag.String("species", "gopher", "the species we are studying") +var species = pflag.String("species", "gopher", "the species we are studying") // Example 2: A flag with a shorthand letter. -var gopherType = flag.StringP("gopher_type", "g", "pocket", "the variety of gopher") +var gopherType = pflag.StringP("gopher_type", "g", "pocket", "the variety of gopher") // Example 3: A user-defined flag type, a slice of durations. type interval []time.Duration @@ -64,14 +64,24 @@ var intervalFlag interval func init() { // Tie the command-line flag to the intervalFlag variable and // set a usage message. - flag.Var(&intervalFlag, "deltaT", "comma-separated list of intervals to use between events") + pflag.Var(&intervalFlag, "deltaT", "comma-separated list of intervals to use between events") } func Example() { // All the interesting pieces are with the variables declared above, but - // to enable the flag package to see the flags defined there, one must + // to enable the pflag package to see the flags defined there, one must // execute, typically at the start of main (not init!): - // flag.Parse() + // pflag.Parse() // We don't run it here because this is not a main function and // the testing suite has already parsed the flags. } + +func ExampleShorthandLookup() { + fs := pflag.NewFlagSet("Example", pflag.ContinueOnError) + fs.BoolP("verbose", "v", false, "verbose output") + + name := "verbose" + flag := fs.ShorthandLookup(name[:1]) + + fmt.Println(flag.Name) +} @@ -786,7 +786,6 @@ func (f *FlagSet) VarP(value Value, name, shorthand, usage string) { // AddFlag will add the flag to the FlagSet func (f *FlagSet) AddFlag(flag *Flag) { - // Call normalizeFlagName function only once normalizedFlagName := f.normalizeFlagName(flag.Name) _, alreadyThere := f.formal[normalizedFlagName] |
