-
Notifications
You must be signed in to change notification settings - Fork 0
/
flag.go
104 lines (87 loc) · 2.25 KB
/
flag.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package cli
type Flag struct {
name string
help string
short byte
value value
}
func (f *Flag) Short(short byte) *Flag {
f.short = short
return f
}
func (f *Flag) Optional() *OptionalFlag {
return &OptionalFlag{f}
}
func (f *Flag) Int(target *int) *Int {
value := &Int{target: target}
f.value = &intValue{inner: value}
return value
}
func (f *Flag) String(target *string) *String {
value := &String{target: target}
f.value = &stringValue{inner: value}
return value
}
func (f *Flag) Strings(target *[]string) *Strings {
*target = []string{}
value := &Strings{target: target}
f.value = &stringsValue{inner: value}
return value
}
func (f *Flag) Enum(target *string, possibilities ...string) *Enum {
value := &Enum{target: target}
f.value = &enumValue{inner: value, possibilities: possibilities}
return value
}
func (f *Flag) StringMap(target *map[string]string) *StringMap {
*target = map[string]string{}
value := &StringMap{target: target}
f.value = &stringMapValue{inner: value}
return value
}
func (f *Flag) Bool(target *bool) *Bool {
value := &Bool{target: target}
f.value = &boolValue{inner: value}
return value
}
// Custom allows you to define a custom parsing function
func (f *Flag) Custom(fn func(string) error) *Custom {
value := &Custom{target: fn}
f.value = &customValue{inner: value}
return value
}
func (f *Flag) verify(name string) error {
return f.value.verify("--" + name)
}
type OptionalFlag struct {
f *Flag
}
func (f *OptionalFlag) String(target **string) *OptionalString {
value := &OptionalString{target: target}
f.f.value = &optionalStringValue{inner: value}
return value
}
func (f *OptionalFlag) Int(target **int) *OptionalInt {
value := &OptionalInt{target: target}
f.f.value = &optionalIntValue{inner: value}
return value
}
func (f *OptionalFlag) Bool(target **bool) *OptionalBool {
value := &OptionalBool{target: target}
f.f.value = &optionalBoolValue{inner: value}
return value
}
func (f *OptionalFlag) Strings(target *[]string) *Strings {
*target = []string{}
value := &Strings{target: target, optional: true}
f.f.value = &stringsValue{inner: value}
return value
}
func verifyFlags(flags []*Flag) error {
for _, flag := range flags {
if err := flag.verify(flag.name); err != nil {
return err
}
}
return nil
}