-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_test.go
71 lines (57 loc) · 1.35 KB
/
parse_test.go
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
60
61
62
63
64
65
66
67
68
69
70
71
package flagfile_test
import (
"flag"
"fmt"
"strings"
"layeh.com/flagfile"
)
const exampleParseStr = `# Enable video output
enable-video
# Enable audio output
#enable-audio
# List of administrative users
user tim cooper
user dave
# Initial size
size 3
# Message for new users
message hello"\t"world
`
type stringSlice []string
func (s stringSlice) String() string {
return strings.Join([]string(s), ",")
}
func (s *stringSlice) Set(v string) error {
*s = append([]string(*s), v)
return nil
}
func ExampleParse() {
args, err := flagfile.Parse(strings.NewReader(exampleParseStr))
if err != nil {
panic(err)
}
f := flag.NewFlagSet("example", flag.ContinueOnError)
enableVideo := f.Bool("enable-video", false, "Enable video output")
enableAudio := f.Bool("enable-audio", false, "Enable audio output")
var users stringSlice
f.Var(&users, "user", "Administrative users")
size := f.Int("size", 0, "Initial Size")
message := f.String("message", "", "New user message")
if err := f.Parse(args); err != nil {
panic(err)
}
fmt.Println("enable-video", *enableVideo)
fmt.Println("enable-audio", *enableAudio)
for _, user := range users {
fmt.Println("user", user)
}
fmt.Println("size", *size)
fmt.Println("message", *message)
// Output:
// enable-video true
// enable-audio false
// user tim cooper
// user dave
// size 3
// message hello world
}