-
Notifications
You must be signed in to change notification settings - Fork 0
/
gli.go
875 lines (741 loc) · 19.9 KB
/
gli.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
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
// Package gli is a CLI parsing and mapping library.
//
// type globalCmd struct {
// Name string
// Age int
// }
// func (g *globalCmd) Run() error {
// // :
// }
// app := gli.NewWith(&globalCmd{})
// err := app.Run(os.Args)
package gli
import (
//"errors"
"fmt"
"io"
"os"
"path/filepath"
"reflect"
"regexp"
"strconv"
"strings"
"github.com/pkg/errors"
"github.com/shu-go/cliparser"
)
var (
// ErrNotDefined means "an option or a subcommand is not defined in the passed struct".
ErrNotDefined = errors.New("not defined")
// ErrNotRunnable means "Run method is not defined for the passed struct".
ErrNotRunnable = fmt.Errorf("command not runnable")
// ErrOptCanNotBeSet is a reflect related error.
ErrOptCanNotBeSet = fmt.Errorf("option can not be set")
)
// App contains parsing and parsed data.
type App struct {
// global help header
// Name is the app name. default: the name of the executable file
Name string
// Desc is a description of the app
// {{Name}} - {{Desc}}
Desc string
// Usage is a long(multiline) usage text
Usage string
// Version numbers
Version string
// global help footer
// Copyright the app author has
Copyright string
// tag keys
// CliTag is a tag key. default: `cli`
CliTag string
// HelpTag is a tag key. default: `help`
HelpTag string
// UsageTag is a tag key. default: `usage`
UsageTag string
// DefaultTag is a tag key. default: `default`
DefaultTag string
// DefDescTag is a tag key. default: `defdesc`
DefDescTag string
// EnvTag is a tag key. default: `env`
EnvTag string
// RequiredTag is a tag key. default: `required`
RequiredTag string
// DecTypeTag is a tag key that is used to lookup decoder. default: `type`
DecTypeTag string
// MyCommandABC => false(default): "mycommandabc" , true: "my-command-abc"
HyphenedCommandName bool
// MyOptionABC => false(default): "myoptionabc" , true: "my-option-abc"
HyphenedOptionName bool
// OptionsGrouped(default: true) allows -abc may be treated as -a -b -c.
OptionsGrouped bool
DoubleHyphen bool
// SuppressErrorOutput is an option to suppresses on cli parsing error.
SuppressErrorOutput bool
Stdout, Stderr *os.File
// true(default): bool options have --no-xxx options.
// AutoNoBoolOptions also appends --no-xxx descriptions in help doc if .
//
// Options:
// opt1, o1 A bool option
// opt2, o2 A bool option (default: true)
// --no-opt2
// opt1, o1 A bool option
AutoNoBoolOptions bool
parser cliparser.Parser
root *command
}
// New makes main gli instance to parse and invoke hooks.
//
// App bridges between application logic code and CLI definition (your struct).
//
// Next, call Bind() to set the CLI up.
func New() App {
app := App{
parser: cliparser.New(),
CliTag: "cli",
HelpTag: "help",
UsageTag: "usage",
DefaultTag: "default",
DefDescTag: "defdesc",
EnvTag: "env",
RequiredTag: "required",
DecTypeTag: "type",
HyphenedCommandName: false,
HyphenedOptionName: false,
OptionsGrouped: true,
AutoNoBoolOptions: true,
DoubleHyphen: true,
Stdout: os.Stdout,
Stderr: os.Stderr,
}
//HINT
app.parser.HintCommand("help")
app.parser.HintCommand("version")
app.parser.HintLongName("help")
app.parser.HintLongName("version")
if exe, err := os.Executable(); err == nil {
app.Name = filepath.Base(exe)
}
return app
}
// NewWith is New().Bind(ptrSt)
func NewWith(ptrSt interface{}) App {
app := New()
if err := app.Bind(ptrSt); err != nil {
panic(err)
}
return app
}
// Bind updates option/command names with ptrSt.
// Extra commands are cleared.
func (g *App) Bind(ptrSt interface{}) error {
v := reflect.ValueOf(ptrSt)
if v.Kind() != reflect.Ptr && v.Elem().Kind() != reflect.Struct {
panic("not a pointer to a struct")
}
// hmmmm....
if !g.OptionsGrouped {
g.parser.HintNoOptionsGrouped()
}
g.root = &command{
selfV: v,
autoNoBoolOptions: g.AutoNoBoolOptions,
}
return g.scanMeta(v.Type(), g.root)
}
func (g *App) scanMeta(t reflect.Type, cmd *command) error {
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
if t.Kind() != reflect.Struct {
return nil
}
fields := fieldsOf(t)
for i := 0; i < len(fields); i++ {
ft := fields[i].Field
// goto next if not public field
if ft.Name[:1] == strings.ToLower(ft.Name[:1]) {
if ft.Name == "help" || ft.Name == "_" {
tag := ft.Tag
// help description
if tv, ok := tag.Lookup(g.HelpTag); ok && cmd.help == "" {
cmd.help = strings.TrimSpace(tv)
}
// usage description
if tv, ok := tag.Lookup(g.UsageTag); ok && cmd.usage == "" {
cmd.usage = strings.TrimSpace(tv)
}
}
continue
}
tag := ft.Tag
var dectype string
dectype = strings.TrimSpace(tag.Get(g.DecTypeTag))
isbool := ft.Type.Kind() == reflect.Bool
iscmd := ft.Type.Kind() == reflect.Struct && LookupTypeDecoder(ft.Type) == nil && LookupTypeDecoder(dectype) == nil ||
ft.Type.Kind() == reflect.Ptr && ft.Type.Elem().Kind() == reflect.Struct && LookupTypeDecoder(ft.Type.Elem()) == nil && LookupTypeDecoder(dectype) == nil
name := g.arrangeName(ft.Name, iscmd)
names := []string{}
var env string
var required bool
var defvalue string
var defdesc string
var help string
var usage string
var placeholder string
// names
if tv, ok := tag.Lookup(g.CliTag); ok {
if tv == "-" || tv == "" {
continue
}
clinames := strings.Split(tv, ",")
for ni := 0; ni < len(clinames); ni++ {
n := strings.TrimSpace(clinames[ni])
if strings.Contains(n, "=") {
nn := strings.Split(n, "=")
n = strings.TrimSpace(nn[0])
placeholder = strings.TrimSpace(nn[1])
}
names = append(names, n)
}
}
if len(names) == 0 {
names = append(names, strings.ToLower(name))
}
/*
if !iscmd && isbool {
ln := names[0]
for _, n := range names {
if len(ln) < len(n) {
ln = n
}
}
names = append(names, "no-"+ln)
}
*/
defvalue = tag.Get(g.DefaultTag)
defdesc = strings.TrimSpace(tag.Get(g.DefDescTag))
env = strings.TrimSpace(tag.Get(g.EnvTag))
required, err := strconv.ParseBool(strings.TrimSpace(tag.Get(g.RequiredTag)))
if err != nil {
required = false
}
help = strings.TrimSpace(tag.Get(g.HelpTag))
usage = strings.TrimSpace(tag.Get(g.UsageTag))
if iscmd /* f.Kind() == reflect.Struct */ {
sub := &command{
names: names,
help: help,
usage: usage,
fieldIdx: i,
parent: cmd,
autoNoBoolOptions: g.AutoNoBoolOptions,
}
cmd.subs = append(cmd.subs, sub)
//HINT
lname := sub.longestName()
for ni := 0; ni < len(names); ni++ {
// a.out cmd1 cmd2
// cmd2 [cmd1]
g.parser.HintCommand(names[ni], cmd.longestNameStack())
// a.out cmd1 help
// help [cmd1]
g.parser.HintCommand("help", cmd.longestNameStack())
// a.out cmd1 help cmd2
// cmd2 [cmd1 help]
g.parser.HintCommand(names[ni], append(cmd.longestNameStack(), "help"))
//rog.Debug("HintAlias", names[ni], lname)
g.parser.HintAlias(names[ni], lname)
}
err := g.scanMeta(ft.Type, sub)
if err != nil {
return err
}
} else {
opt := &option{
names: names,
env: env,
defValue: defvalue,
defDesc: defdesc,
required: required,
dectype: dectype,
help: help,
tag: tag,
placeholder: placeholder,
fieldIdx: fields[i].Path,
nondefFirstParsing: true,
}
cmd.options = append(cmd.options, opt)
//HINT
lname := opt.longestName()
for ni := 0; ni < len(names); ni++ {
if len(names[ni]) > 1 {
g.parser.HintLongName(names[ni], cmd.longestNameStack())
}
if !isbool {
g.parser.HintWithArg(names[ni], cmd.longestNameStack())
}
g.parser.HintAlias(names[ni], lname)
}
}
}
return nil
}
// AddExtraCommand adds a sub command.
func (g *App) AddExtraCommand(ptrSt interface{}, names, help string, inits ...extraCmdInit) {
if g.root == nil {
panic("need Bind or use NewWith")
}
v := reflect.ValueOf(ptrSt)
if v.Kind() != reflect.Ptr && v.Elem().Kind() != reflect.Struct {
panic("not a pointer to a struct")
}
if len(names) == 0 {
panic("name the extra command")
}
nameslice := strings.Split(names, ",")
for i := 0; i < len(nameslice); i++ {
nameslice[i] = strings.TrimSpace(nameslice[i])
g.parser.HintCommand(nameslice[i])
g.parser.HintCommand(nameslice[i], []string{"help"})
}
cmd := command{
names: nameslice,
help: help,
selfV: v,
parent: g.root,
autoNoBoolOptions: g.AutoNoBoolOptions,
}
lname := cmd.longestName()
for ni := 0; ni < len(cmd.names); ni++ {
g.parser.HintAlias(cmd.names[ni], lname)
}
for _, init := range inits {
init(&cmd)
}
err := g.scanMeta(v.Type(), &cmd)
if err != nil {
panic(err.Error())
}
g.root.extras = append(g.root.extras, &cmd)
}
// Parse parses args and returns results.
// tgt (interface{}) : a resultant struct
// tgtargs ([]string) : args of last subcommand
// err : parsing error
func (g *App) Parse(args []string) (tgt interface{}, tgtargs []string, err error) {
if g.root == nil {
panic("need Bind or use NewWith")
}
return g.exec(args, false)
}
// Run parses args and calls Run method of a subcommand.
func (g *App) Run(args []string) error {
if g.root == nil {
panic("need Bind or use NewWith")
}
_, _, err := g.exec(args, true)
return err
}
func (g *App) exec(args []string, doRun bool) (tgt interface{}, tgtargs []string, appRunErr error) {
cmd := g.root
if !g.DoubleHyphen {
g.parser.HintDisableDoubleHyphen()
}
if len(args) == len(os.Args) && len(args) > 0 && args[0] == os.Args[0] {
args = make([]string, len(os.Args)-1)
copy(args, os.Args[1:])
}
cmdStack := []*command{cmd}
cmd.setMembersReferMe()
cmd.setDefaultValues(g.DecTypeTag)
_, defErr := g.call("Init", cmd.selfV, cmdStack, cmd.args)
if defErr != nil {
if !g.SuppressErrorOutput {
fmt.Fprintf(g.Stderr, "%v\n", defErr)
}
return nil, nil, defErr
}
helpMode := false
g.parser.Reset()
g.parser.Feed(args)
if err := g.parser.Parse(); err != nil {
if !g.SuppressErrorOutput {
fmt.Fprintf(g.Stderr, "%v\n", err)
}
return nil, nil, err
}
for {
c := g.parser.GetComponent()
if c == nil {
break
}
if c.Name == "help" {
helpMode = true
continue
}
if len(cmdStack) == 1 && (c.Name == "version") {
fmt.Fprintln(g.Stdout, g.Version)
return nil, nil, nil
}
switch c.Type {
case cliparser.Arg:
cmd.args = append(cmd.args, c.Arg)
case cliparser.Option:
//rog.Debug(c.Name)
o := cmd.findOptionExact(c.Name)
// "--no-bool" ?
if g.AutoNoBoolOptions && o == nil && strings.HasPrefix(c.Name, "no-") {
o = cmd.findOptionExact(c.Name[3:])
fv := o.ownerV.Elem().FieldByIndex(o.fieldIdx)
if o != nil && fv.Type().Kind() == reflect.Bool {
c.Name = c.Name[3:]
c.Arg = "false"
}
}
if o == nil {
if !g.SuppressErrorOutput {
fmt.Fprintf(g.Stdout, "option %q %v\n\n", c.Name, ErrNotDefined)
var candidates []string
for oi := 0; oi < len(cmd.options); oi++ {
names := cmd.options[oi].names
for ni := 0; ni < len(names); ni++ {
if strings.HasPrefix(names[ni], c.Name) {
candidates = append(candidates, names[ni])
break
} else if re, err := regexp.Compile("[" + names[ni] + "]"); err == nil {
if len(re.ReplaceAllLiteralString(c.Name, "")) <= len(c.Name)/10 {
candidates = append(candidates, names[ni])
break
}
}
}
}
if len(candidates) > 0 {
fmt.Fprintf(g.Stdout, " maybe %v ?\n\n", candidates)
}
g.Help(g.Stdout)
}
return nil, nil, errors.Wrap(ErrNotDefined, "option "+c.Name)
}
fv := o.ownerV.Elem().FieldByIndex(o.fieldIdx)
err := setOptValue(fv, c.Arg, o.tag, g.DecTypeTag, false, &o.nondefFirstParsing)
if err != nil {
if !g.SuppressErrorOutput {
fmt.Fprintf(g.Stderr, "option %q: %v\n\n", c.Name, err)
g.Help(g.Stdout)
}
return nil, nil, err
}
o.assigned = true
case cliparser.Command: // may be an arg
if len(cmd.subs)+len(cmd.extras) == 0 {
cmd.args = append(cmd.args, c.Name) // command name? -> no, it's an arg
continue
}
sub, isextra := cmd.findCommandExact(c.Name)
if sub == nil {
if !g.SuppressErrorOutput {
//rog.Debug("notdefined")
fmt.Fprintf(g.Stderr, "command %q %v\n\n", c.Name, ErrNotDefined)
g.Help(g.Stdout)
}
//rog.Debug("notdefined")
return nil, nil, errors.Wrap(ErrNotDefined, "command "+c.Name)
}
if !isextra {
sub.ownerV = cmd.selfV
subt := cmd.selfV.Type().Elem().Field(sub.fieldIdx).Type
if subt.Kind() == reflect.Ptr {
sub.selfV = reflect.New(subt.Elem())
cmd.selfV.Elem().Field(sub.fieldIdx).Set(sub.selfV)
} else {
sub.selfV = cmd.selfV.Elem().Field(sub.fieldIdx).Addr()
}
}
cmd = sub
cmdStack = append(cmdStack, cmd)
cmd.setMembersReferMe()
cmd.setDefaultValues(g.DecTypeTag)
_, defErr := g.call("Init", cmd.selfV, cmdStack, cmd.args)
if defErr != nil {
return nil, nil, defErr
}
}
}
if helpMode {
funcName := "Help"
callErr, helpErr := g.call(funcName, cmd.selfV, cmdStack, cmd.args)
if callErr == ErrNotRunnable {
callErr, helpErr = g.call(funcName, g.root.selfV, cmdStack, g.root.args)
}
if callErr != nil {
if cmd.selfV == g.root.selfV {
g.Help(g.Stdout)
} else {
cmd.outputHelp(g.Stdout)
}
}
return nil, nil, helpErr
}
err := errorIfEmptyRequired(cmdStack)
if err != nil {
if !g.SuppressErrorOutput {
fmt.Fprintln(g.Stderr, err)
g.Help(g.Stdout)
}
return nil, nil, err
}
// call Before->Run->After
// Before/After
// Before: root->sub->subsub
// After: subsub->sub->root *deferred*
if doRun {
for ci := 0; ci < len(cmdStack); ci++ {
callErr, beforeErr := g.call("Before", cmdStack[ci].selfV, cmdStack, cmdStack[ci].args)
if callErr == nil && beforeErr != nil {
if !g.SuppressErrorOutput {
fmt.Fprintf(g.Stderr, "%v\n", beforeErr)
cmdStack[ci].outputHelp(g.Stdout)
}
return nil, nil, beforeErr
}
defer func(cmd *command) {
// After()
callErr, afterErr := g.call("After", cmd.selfV, cmdStack, cmd.args)
if callErr != nil && appRunErr == nil {
appRunErr = afterErr
}
}(cmdStack[ci])
}
}
if doRun {
funcName := "Run"
callErr, runErr := g.call(funcName, cmd.selfV, cmdStack, cmd.args)
if callErr != nil {
if cmd == g.root {
g.Help(g.Stdout)
} else {
cmd.outputHelp(g.Stdout)
}
//return ErrNotDefined
return nil, nil, nil
}
if runErr != nil {
if !g.SuppressErrorOutput {
fmt.Fprintf(g.Stderr, "%v\n", runErr)
}
return nil, nil, runErr
}
}
return cmd.selfV.Interface(), cmd.args, nil
}
func (g *App) arrangeName(name string, iscmd bool) string {
if iscmd && !g.HyphenedCommandName {
return name
}
if !iscmd && !g.HyphenedOptionName {
return name
}
result := make([]rune, 0, len(name))
prevU := false
for i, c := range name {
if i != 0 && 'A' <= c && c <= 'Z' {
if !prevU {
result = append(result, '-')
}
prevU = true
} else {
prevU = false
}
result = append(result, c)
}
return string(result)
}
func (g *App) call(funcName string, cmd reflect.Value, cmdStack []*command, args []string) (callErr, userErr error) {
methv := cmd.MethodByName(funcName)
if methv == (reflect.Value{}) {
return ErrNotRunnable, nil
}
var argv []reflect.Value
for i := 0; i < methv.Type().NumIn(); i++ {
in := methv.Type().In(i)
if in.Kind() == reflect.Struct {
st := findStructByType(cmdStack, in)
if st != nil {
argv = append(argv, reflect.ValueOf(st).Elem())
} else if reflect.TypeOf(g) == in {
argv = append(argv, reflect.ValueOf(*g))
} else {
return ErrNotRunnable, nil
}
} else if in.Kind() == reflect.Ptr && in.Elem().Kind() == reflect.Struct {
st := findStructByType(cmdStack, in)
if st != nil {
argv = append(argv, reflect.ValueOf(st))
} else if reflect.TypeOf(g) == in {
argv = append(argv, reflect.ValueOf(g))
} else {
return ErrNotRunnable, nil
}
} else if in.Kind() == reflect.Slice && in.Elem().Kind() == reflect.String {
// args
argv = append(argv, reflect.ValueOf(args))
} else {
panic("*struct, struct or []string are allowed")
}
}
retv := methv.Call(argv)
return nil, returnErr(retv)
}
func returnErr(retv []reflect.Value) error {
if len(retv) == 0 {
return nil
}
mayerr := retv[len(retv)-1].Interface()
if err, ok := mayerr.(error); ok {
return err
}
return nil
}
func findStructByType(stack []*command, typ reflect.Type) interface{} {
for i := len(stack) - 1; i >= 0; i-- {
e := stack[i].selfV
ei := stack[i].selfV.Interface()
et := e.Type()
if et == typ || et.Kind() == reflect.Ptr && et.Elem() == typ {
return ei
}
}
return nil
}
func setOptValue(opt reflect.Value, value string, tag reflect.StructTag, dectypeTag string, parsingDef bool, nondefFirstParsing *bool) error {
if opt.Type().Kind() == reflect.Ptr {
var pv reflect.Value
if opt.IsNil() {
pv = reflect.New(opt.Type().Elem())
} else {
pv = opt
}
err := setOptValue(pv.Elem(), value, tag, dectypeTag, parsingDef, nondefFirstParsing)
if err != nil {
return err
}
opt.Set(pv)
return nil
}
ndfp := *nondefFirstParsing
if !parsingDef && *nondefFirstParsing {
*nondefFirstParsing = false
}
dec := LookupTypeDecoder(strings.TrimSpace(tag.Get(dectypeTag)))
if dec != nil {
return dec(value, opt, tag, ndfp)
} else {
dec = LookupTypeDecoder(opt.Type())
if dec != nil {
err := dec(value, opt, tag, ndfp)
if err == nil {
return nil
}
}
}
switch opt.Kind() {
case reflect.String:
opt.Set(reflect.ValueOf(value))
return nil
case reflect.Bool:
b, err := strconv.ParseBool(value)
if err != nil {
return err
}
opt.Set(reflect.ValueOf(b))
return nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
size := int(opt.Type().Size())
i, err := strconv.ParseInt(value, 10, size*8)
if err != nil {
return err
}
opt.Set(reflect.ValueOf(i).Convert(opt.Type()))
return nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
size := int(opt.Type().Size())
i, err := strconv.ParseUint(value, 10, size*8)
if err != nil {
return err
}
opt.Set(reflect.ValueOf(i).Convert(opt.Type()))
return nil
case reflect.Float32, reflect.Float64:
size := int(opt.Type().Size())
f, err := strconv.ParseFloat(value, size*8)
if err != nil {
return err
}
opt.Set(reflect.ValueOf(f).Convert(opt.Type()))
return nil
default:
//nop
}
return ErrOptCanNotBeSet
}
func errorIfEmptyRequired(cmdStack []*command) error {
for i := len(cmdStack) - 1; i >= 0; i-- {
c := cmdStack[i]
for _, o := range c.options {
if !o.required {
continue
}
if !o.assigned {
return errors.New("option " + o.longestName() + " is required")
}
}
}
return nil
}
// Help displays help messages.
func (g App) Help(w io.Writer) {
if g.root == nil {
panic("need Bind or use NewWith")
}
appinfo := g.Name
if g.Desc != "" {
appinfo += " - " + g.Desc
}
if g.Version != "" {
appinfo += "(" + g.Version + ")"
}
if appinfo != "" {
fmt.Fprintf(w, "%s\n", appinfo)
}
g.root.usage = g.Usage
g.root.outputHelp(w)
fmt.Fprintln(w, `
Help sub commands:
help `+g.Name+` help subcommnad subsubcommand
version show version`)
if g.Copyright != "" {
fmt.Fprintf(w, "\n%s\n", g.Copyright)
}
}
type fieldAndPath struct {
Field reflect.StructField
Path []int
}
func fieldsOf(t reflect.Type) []fieldAndPath {
result := make([]fieldAndPath, 0, t.NumField())
for i := 0; i < t.NumField(); i++ {
ft := t.Field(i)
if ft.Type.Kind() == reflect.Struct && ft.Anonymous {
for j := 0; j < ft.Type.NumField(); j++ {
jft := ft.Type.Field(j)
result = append(result, fieldAndPath{Field: jft, Path: []int{i, j}})
}
} else {
result = append(result, fieldAndPath{Field: ft, Path: []int{i}})
}
}
return result
}