This repository has been archived by the owner on May 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflag_util.go
210 lines (180 loc) · 4.96 KB
/
flag_util.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package bite
import (
"fmt"
"reflect"
"strconv"
"strings"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
// FlagPair is just a map[string]interface{}, see `CheckRequiredFlags` for more.
type FlagPair map[string]interface{}
type Flags = pflag.FlagSet
// CheckRequiredFlags function can be used to manually check for required flags, when the command does not specify a required flag (mostly because of file loading feature).
func CheckRequiredFlags(cmd *cobra.Command, nameValuePairs FlagPair) (err error) {
if nameValuePairs == nil {
return nil
}
var emptyFlags []string
for name, value := range nameValuePairs {
v := reflect.Indirect(reflect.ValueOf(value))
if v.CanInterface() && v.Type().Comparable() {
if v.Interface() == reflect.Zero(reflect.TypeOf(value)).Interface() {
emptyFlags = append(emptyFlags, strconv.Quote(name))
}
}
}
if n := len(emptyFlags); n > 0 {
if n == 1 {
// required flag "flag 1" not set
err = fmt.Errorf("required flag %s not set", emptyFlags[0])
} else {
// required flags "flag 1" and "flag 2" not set
// required flags "flag 1", "flag 2" and "flag 3" not set
err = fmt.Errorf("required flags %s and %s not set",
strings.Join(emptyFlags[0:n-1], ", "), emptyFlags[n-1])
}
if len(nameValuePairs) == n {
// if all required flags are not passed, then show an example in the end.
err = fmt.Errorf("%s\nexample:\n\t%s", err, cmd.Example)
}
}
return
}
func RequireFlags(nameValuePairsFunc func() FlagPair) CobraRunner {
return func(cmd *cobra.Command, args []string) error {
return CheckRequiredFlags(cmd, nameValuePairsFunc())
}
}
// CountRegisteredFlags returns the length of the registered flags (except help), some of them may not be used "now" at all.
func CountRegisteredFlags(set *pflag.FlagSet) (n int) {
// formal instead of actual, no hidden, it may include help flag.
set.VisitAll(func(*pflag.Flag) { n++ })
return
}
func getFlagValue(name string, set *pflag.FlagSet) (flagValue reflect.Value) {
flag := set.Lookup(name)
flagName := flag.Name
switch flag.Value.Type() {
case "string":
vv, _ := set.GetString(flagName)
// don't be lazy, don't try something like 'var flagValue interface{}; ...end... reflect.ValueOf(flagValue)`.
flagValue = reflect.ValueOf(vv)
case "bool":
vv, _ := set.GetBool(flagName)
flagValue = reflect.ValueOf(vv)
case "int", "count":
vv, _ := set.GetInt(flagName)
flagValue = reflect.ValueOf(vv)
case "int32":
vv, _ := set.GetInt32(flagName)
flagValue = reflect.ValueOf(vv)
case "int64":
vv, _ := set.GetInt64(flagName)
flagValue = reflect.ValueOf(vv)
case "float32":
vv, _ := set.GetFloat32(flagName)
flagValue = reflect.ValueOf(vv)
case "float64":
vv, _ := set.GetFloat64(flagName)
flagValue = reflect.ValueOf(vv)
case "uint8":
vv, _ := set.GetUint8(flagName)
flagValue = reflect.ValueOf(vv)
case "uint16":
vv, _ := set.GetUint16(flagName)
flagValue = reflect.ValueOf(vv)
case "uint32":
vv, _ := set.GetUint32(flagName)
flagValue = reflect.ValueOf(vv)
case "uint64":
vv, _ := set.GetUint64(flagName)
flagValue = reflect.ValueOf(vv)
case "stringSlice":
vv, _ := set.GetStringSlice(flagName)
flagValue = reflect.ValueOf(vv)
case "intSlice":
vv, _ := set.GetIntSlice(flagName)
flagValue = reflect.ValueOf(vv)
case "uintSlice":
vv, _ := set.GetUintSlice(flagName)
flagValue = reflect.ValueOf(vv)
case "boolSlice":
vv, _ := set.GetBoolSlice(flagName)
flagValue = reflect.ValueOf(vv)
case "ip":
vv, _ := set.GetIP(flagName)
flagValue = reflect.ValueOf(vv)
case "ipMask":
vv, _ := set.GetIPv4Mask(flagName)
flagValue = reflect.ValueOf(vv)
case "ipNet":
vv, _ := set.GetIPNet(flagName)
flagValue = reflect.ValueOf(vv)
}
return
}
// Supported custom types underline are: strings, ints and booleans only.
type FlagVar struct {
value reflect.Value
}
func NewFlagVar(v interface{}) *FlagVar {
return &FlagVar{reflect.ValueOf(v)}
}
func (f FlagVar) String() string {
return f.value.Elem().String()
}
func (f FlagVar) Set(v string) error {
typ := f.value.Elem().Kind()
switch typ {
case reflect.String:
f.value.Elem().SetString(v)
break
case reflect.Int:
intValue, err := strconv.Atoi(v)
if err != nil {
return err
}
f.value.Elem().SetInt(int64(intValue))
break
case reflect.Bool:
boolValue, err := strconv.ParseBool(v)
if err != nil {
return err
}
f.value.Elem().SetBool(boolValue)
break
}
return nil
}
func (f FlagVar) Type() string {
return f.value.Elem().Kind().String() // reflect/type.go#605
}
func whichColor(v string, base int) (intValue int) {
switch strings.ToLower(v) {
case "black":
intValue = base
case "red":
intValue = base + 1
case "green":
intValue = base + 2
case "yellow":
intValue = base + 3
case "blue":
intValue = base + 4
case "magenta":
intValue = base + 5
case "cyan":
intValue = base + 6
case "white":
intValue = base + 7
}
if intValue < 30 {
var err error
intValue, err = strconv.Atoi(v)
if err != nil {
return 0
}
}
return
}