-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_flagset.go
333 lines (307 loc) · 7.12 KB
/
config_flagset.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package conf
import (
"flag"
"fmt"
"io"
"log"
"os"
"time"
)
// createFlagSet defines the flagset for all options that have
// been specified within the current working set; All errors are
// accumulated in the Config.errs field and checked at the end of the
// function.
func setupFlagSet(c *Config) error {
const fname = "createFlagSet"
if c.set == nil {
const event = "Config.set is nil"
return fmt.Errorf("%s: %s", fname, event)
}
createFlagSet(c, os.Stdout)
if err := optionsToFlagSet(c); err != nil {
return fmt.Errorf("%s: %w", fname, err)
}
if err := parseFlagSet(c); err != nil {
return fmt.Errorf("%s: %w", fname, err)
}
if v2() {
log.Printf("%s: completed\n", fname)
}
return nil
}
func createFlagSet(c *Config, w io.Writer) {
const fname = "createFlagSet"
if c.set == nil {
panic(fname + ": c.set is nil")
}
// Create our custom flagset.
c.flagSet = flag.NewFlagSet(c.set.cmd, flag.ExitOnError)
// Define help or usage output function, overriding the default
// flag package help function.
if w == nil {
w = os.Stderr
}
c.flagSet.SetOutput(w)
c.flagSet.Usage = func() {
io.WriteString(w, c.header)
io.WriteString(w, c.set.usage)
c.flagSet.VisitAll(flagUsage)
}
if v2() {
log.Printf("%s: completed\n", fname)
}
}
func optionsToFlagSet(c *Config) error {
const fname = "optionsToFlagSet"
for _, o := range c.set.options {
if c.set.flag&o.Commands != 0 {
opt := c.set.options.find(o.Flag)
err := flagsToFlagSet(c, opt)
if err != nil {
opt.err = fmt.Errorf("%s: %s: %w",
fname, o.Flag, err)
c.errs = fmt.Errorf("%s: %s: %w",
fname, c.errs.Error(), opt.err)
}
if v3() {
log.Printf("%s: %s: option added\n",
fname, opt.Flag)
}
}
}
if c.errs != nil {
return fmt.Errorf("%s: %s: %w", fname, c.errs, errConfig)
}
if v2() {
log.Printf("%s: completed\n", fname)
}
return nil
}
// flagsToFlagSet generates a flag within the given set for the current
// option.
func flagsToFlagSet(c *Config, o *Option) error {
const fname = "toFlagSet"
const def = "Default"
const va = "Var"
switch o.Type {
case Int:
i, ok := o.Default.(int)
if !ok {
return fmt.Errorf("%s: %q: %w", o.Type, def,
errType)
}
o.data = c.flagSet.Int(o.Flag, i, o.Usage)
case IntVar:
i, ok := o.Default.(int)
if !ok {
return fmt.Errorf("%s: %q: %w", o.Type, def,
errType)
}
v, ok := o.Var.(*int)
if !ok {
return fmt.Errorf("%s: %q: %w", o.Type, va,
errType)
}
c.flagSet.IntVar(v, o.Flag, i, o.Usage)
case Int64:
i, ok := o.Default.(int64)
if !ok {
return fmt.Errorf("%s: %q: %w", o.Type, def,
errType)
}
o.data = c.flagSet.Int64(o.Flag, i, o.Usage)
case Int64Var:
i, ok := o.Default.(int64)
if !ok {
return fmt.Errorf("%s: %q: %w", o.Type, def,
errType)
}
v, ok := o.Var.(*int64)
if !ok {
return fmt.Errorf("%s: %q: %w", o.Type, va,
errType)
}
c.flagSet.Int64Var(v, o.Flag, i, o.Usage)
case Uint:
i, ok := o.Default.(uint)
if !ok {
return fmt.Errorf("%s: %q: %w", o.Type, def,
errType)
}
o.data = c.flagSet.Uint(o.Flag, i, o.Usage)
case UintVar:
i, ok := o.Default.(uint)
if !ok {
return fmt.Errorf("%s: %q: %w", o.Type, def,
errType)
}
v, ok := o.Var.(*uint)
if !ok {
return fmt.Errorf("%s: %q: %w", o.Type, va,
errType)
}
c.flagSet.UintVar(v, o.Flag, i, o.Usage)
case Uint64:
i, ok := o.Default.(uint64)
if !ok {
return fmt.Errorf("%s: %q: %w", o.Type, def,
errType)
}
o.data = c.flagSet.Uint64(o.Flag, i, o.Usage)
case Uint64Var:
i, ok := o.Default.(uint64)
if !ok {
return fmt.Errorf("%s: %q: %w", o.Type, def,
errType)
}
v, ok := o.Var.(*uint64)
if !ok {
return fmt.Errorf("%s: %q: %w", o.Type, va,
errType)
}
c.flagSet.Uint64Var(v, o.Flag, i, o.Usage)
case Float64:
f, ok := o.Default.(float64)
if !ok {
return fmt.Errorf("%s: %q: %w", o.Type, def,
errType)
}
o.data = c.flagSet.Float64(o.Flag, f, o.Usage)
case Float64Var:
f, ok := o.Default.(float64)
if !ok {
return fmt.Errorf("%s: %q: %w", o.Type, def,
errType)
}
v, ok := o.Var.(*float64)
if !ok {
return fmt.Errorf("%s: %q: %w", o.Type, va,
errType)
}
c.flagSet.Float64Var(v, o.Flag, f, o.Usage)
case String:
s, ok := o.Default.(string)
if !ok {
return fmt.Errorf("%s: %q: %w", o.Type, def,
errType)
}
o.data = c.flagSet.String(o.Flag, s, o.Usage)
case StringVar:
s, ok := o.Default.(string)
if !ok {
return fmt.Errorf("%s: %q: %w", o.Type, def,
errType)
}
v, ok := o.Var.(*string)
if !ok {
return fmt.Errorf("%s: %q: %w", o.Type, va,
errType)
}
c.flagSet.StringVar(v, o.Flag, s, o.Usage)
case Bool:
b, ok := o.Default.(bool)
if !ok {
return fmt.Errorf("%s: %q: %w", o.Type, def,
errType)
}
o.data = c.flagSet.Bool(o.Flag, b, o.Usage)
case BoolVar:
b, ok := o.Default.(bool)
if !ok {
return fmt.Errorf("%s: %q: %w", o.Type, def,
errType)
}
v, ok := o.Var.(*bool)
if !ok {
return fmt.Errorf("%s: %q: %w", o.Type, va,
errType)
}
c.flagSet.BoolVar(v, o.Flag, b, o.Usage)
case Duration:
d, ok := o.Default.(time.Duration)
if !ok {
return fmt.Errorf("%s: %q: %w", o.Type, def,
errType)
}
o.data = c.flagSet.Duration(o.Flag, d, o.Usage)
case DurationVar:
d, ok := o.Default.(time.Duration)
if !ok {
return fmt.Errorf("%s: %q: %w", o.Type, def,
errType)
}
v, ok := o.Var.(*time.Duration)
if !ok {
return fmt.Errorf("%s: %q: %w", o.Type, va,
errType)
}
c.flagSet.DurationVar(v, o.Flag, d, o.Usage)
case Var:
if o.Value == nil {
return fmt.Errorf("%s: %q: %w", o.Type, def,
errTypeNil)
}
c.flagSet.Var(o.Value, o.Flag, o.Usage)
case Nil:
return fmt.Errorf("%s: %q: %w", o.Type, def,
errTypeNil)
default:
return fmt.Errorf("%s: internal error: (%q, %s) %w",
fname, o.Flag, o.Type, errType)
}
if v3() {
log.Printf("%s: completed\n", fname)
}
return nil
}
// parseFlagSet runs the parse command on the configs flagset.
func parseFlagSet(c *Config) error {
const fname = "parseFlagSet"
// When tests are being run, we do not parse the flagset here.
if test {
return nil
}
offset := 1 // the program call needs to be skipped.
// If not the default then a command has been used and we need
// to offset the args by one more place.
if c.set.flag != 1 {
offset++
}
err := c.flagSet.Parse(os.Args[offset:])
if err != nil {
return fmt.Errorf("%s: %w", fname, err)
}
if v2() {
log.Printf("%s: completed\n", fname)
}
return nil
}
// runUserCheckFuncs runs all user given ckFunc functions in the command
// set if data has been provided.
func runUserCheckFuncs(c *Config) error {
const fname = "runUserCheckFuncs"
for _, o := range c.set.options {
if o.Check == nil || o.data == nil {
continue
}
var err error
opt := c.set.options.find(o.Flag)
opt.data, err = o.Check(o.data)
if err != nil {
err := fmt.Errorf("%s: %w", err, ErrCheck)
opt.err = err
if c.errs != nil {
c.errs = fmt.Errorf("%s: %w", c.errs, err)
} else {
c.errs = err
}
}
}
if c.errs != nil {
return fmt.Errorf("%s: %s: %w", fname, c.errs, ErrCheck)
}
if v2() {
log.Printf("%s: completed\n", fname)
}
return nil
}