forked from jpillora/cloud-torrent
/
opts.go
638 lines (571 loc) · 14.6 KB
/
opts.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
package opts
import (
"encoding/json"
"errors"
"flag"
"fmt"
"io/ioutil"
"os"
"reflect"
"strconv"
"strings"
"time"
)
//Opts is the main class, it contains
//all parsing state for a single set of
//arguments
type Opts struct {
//embed item since an Opts can also be an item
item
parent *Opts
cmds map[string]*Opts
opts []*item
args []*item
arglist *argumentlist
optnames map[string]bool
envnames map[string]bool
order []string
templates map[string]string
internalOpts struct {
//pretend these are in the user struct :)
Help, Version bool
}
cfgPath string
erred error
cmdname *reflect.Value
repo, author, version string
pkgrepo, pkgauthor string
//LineWidth defines where new-lines
//are inserted into the help text
//(defaults to 42)
LineWidth int
//PadAll enables padding around the
//help text (defaults to true)
PadAll bool
//PadWidth defines the amount padding
//when rendering help text (defaults to 2)
PadWidth int
}
//argumentlist represends a
//named string slice
type argumentlist struct {
item
min int
}
//item is the structure representing a
//an opt item
type item struct {
val reflect.Value
name string
shortName string
envName string
useEnv bool
typeName string
help string
defstr string
}
//New creates a new Opts instance
func New(config interface{}) *Opts {
v := reflect.ValueOf(config)
//nil parent -> root command
o := fork(nil, v)
if o.erred != nil {
//opts has already encounted an error
return o
}
//attempt to infer package name, repo, author
pkgpath := v.Elem().Type().PkgPath()
parts := strings.Split(pkgpath, "/")
if len(parts) >= 3 {
o.pkgauthor = parts[1]
o.Name(parts[2])
switch parts[0] {
case "github.com", "bitbucket.org":
o.pkgrepo = "https://" + strings.Join(parts[0:3], "/")
}
}
return o
}
//Parse(&config) is shorthand for New(&config).Parse()
func Parse(config interface{}) *Opts {
return New(config).Parse()
}
func fork(parent *Opts, val reflect.Value) *Opts {
//TODO allow order and template per cmd
//for now, there is only the root
var order []string = nil
var tmpls map[string]string = nil
if parent == nil {
order = make([]string, len(DefaultOrder))
copy(order, DefaultOrder)
tmpls = map[string]string{}
} else {
order = parent.order
tmpls = parent.templates
}
//instantiate
o := &Opts{
item: item{
val: val,
},
parent: parent,
//each cmd/cmd has its own set of names
optnames: map[string]bool{},
envnames: map[string]bool{},
cmds: map[string]*Opts{},
opts: []*item{},
//these are only set at the root
order: order,
templates: tmpls,
//public defaults
LineWidth: 72,
PadAll: true,
PadWidth: 2,
}
//all fields from val
if val.Type().Kind() != reflect.Ptr {
o.errorf("opts: %s should be a pointer to a struct", val.Type().Name())
return o
}
o.addFields(val.Elem())
//add help option
g := reflect.ValueOf(&o.internalOpts).Elem()
o.addOptArg(g.Type().Field(0), g.Field(0))
return o
}
func (o *Opts) addFields(c reflect.Value) *Opts {
t := c.Type()
k := t.Kind()
//deref pointer
if k == reflect.Ptr {
c = c.Elem()
t = c.Type()
k = t.Kind()
}
if k != reflect.Struct {
o.errorf("opts: %s should be a pointer to a struct (got %s)", t.Name(), k)
return o
}
//parse struct fields
for i := 0; i < c.NumField(); i++ {
val := c.Field(i)
//ignore unexported
if !val.CanSet() {
continue
}
sf := t.Field(i)
k := sf.Type.Kind()
switch k {
case reflect.Ptr, reflect.Struct:
if sf.Tag.Get("type") == "embedded" {
o.addFields(val)
} else {
o.addCmd(sf, val)
}
case reflect.Slice:
if sf.Type.Elem().Kind() != reflect.String {
o.errorf("arglist must be of type []string")
return o
}
o.addArgs(sf, val)
case reflect.Bool, reflect.String, reflect.Int, reflect.Int64:
if sf.Tag.Get("type") == "cmdname" {
if k != reflect.String {
o.errorf("cmdname field '%s' must be a string", sf.Name)
return o
}
o.cmdname = &val
} else {
o.addOptArg(sf, val)
}
default:
o.errorf("Struct field '%s' has unsupported type: %s", sf.Name, k)
return o
}
}
return o
}
func (o *Opts) errorf(format string, args ...interface{}) *Opts {
//only store the first error
if o.erred == nil {
o.erred = fmt.Errorf(format, args...)
}
return o
}
func (o *Opts) addCmd(sf reflect.StructField, val reflect.Value) {
if o.arglist != nil {
o.errorf("argslists and commands cannot be used together")
return
}
//requires address
switch sf.Type.Kind() {
case reflect.Ptr:
//if nil ptr, auto-create new struct
if val.IsNil() {
ptr := reflect.New(val.Type().Elem())
val.Set(ptr)
}
case reflect.Struct:
val = val.Addr()
}
name := sf.Tag.Get("name")
if name == "" || name == "!" {
name = camel2dash(sf.Name) //default to struct field name
}
// log.Printf("define cmd: %s =====", subname)
sub := fork(o, val)
sub.name = name
sub.help = sf.Tag.Get("help")
o.cmds[name] = sub
}
func (o *Opts) addArgs(sf reflect.StructField, val reflect.Value) {
if len(o.cmds) > 0 {
o.errorf("argslists and commands cannot be used together")
return
}
if o.arglist != nil {
o.errorf("only 1 arglist field is allowed ('%s' already defined)", o.arglist.name)
return
}
name := sf.Tag.Get("name")
if name == "" || name == "!" {
name = camel2dash(sf.Name) //default to struct field name
}
if val.Len() != 0 {
o.errorf("arglist '%s' is required so it should not be set. "+
"If you'd like to set a default, consider using an option instead.", name)
return
}
min, _ := strconv.Atoi(sf.Tag.Get("min"))
//insert
o.arglist = &argumentlist{
item: item{
val: val,
name: name,
help: sf.Tag.Get("help"),
},
min: min,
}
}
var durationType = reflect.TypeOf(time.Second)
func (o *Opts) addOptArg(sf reflect.StructField, val reflect.Value) {
i := &item{val: val}
//find name
i.name = sf.Tag.Get("name")
if i.name == "" {
i.name = camel2dash(sf.Name) //default to struct field name
}
//assume int64s are durations
if sf.Type.Kind() == reflect.Int64 &&
!sf.Type.AssignableTo(durationType) {
o.errorf("int64 field '%s' must be of type time.Duration", i.name)
return
}
//specific environment name
i.envName = sf.Tag.Get("env")
if i.envName != "" {
if o.envnames[i.envName] {
o.errorf("option env name '%s' already in use", i.name)
return
}
o.envnames[i.envName] = true
i.useEnv = true
}
//assume opt, unless arg tag is present
t := sf.Tag.Get("type")
if t == "" {
t = "opt"
}
//opt names cannot clash with each other
if o.optnames[i.name] {
o.errorf("option name '%s' already in use", i.name)
return
}
o.optnames[i.name] = true
//get help text
i.help = sf.Tag.Get("help")
//the **displayed** default, use 'default' tag, otherwise infer
defstr := sf.Tag.Get("default")
if defstr != "" {
i.defstr = defstr
} else if def := val.Interface(); def != reflect.Zero(sf.Type).Interface() {
//not the zero-value, stringify!
i.defstr = fmt.Sprintf("%v", def)
}
switch t {
case "opt":
//options can also set short names
if short := sf.Tag.Get("short"); short != "" {
if o.optnames[short] {
o.errorf("option short name '%s' already in use", short)
return
} else {
o.optnames[i.shortName] = true
i.shortName = short
}
}
// log.Printf("define option: %s %s", name, sf.Type)
o.opts = append(o.opts, i)
case "arg":
//TODO allow other types in 'arg' fields
if sf.Type.Kind() != reflect.String {
o.errorf("arg '%s' type must be a string", i.name)
return
}
o.args = append(o.args, i)
default:
o.errorf("Invalid optype: %s", t)
}
}
//Name sets the name of the program
func (o *Opts) Name(name string) *Opts {
o.name = name
return o
}
//Version sets the version of the program
//and renders the 'version' template in the help text
func (o *Opts) Version(version string) *Opts {
//add version option
g := reflect.ValueOf(&o.internalOpts).Elem()
o.addOptArg(g.Type().Field(1), g.Field(1))
o.version = version
return o
}
//Repo sets the repository link of the program
//and renders the 'repo' template in the help text
func (o *Opts) Repo(repo string) *Opts {
o.repo = repo
return o
}
//PkgRepo infers the repository link of the program
//from the package import path of the struct (So note,
//this will not work for 'main' packages)
func (o *Opts) PkgRepo() *Opts {
if o.pkgrepo == "" {
return o.errorf("Package repository could not be infered")
}
o.Repo(o.pkgrepo)
return o
}
//Author sets the author of the program
//and renders the 'author' template in the help text
func (o *Opts) Author(author string) *Opts {
o.author = author
return o
}
//PkgRepo infers the repository link of the program
//from the package import path of the struct (So note,
//this will not work for 'main' packages)
func (o *Opts) PkgAuthor() *Opts {
if o.pkgrepo == "" {
return o.errorf("Package author could not be infered")
}
o.Author(o.pkgauthor)
return o
}
//DocBefore inserts a text block before the specified template
func (o *Opts) DocBefore(target, newid, template string) *Opts {
return o.docOffset(0, target, newid, template)
}
//DocAfter inserts a text block after the specified template
func (o *Opts) DocAfter(target, newid, template string) *Opts {
return o.docOffset(1, target, newid, template)
}
func (o *Opts) docOffset(offset int, target, newid, template string) *Opts {
if _, ok := o.templates[newid]; ok {
o.errorf("new template already exists: %s", newid)
return o
}
for i, id := range o.order {
if id == target {
o.templates[newid] = template
index := i + offset
rest := []string{newid}
if index < len(o.order) {
rest = append(rest, o.order[index:]...)
}
o.order = append(o.order[:index], rest...)
return o
}
}
o.errorf("target template not found: %s", target)
return o
}
//DecSet replaces the specified template
func (o *Opts) DocSet(id, template string) *Opts {
if _, ok := DefaultTemplates[id]; !ok {
if _, ok := o.templates[id]; !ok {
o.errorf("template does not exist: %s", id)
return o
}
}
o.templates[id] = template
return o
}
//ConfigPath defines a path to a JSON file which matches
//the structure of the provided config. Environment variables
//override JSON Config variables.
func (o *Opts) ConfigPath(path string) *Opts {
o.cfgPath = path
return o
}
//UseEnv enables an implicit "env" struct tag option on
//all struct fields, the name of the field is converted
//into an environment variable with the transform
//`FooBar` -> `FOO_BAR`.
func (o *Opts) UseEnv() *Opts {
o.useEnv = true
return o
}
//Parse with os.Args
func (o *Opts) Parse() *Opts {
return o.ParseArgs(os.Args[1:])
}
//ParseArgs with the provided arguments
func (o *Opts) ParseArgs(args []string) *Opts {
if err := o.Process(args); err != nil {
fmt.Fprintf(os.Stderr, err.Error()+"\n")
os.Exit(1)
}
return o
}
//Process is the same as ParseArgs except
//it returns an error on failure
func (o *Opts) Process(args []string) error {
//cannot be processed - already encountered error - programmer error
if o.erred != nil {
return fmt.Errorf("Process error: %s", o.erred)
}
//1. set config via JSON file
if o.cfgPath != "" {
b, err := ioutil.ReadFile(o.cfgPath)
if err == nil {
v := o.val.Interface() //*struct
err = json.Unmarshal(b, v)
if err != nil {
o.erred = fmt.Errorf("Invalid config file: %s", err)
return errors.New(o.Help())
}
}
}
flagset := flag.NewFlagSet(o.name, flag.ContinueOnError)
flagset.SetOutput(ioutil.Discard)
//pre-loop through the options and
//add shortnames and env names where possible
for _, opt := range o.opts {
//should generate shortname?
if len(opt.name) >= 3 && opt.shortName == "" {
//not already taken?
if s := opt.name[0:1]; !o.optnames[s] {
opt.shortName = s
o.optnames[s] = true
}
}
env := camel2const(opt.name)
if o.useEnv && (opt.envName == "" || opt.envName == "!") &&
opt.name != "help" && opt.name != "version" &&
!o.envnames[env] {
opt.envName = env
}
}
for _, opt := range o.opts {
// TODO remove debug
// log.Printf("parse prepare option: %s", opt.name)
//2. set config via environment
envVal := ""
if opt.useEnv || o.useEnv {
envVal = os.Getenv(opt.envName)
}
//3. set config via Go's pkg/flags
addr := opt.val.Addr().Interface()
switch addr := addr.(type) {
case flag.Value:
flagset.Var(addr, opt.name, "")
if opt.shortName != "" {
flagset.Var(addr, opt.shortName, "")
}
case *bool:
str2bool(envVal, addr)
flagset.BoolVar(addr, opt.name, *addr, "")
if opt.shortName != "" {
flagset.BoolVar(addr, opt.shortName, *addr, "")
}
case *string:
str2str(envVal, addr)
flagset.StringVar(addr, opt.name, *addr, "")
if opt.shortName != "" {
flagset.StringVar(addr, opt.shortName, *addr, "")
}
case *int:
str2int(envVal, addr)
flagset.IntVar(addr, opt.name, *addr, "")
if opt.shortName != "" {
flagset.IntVar(addr, opt.shortName, *addr, "")
}
case *time.Duration:
flagset.DurationVar(addr, opt.name, *addr, "")
if opt.shortName != "" {
flagset.DurationVar(addr, opt.shortName, *addr, "")
}
default:
return fmt.Errorf("Option '%s' has unsupported type", opt.name)
}
}
// log.Printf("parse %+v", args)
//set user config
err := flagset.Parse(args)
if err != nil {
//insert flag errors into help text
o.erred = err
o.internalOpts.Help = true
}
//internal opts (--help and --version)
if o.internalOpts.Help {
return errors.New(o.Help())
} else if o.internalOpts.Version {
fmt.Println(o.version)
os.Exit(0)
}
//fill each individual arg
args = flagset.Args()
for i, argument := range o.args {
if len(args) > 0 {
str := args[0]
args = args[1:]
argument.val.SetString(str)
} else if argument.defstr == "" {
//not-set and no default!
o.erred = fmt.Errorf("Argument #%d '%s' has no default value", i+1, argument.name)
return errors.New(o.Help())
}
}
//use command? peek at args
if len(o.cmds) > 0 && len(args) > 0 {
a := args[0]
//matching command, use it
if sub, exists := o.cmds[a]; exists {
//user wants name to be set
if o.cmdname != nil {
o.cmdname.SetString(a)
}
return sub.Process(args[1:])
}
}
//fill arglist? assign remaining as slice
if o.arglist != nil {
if len(args) < o.arglist.min {
o.erred = fmt.Errorf("Too few arguments (expected %d, got %d)", o.arglist.min, len(args))
return errors.New(o.Help())
}
o.arglist.val.Set(reflect.ValueOf(args))
args = nil
}
//we *should* have consumed all args at this point.
//this prevents: ./foo --bar 42 -z 21 ping --pong 7
//where --pong 7 is ignored
if len(args) != 0 {
o.erred = fmt.Errorf("Unexpected arguments: %+v", args)
return errors.New(o.Help())
}
return nil
}