-
Notifications
You must be signed in to change notification settings - Fork 2
/
flags.go
292 lines (252 loc) · 6.4 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package flags
import (
"errors"
"fmt"
"strconv"
"strings"
"github.com/simonleung8/flags/flag"
)
type FlagSet interface {
fmt.Stringer
GetName() string
GetShortName() string
GetValue() interface{}
Set(string)
}
type FlagContext interface {
Parse(...string) error
Args() []string
Int(string) int
Float64(string) float64
Bool(string) bool
String(string) string
StringSlice(string) []string
IsSet(string) bool
SkipFlagParsing(bool)
NewStringFlag(name string, shortName string, usage string)
NewStringFlagWithDefault(name string, shortName string, usage string, value string)
NewBoolFlag(name string, shortName string, usage string)
NewIntFlag(name string, shortName string, usage string)
NewIntFlagWithDefault(name string, shortName string, usage string, value int)
NewFloat64Flag(name string, shortName string, usage string)
NewFloat64FlagWithDefault(name string, shortName string, usage string, value float64)
NewStringSliceFlag(name string, shortName string, usage string)
NewStringSliceFlagWithDefault(name string, shortName string, usage string, value []string)
ShowUsage(leadingSpace int) string
}
type flagContext struct {
flagsets map[string]FlagSet
args []string
cmdFlags map[string]FlagSet //valid flags for command
cursor int
skipFlagParsing bool
}
func New() FlagContext {
return &flagContext{
flagsets: make(map[string]FlagSet),
cmdFlags: make(map[string]FlagSet),
cursor: 0,
}
}
func NewFlagContext(cmdFlags map[string]FlagSet) FlagContext {
return &flagContext{
flagsets: make(map[string]FlagSet),
cmdFlags: cmdFlags,
cursor: 0,
}
}
func (c *flagContext) Parse(args ...string) error {
var flagset FlagSet
var ok bool
var v string
var err error
c.setDefaultFlagValueIfAny()
for c.cursor <= len(args)-1 {
arg := args[c.cursor]
if !c.skipFlagParsing && (strings.HasPrefix(arg, "-") || strings.HasPrefix(arg, "--")) {
flg := strings.TrimLeft(strings.TrimLeft(arg, "-"), "-")
c.extractEqualSignIfAny(&flg, &args)
if flagset, ok = c.cmdFlags[flg]; !ok {
flg = c.getFlagNameWithShortName(flg)
if flagset, ok = c.cmdFlags[flg]; !ok {
return errors.New("Invalid flag: " + arg)
}
}
switch flagset.GetValue().(type) {
case bool:
c.flagsets[flg] = &cliFlags.BoolFlag{Name: flg, Value: c.getBoolFlagValue(args)}
case int:
if v, err = c.getFlagValue(args); err != nil {
return err
}
i, err := strconv.ParseInt(v, 10, 32)
if err != nil {
return errors.New("Value for flag '" + flg + "' must be an integer")
}
c.flagsets[flg] = &cliFlags.IntFlag{Name: flg, Value: int(i)}
case float64:
if v, err = c.getFlagValue(args); err != nil {
return err
}
i, err := strconv.ParseFloat(v, 64)
if err != nil {
return errors.New("Value for flag '" + flg + "' must be a float64")
}
c.flagsets[flg] = &cliFlags.Float64Flag{Name: flg, Value: float64(i)}
case string:
if v, err = c.getFlagValue(args); err != nil {
return err
}
c.flagsets[flg] = &cliFlags.StringFlag{Name: flg, Value: v}
case []string:
if v, err = c.getFlagValue(args); err != nil {
return err
}
if _, ok = c.flagsets[flg]; !ok {
c.flagsets[flg] = &cliFlags.StringSliceFlag{Name: flg, Value: []string{v}}
} else {
c.flagsets[flg].Set(v)
}
}
} else {
c.args = append(c.args, args[c.cursor])
}
c.cursor++
}
return nil
}
func (c *flagContext) getFlagValue(args []string) (string, error) {
if c.cursor >= len(args)-1 {
return "", errors.New("No value provided for flag: " + args[c.cursor])
}
c.cursor++
return args[c.cursor], nil
}
func (c *flagContext) getBoolFlagValue(args []string) bool {
if c.cursor >= len(args)-1 {
return true
}
b, err := strconv.ParseBool(args[c.cursor+1])
if err == nil {
c.cursor++
return b
}
return true
}
func (c *flagContext) Args() []string {
return c.args
}
func (c *flagContext) IsSet(k string) bool {
return c.isFlagProvided(&k)
}
func (c *flagContext) Int(k string) int {
if !c.isFlagProvided(&k) {
return 0
}
v := c.flagsets[k].GetValue()
switch v.(type) {
case int:
return v.(int)
}
return 0
}
func (c *flagContext) Float64(k string) float64 {
if !c.isFlagProvided(&k) {
return 0
}
v := c.flagsets[k].GetValue()
switch v.(type) {
case float64:
return v.(float64)
}
return 0
}
func (c *flagContext) String(k string) string {
if !c.isFlagProvided(&k) {
return ""
}
v := c.flagsets[k].GetValue()
switch v.(type) {
case string:
return v.(string)
}
return ""
}
func (c *flagContext) Bool(k string) bool {
if !c.isFlagProvided(&k) {
return false
}
v := c.flagsets[k].GetValue()
switch v.(type) {
case bool:
return v.(bool)
}
return false
}
func (c *flagContext) StringSlice(k string) []string {
if !c.isFlagProvided(&k) {
return []string{}
}
v := c.flagsets[k].GetValue()
switch v.(type) {
case []string:
return v.([]string)
}
return []string{}
}
func (c *flagContext) SkipFlagParsing(skip bool) {
c.skipFlagParsing = skip
}
func (c *flagContext) extractEqualSignIfAny(flg *string, args *[]string) {
if strings.Contains(*flg, "=") {
tmpAry := strings.SplitN(*flg, "=", 2)
*flg = tmpAry[0]
tmpArg := append((*args)[:c.cursor], tmpAry[1])
*args = append(tmpArg, (*args)[c.cursor:]...)
}
}
func (c *flagContext) setDefaultFlagValueIfAny() {
var v interface{}
for flgName, flg := range c.cmdFlags {
v = flg.GetValue()
switch v.(type) {
case bool:
if v.(bool) != false {
c.flagsets[flgName] = &cliFlags.BoolFlag{Name: flgName, Value: v.(bool)}
}
case int:
if v.(int) != 0 {
c.flagsets[flgName] = &cliFlags.IntFlag{Name: flgName, Value: v.(int)}
}
case float64:
if v.(float64) != 0 {
c.flagsets[flgName] = &cliFlags.Float64Flag{Name: flgName, Value: v.(float64)}
}
case string:
if len(v.(string)) != 0 {
c.flagsets[flgName] = &cliFlags.StringFlag{Name: flgName, Value: v.(string)}
}
case []string:
if len(v.([]string)) != 0 {
c.flagsets[flgName] = &cliFlags.StringSliceFlag{Name: flgName, Value: v.([]string)}
}
}
}
}
func (c *flagContext) getFlagNameWithShortName(shortName string) string {
for n, f := range c.cmdFlags {
if f.GetShortName() == shortName {
return n
}
}
return ""
}
func (c *flagContext) isFlagProvided(flg *string) bool {
if _, ok := c.flagsets[*flg]; !ok {
*flg = c.getFlagNameWithShortName(*flg)
if _, ok := c.flagsets[*flg]; !ok {
return false
}
}
return true
}