-
Notifications
You must be signed in to change notification settings - Fork 313
/
flags.go
221 lines (203 loc) · 4.31 KB
/
flags.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
211
212
213
214
215
216
217
218
219
220
221
package types
import (
"fmt"
"strconv"
"strings"
"github.com/gobwas/glob"
"github.com/spf13/cobra"
)
// Flag is a option flag.
type Flag struct {
ContextKey ContextKey
Type string
Desc string
Default string
Short string
Enums []string
Aliases []string
Hidden bool
}
// FlagSet is a set of option flags.
type FlagSet struct {
Type string
Name string
Flag Flag
}
// Add adds the flag to the cmd.
func (flag FlagSet) Add(cmd *cobra.Command, values map[ContextKey]*Value) error {
switch flag.Flag.Type {
case "bool", "int", "string", "[]string", "glob":
default:
return fmt.Errorf("unknown flag type %s", flag.Flag.Type)
}
// create value
if _, ok := values[flag.Flag.ContextKey]; !ok {
values[flag.Flag.ContextKey] = NewValue(flag.Flag.Type, flag.Flag.Default, flag.Flag.Desc, flag.Flag.Enums...)
}
flags, name, desc := cmd.Flags(), flag.Type+"-"+flag.Name, values[flag.Flag.ContextKey].Desc()
// force pflag flag as bool
noOptDefValue := ""
if flag.Flag.Type == "bool" {
noOptDefValue = "true"
}
// add flag
switch {
case flag.Flag.Short == "":
flags.Var(values[flag.Flag.ContextKey], name, desc)
case flags.ShorthandLookup(flag.Flag.Short) != nil:
desc += fmt.Sprintf(" (short flag -%s also available)", flag.Flag.Short)
flags.Var(values[flag.Flag.ContextKey], name, desc)
default:
flags.VarP(values[flag.Flag.ContextKey], name, flag.Flag.Short, desc)
}
// add aliases
for _, alias := range flag.Flag.Aliases {
if flags.Lookup(alias) != nil {
continue
}
flags.Var(values[flag.Flag.ContextKey], alias, desc)
f := flags.Lookup(alias)
f.Hidden, f.NoOptDefVal = true, noOptDefValue
}
// copy short, hidden, bool
f := flags.Lookup(name)
f.Hidden, f.NoOptDefVal = flag.Flag.Hidden, noOptDefValue
return nil
}
// Value wraps a flag value.
type Value struct {
typ string
def string
desc string
enums []string
set bool
v interface{}
}
// NewValue creates a new flag value.
func NewValue(typ, def, desc string, enums ...string) *Value {
var z interface{}
switch typ {
case "bool":
var b bool
z = b
case "int":
var i int
z = i
case "string":
var s string
z = s
case "[]string":
var s []string
z = s
case "glob":
var v []glob.Glob
z = v
}
v := &Value{
typ: typ,
def: def,
desc: desc,
enums: enums,
v: z,
}
if v.def != "" {
if err := v.Set(v.def); err != nil {
panic(err)
}
v.set = false
}
return v
}
// String satisfies the pflag.Value interface.
func (v *Value) String() string {
return v.def
}
// Desc returns the usage description for the flag value.
func (v *Value) Desc() string {
if v.enums != nil {
return v.desc + " <" + strings.Join(v.enums, "|") + ">"
}
return v.desc
}
// Set satisfies the pflag.Value interface.
func (v *Value) Set(s string) error {
v.set = true
if v.enums != nil {
if !contains(v.enums, s) {
return fmt.Errorf("invalid value %q", s)
}
}
switch v.typ {
case "bool":
b, err := strconv.ParseBool(s)
if err != nil {
return err
}
v.v = b
case "int":
i, err := strconv.Atoi(s)
if err != nil {
return err
}
v.v = i
case "string":
v.v = s
case "[]string":
v.v = append(v.v.([]string), strings.Split(s, ",")...)
case "glob":
g, err := glob.Compile(s)
if err != nil {
return err
}
v.v = append(v.v.([]glob.Glob), g)
}
return nil
}
// Interface returns the value.
func (v *Value) Interface() interface{} {
if v.v == nil {
panic("v should not be nil!")
}
return v.v
}
// AsBool returns the value as a bool.
func (v *Value) AsBool() bool {
b, _ := v.v.(bool)
return b
}
// AsInt returns the value as a int.
func (v *Value) AsInt() int {
i, _ := v.v.(int)
return i
}
// AsString returns the value as a string.
func (v *Value) AsString() string {
s, _ := v.v.(string)
return s
}
// AsStringSlice returns the value as a string slice.
func (v *Value) AsStringSlice() []string {
z, _ := v.v.([]string)
return z
}
// AsGlob returns the value as a glob slice.
func (v *Value) AsGlob() []glob.Glob {
z, _ := v.v.([]glob.Glob)
return z
}
// Type satisfies the pflag.Value interface.
func (v *Value) Type() string {
if v.typ == "[]string" {
return "string"
}
return v.typ
}
// contains determines if v contains str.
func contains(v []string, str string) bool {
for _, s := range v {
if s == str {
return true
}
}
return false
}