diff options
| author | Albert Nigmatzianov <[email protected]> | 2017-05-05 07:49:55 +0200 |
|---|---|---|
| committer | Albert Nigmatzianov <[email protected]> | 2017-05-05 07:49:55 +0200 |
| commit | 86425cb5c94235b7fcd9e788f53a0fa84130b470 (patch) | |
| tree | 991e54e750bcbf9721207cb8c4d0e4cbbec47add /example_test.go | |
| parent | 10b28b334f9fd331a1c6adef6cb7935ce261311e (diff) | |
Add example to ShorthandLookup
Diffstat (limited to 'example_test.go')
| -rw-r--r-- | example_test.go | 24 |
1 files changed, 17 insertions, 7 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) +} |
