forked from ogier/pflag
-
Notifications
You must be signed in to change notification settings - Fork 365
Expand file tree
/
Copy pathexample_test.go
More file actions
59 lines (43 loc) · 1.15 KB
/
example_test.go
File metadata and controls
59 lines (43 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package pflag_test
import (
"fmt"
"github.com/spf13/pflag"
)
func ExampleShorthandLookup() {
name := "verbose"
short := name[:1]
pflag.BoolP(name, short, false, "verbose output")
// len(short) must be == 1
flag := pflag.ShorthandLookup(short)
fmt.Println(flag.Name)
}
func ExampleFlagSet_ShorthandLookup() {
name := "verbose"
short := name[:1]
fs := pflag.NewFlagSet("Example", pflag.ContinueOnError)
fs.BoolP(name, short, false, "verbose output")
// len(short) must be == 1
flag := fs.ShorthandLookup(short)
fmt.Println(flag.Name)
}
func ExampleFlagSet_StringToString() {
args := []string{
"--arg", "a=1,b=2",
"--arg", "a=2",
"--arg=d=4",
}
fs := pflag.NewFlagSet("Example", pflag.ContinueOnError)
fs.StringToString("arg", make(map[string]string), "string-to-string arg accepting key=value pairs")
if err := fs.Parse(args); err != nil {
panic(err)
}
value, err := fs.GetStringToString("arg")
if err != nil {
panic(err)
}
fmt.Println(value)
// Output: map[a:2 b:2 d:4]
}