summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/spf13/pflag/example_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/spf13/pflag/example_test.go')
-rw-r--r--vendor/github.com/spf13/pflag/example_test.go73
1 files changed, 16 insertions, 57 deletions
diff --git a/vendor/github.com/spf13/pflag/example_test.go b/vendor/github.com/spf13/pflag/example_test.go
index 9be7a49f2..abd7806fa 100644
--- a/vendor/github.com/spf13/pflag/example_test.go
+++ b/vendor/github.com/spf13/pflag/example_test.go
@@ -2,76 +2,35 @@
// 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.
package pflag_test
import (
- "errors"
"fmt"
- "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")
+func ExampleShorthandLookup() {
+ name := "verbose"
+ short := name[:1]
-// Example 2: A flag with a shorthand letter.
-var gopherType = flag.StringP("gopher_type", "g", "pocket", "the variety of gopher")
+ pflag.BoolP(name, short, false, "verbose output")
-// Example 3: A user-defined flag type, a slice of durations.
-type interval []time.Duration
+ // len(short) must be == 1
+ flag := pflag.ShorthandLookup(short)
-// String is the method to format the flag's value, part of the flag.Value interface.
-// The String method's output will be used in diagnostics.
-func (i *interval) String() string {
- return fmt.Sprint(*i)
+ fmt.Println(flag.Name)
}
-func (i *interval) Type() string {
- return "interval"
-}
-
-// Set is the method to set the flag value, part of the flag.Value interface.
-// Set's argument is a string to be parsed to set the flag.
-// It's a comma-separated list, so we split it.
-func (i *interval) Set(value string) error {
- // If we wanted to allow the flag to be set multiple times,
- // accumulating values, we would delete this if statement.
- // That would permit usages such as
- // -deltaT 10s -deltaT 15s
- // and other combinations.
- if len(*i) > 0 {
- return errors.New("interval flag already set")
- }
- for _, dt := range strings.Split(value, ",") {
- duration, err := time.ParseDuration(dt)
- if err != nil {
- return err
- }
- *i = append(*i, duration)
- }
- return nil
-}
+func ExampleFlagSet_ShorthandLookup() {
+ name := "verbose"
+ short := name[:1]
-// Define a flag to accumulate durations. Because it has a special type,
-// we need to use the Var function and therefore create the flag during
-// init.
+ fs := pflag.NewFlagSet("Example", pflag.ContinueOnError)
+ fs.BoolP(name, short, false, "verbose output")
-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")
-}
+ // len(short) must be == 1
+ flag := fs.ShorthandLookup(short)
-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
- // execute, typically at the start of main (not init!):
- // flag.Parse()
- // We don't run it here because this is not a main function and
- // the testing suite has already parsed the flags.
+ fmt.Println(flag.Name)
}