-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder.go
1249 lines (1143 loc) · 31.4 KB
/
builder.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
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package scli
import (
"errors"
"fmt"
"os"
"reflect"
"sort"
"strconv"
"strings"
"sync/atomic"
"unsafe"
)
// Parse is the interface for custom type. Any type implemented this interface
// can be used as an argument/option in scli's definition struct.
type Parse interface {
FromString(s string) error
Example() string
}
// ParseOnce is an interface for types that are Parse, but the FromString() of
// the type should be called at most once. This restriction is vital for
// implementing types like ConfigFile. Lets see why. First, some definitions.
//
// Definitions:
//
// 1. Stateful
// Assuming all the outside running environment is unchanged, for instance t,
// calling t.FromString() once and calling t.FromString(..) for the second time
// will result in different behavior. One example is FromString contains logics
// based on a global variable which is inherited next time, then the second call
// will not trigger the same logic.
//
// 2. Impure
// For instance t, the result of t.FromString(..) not only dependent on the
// code of FromString it self, and may produce different outcome based on the
// running environment. One example is FromString read a file, and file may or
// may not exist on different environment.
//
// For ConfigFile type, apparently, it's Impure because it relies on files which
// is not part of the code.
// For ConfigFile that can live update it's value when file is changed, the type
// is Stateful. A watcher is created for the first time a FromString() success
// and when file changes, FromString is called again to load new data, but not
// creating new watchers.
//
// For ParseOnce, scli will not check default value nor example value, and
// Parser.Parse()FromString is called only once at parsing.
// If you think your type T is ParseOnce, use MarkOnce[T] to convert your Parse
// type to ParseOnce
type ParseOnce interface {
Parse
// make it private so users cannot implement their own ParseOnce
statefulOrImpure()
}
// EXPERIMENTAL: MarkOnce[T]: convert Parse to ParseOnce
// for type T that *T is Parse
//
// struct {
// t T
// }
//
// should now define:
//
// type TOnce = scli.MarkOnce[*T]
//
// struct {
// tOnce TOnce
// }
//
// and get T by call tOnce.Get()
type MarkOnce[T Parse] struct {
called atomic.Bool
t T
}
func (m *MarkOnce[T]) FromString(s string) error {
if m.called.CompareAndSwap(false, true) {
valTy := reflect.TypeOf(m.t).Elem()
tField := reflect.ValueOf(m).Elem().Field(1)
// convert unexported field to a reflect.Value that can set
tField = reflect.NewAt(tField.Type(), unsafe.Pointer(tField.UnsafeAddr())).Elem()
tField.Set(reflect.New(valTy))
return m.t.FromString(s)
}
panic("FromString() of ParseOnce is called twice")
}
func (m *MarkOnce[T]) Example() string {
return m.t.Example()
}
func (m *MarkOnce[T]) Get() *T {
return &m.t
}
func (m *MarkOnce[T]) statefulOrImpure() {}
var (
ErrIsHelp = errors.New("argument is help message")
)
const ( // build time errors
errParseDefault = `error parsing default value "%s" of field "%s", error: %w`
errHelpIsReserved = `"help" is a reserved word, please change option name of "%s"`
errNotImplParse = "*%s did not implement `Parse` interface, at field %s"
errCmdRedefined = `subcommand %s is redefined, at field %s`
errOptionRedefined = `option %s is redefined, at field %s`
errArgRedefined = `argument %s is redefined, at field %s`
errBothArgAndCommand = `command %s and argument %s both defined, which is not allowed,` +
` at field %s and %s`
errNotStructPtr = "type %v of field %s must be *struct{...} to represent a subcommand, " +
"or type `Parse` to represent a custom type"
errNotValidExample = "Example() of custom type *%s cannot parsed by FromString(..) " +
"at field %s. If you are sure parsing Example() may fail and it is AS EXPECTED, " +
"consider use MarkOnce, see MarkOnce"
errArgNoDefault = "positional argument type %s at field %s requires a default value " +
"because a previous argument has default value"
errSliceArgHasDefault = "slice argument cannot have default value: argument %s at field %s"
errMultipleArgSlice = `more than one slice type argument: %s at field %s`
errArgAfterSlice = `cannot define argument after slice argument: %s at field %s`
errSliceArgAfterDefaultArg = "cannot define slice argument after optional(has default)" +
" argument: %s at field %s"
)
const ( // runtime errors
errOptionNoValue = `no value provided for option "--%s" in %s`
errOptionNotFound = `option "--%s" is required in "%s" but not provided`
errOptionDuplicate = `option "--%s" is given more than once`
errNoOption = `option "--%s" provided in "%s" but not defined`
errNoSubcommand = `subcommand "%s" provided in "%s" but not defined`
errRemainUnparsed = `parse success until "%s", maybe remove "%s" and try again?`
errParseOption = `error parsing argument of option "--%s %s" error: %w`
errParsePositionalArg = `error parsing positional argument "%s" value %s, error: %w`
errTooManyArgs = `error parsing positional argument: too many args, expect %d, given %d args`
errArgNotFound = `argument <%s> is required in "%s" but not provided`
)
func maxInt(a, b int) int {
if a < b {
return b
}
return a
}
// type inputType int
// const (
// notOption inputType = iota
// validOption
// maybeOption
type parseCmdResult struct {
rv reflect.Value
isHelpCmd bool
helpText string
}
type parseArgResult struct {
rv reflect.Value
}
func (p parseCmdResult) IsHelp() bool {
return p.isHelpCmd
}
type parseCmdError struct {
// err is the error causing parse failed
// usage is the correct usage, will be printed when
// error happens
err error
usage string
}
func (pe *parseCmdError) Error() string {
return pe.err.Error()
}
type parseCmdFn func([]string) (parseCmdResult, *parseCmdError)
// TODO: parseArgResult is not used now. The set work is now done
// by parse function it self. The parseArgResult.rv is not used anymore.
// Maybe change signature return error only
type parseArgFn func([]string) (parseArgResult, error)
type Parser[T any] struct {
parse parseCmdFn
help string
checkFn func(T) error
}
func (p Parser[T]) Checker(checkFn func(T) error) Parser[T] {
p.checkFn = checkFn
return p
}
func (p Parser[T]) ParseArg(s string) (zero T, err error) {
r, e := p.parse(strings.Fields(s))
if e != nil {
return zero, e
}
if r.IsHelp() {
return zero, ErrIsHelp
}
res := r.rv.Interface().(T)
if p.checkFn != nil {
return res, p.checkFn(res)
}
return res, nil
}
func (p Parser[T]) Parse() T {
r, e := p.parse(os.Args[1:])
if e != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", e)
fmt.Println()
fmt.Println("The usage is:")
fmt.Print(e.usage)
os.Exit(2)
}
if r.IsHelp() {
fmt.Print(r.helpText)
os.Exit(0)
}
res := r.rv.Interface().(T)
if p.checkFn != nil {
if err := p.checkFn(res); err != nil {
fmt.Fprintf(os.Stderr, "parse ok, but check failed: %v\n", err)
fmt.Println()
fmt.Println("The usage is:")
fmt.Print(r.helpText)
os.Exit(2)
}
}
return res
}
func (p Parser[T]) Help() string {
return p.help
}
func BuildParser[T any](u *T) Parser[T] {
parse, topHelp := checkTopAndBuildParseFn(u, os.Args[0])
return Parser[T]{parse: parse, help: topHelp}
}
func checkTopAndBuildParseFn(u any, execName string) (parseCmdFn, string) {
if err := checkType(u); err != nil {
panic(err)
}
return buildParseCmdFn(
reflect.ValueOf(u).Elem().Type().Name(),
execName,
reflect.ValueOf(u),
)
}
func checkType(u any) error {
ptr := reflect.ValueOf(u) // any -> *T
if ptr.Kind() != reflect.Pointer || ptr.IsNil() {
return errors.New("in BuildParser(v), type of v must be non nil *struct{...}")
}
argStructPtr := ptr.Elem() // *T -> T
if argStructPtr.Kind() != reflect.Struct {
return errors.New("in BuildParser(v), type of v must be *struct{...}")
}
return nil
}
const (
// TODO: use map[kwType]?
boolArgLen = 1
argLen = 2
subcommandLen = 1
)
type kwType int
const (
boolArg kwType = iota
valArg
sliceArg
)
// cmdInfo contains general information of a command
// A command can have options, arguments and sub-commands
// cmdInfo is used to build parse functions
type cmdInfo struct {
options map[string]optionInfo
subcmds map[string]subcmdInfo
args []argInfo // preserving order of positional arguments
}
func newCmdInfo() cmdInfo {
return cmdInfo{
options: map[string]optionInfo{},
subcmds: map[string]subcmdInfo{},
args: []argInfo{},
}
}
// optionInfo contains information for options (--option value)
type optionInfo struct {
parseFn parseArgFn
defName string // name of field in struct definition
consumeLen int
ty kwType // type of option argument. boolean or normal argument
usage string
defaultVal *string // non-nil if argument has a default value
example *string // non-nil if argument is a custom type
// the details of Parse interface if this is a custom type
parseDetail parseStatus
}
// argInfo contains information for positional arguments
// eg: cmd arg1 arg2
type argInfo struct {
cliName string // argument name in CLI
parseFn parseArgFn
defName string // name of field in struct definition
ty kwType // type of argument
usage string
defaultVal *string // non-nil if argument has a default value
example *string // non-nil if argument is a custom type
// the details of Parse interface if this is a custom type
parseDetail parseStatus
}
// define argOrOption to make call simpler
type argOrOption struct {
isArg bool
// common for both argument and option
parseFn parseArgFn
defName string // name of field in struct definition
usage string
example *string // non-nil if custom type
// only valid if isArg == false, e.g. is option
consumeLen int
ty kwType // type of option argument. boolean or normal argument
defaultVal *string // non-nil if argument has a default value
// the details of Parse interface if this is a custom type
parseDetail parseStatus
}
// subcmdInfo contains information of a subcommand
type subcmdInfo struct {
parseFn parseCmdFn
defName string
usage string
}
func buildParseCmdFn(
fieldChain string, viewNameChain string, u reflect.Value,
) (p parseCmdFn, usageText string) {
argStructPtr := u.Elem() // *T -> T
structDef := u.Type().Elem()
cmd, err := buildArgAndCommandList(
fieldChain, viewNameChain, structDef, argStructPtr,
)
if err != nil {
panic(err)
}
err = validateArgAndCommand(fieldChain, cmd)
if err != nil {
panic(err)
}
// parseOption try to parse input as an option
// if ok, set argStruct and returns (remaining input, nil)
// if failed, returns (input unchanged, error)
parseOption := func(cmd cmdInfo, helpText string, input []string) (
_rest []string, _err *parseCmdError,
) {
first, _ := input[0], input[1:]
option := strings.Trim(first, "-/")
t, ok := cmd.options[option]
if !ok {
return input, &parseCmdError{
err: fmt.Errorf(errNoOption, option, viewNameChain),
usage: helpText,
}
}
if t.ty == boolArg {
// bool is special, no need to parse, set value now
argStructPtr.FieldByName(t.defName).Set(
reflect.ValueOf(
!strings.HasPrefix(strings.Trim(first, "-"), "/"),
),
)
} else {
if len(input) < t.consumeLen {
return input, &parseCmdError{
err: fmt.Errorf(
errOptionNoValue, option, viewNameChain,
),
usage: helpText,
}
}
val := input[1:t.consumeLen] // ["--option", "arg"], we take "arg"
_, err := t.parseFn(val)
if err != nil {
return input, &parseCmdError{
err: fmt.Errorf(
errParseOption,
option, strings.Join(val, ""), err,
),
usage: helpText,
}
}
}
return input[t.consumeLen:], nil
}
// parseArg try to parse input as an argument
// if ok, set argStruct and returns (remaind input, nil)
// if failed, returns (input unchanged, error)
parseArg := func(
cmd cmdInfo, helpText string,
argNumber int, input []string,
) (
_rest []string, _err *parseCmdError,
) {
first, _ := input[0], input[1:]
if len(cmd.args) <= argNumber {
return input, &parseCmdError{
err: fmt.Errorf(
errTooManyArgs,
len(cmd.args),
len(input)+len(cmd.args),
),
usage: helpText,
}
}
t := cmd.args[argNumber]
if t.ty == sliceArg {
_, err := t.parseFn(input)
if err != nil {
return input, &parseCmdError{
err: fmt.Errorf(
errParsePositionalArg,
t.cliName, strings.Join(input, " "), err,
),
usage: helpText,
}
}
return []string{}, nil // slice arg consumes all input
}
// argument is not slice
_, err := t.parseFn([]string{first})
if err != nil {
return input, &parseCmdError{
err: fmt.Errorf(
errParsePositionalArg,
t.cliName, first, err,
),
usage: helpText,
}
}
return input[1:], nil
}
// parseSubcmd try to parse input as an subcommand
// If ok, set argStruct and returns (_, parseCmdResult, nil)
// If failed, returns (_, nil, error)
// If input is a subcommand help or subcommand error, directReturn will be true
// caller should return immediately
parseSubcmd := func(cmd cmdInfo, helpText string, input []string) (
_directReturn bool, _result parseCmdResult, _err *parseCmdError,
) {
first, rest := input[0], input[1:]
t, ok := cmd.subcmds[first]
if !ok {
return true, parseCmdResult{}, &parseCmdError{
err: fmt.Errorf(
errNoSubcommand, first, viewNameChain,
),
usage: helpText,
}
}
r, err := t.parseFn(rest)
if err != nil {
return true, r, err
}
if r.IsHelp() {
return true, r, nil
}
argStructPtr.FieldByName(t.defName).Set(r.rv.Addr())
return false, parseCmdResult{}, nil
}
parse := func(input []string) (parseCmdResult, *parseCmdError) {
encounter := make(map[string]bool)
argsProcessed := 0
helpText := makeUsageText(viewNameChain, cmd)
noMoreOption := false
// parse options and args
for len(input) > 0 {
first, _ := input[0], input[1:]
// if input starts with "-" but not valid option,
// it may be a argument value
if strings.HasPrefix(first, "-") && !noMoreOption {
realOption := strings.Trim(first, "-/")
if realOption == "help" {
return parseCmdResult{
helpText: helpText,
isHelpCmd: true,
}, nil
}
if _, ok := encounter[realOption]; ok {
// encoutered before
return parseCmdResult{}, &parseCmdError{
err: fmt.Errorf(errOptionDuplicate, realOption),
usage: helpText,
}
}
remain, parseOptionErr := parseOption(cmd, helpText, input)
if parseOptionErr != nil {
remain, parseArgErr := parseArg(cmd, helpText, argsProcessed, input)
if parseArgErr != nil {
// TODO: tell user it's not option and also not arg
return parseCmdResult{}, parseOptionErr
}
// the input parsed success, from now on, no more options
// all input should be args
noMoreOption = true
input = remain
argsProcessed++
} else {
encounter[realOption] = true
input = remain
}
} else if cmd.HasArgs() {
if len(cmd.args) <= argsProcessed {
return parseCmdResult{}, &parseCmdError{
err: fmt.Errorf(
errTooManyArgs,
len(cmd.args),
len(input)+len(cmd.args),
),
usage: helpText,
}
}
noMoreOption = true
remain, err := parseArg(cmd, helpText, argsProcessed, input)
if err != nil {
return parseCmdResult{}, err
}
input = remain
argsProcessed++
} else {
break
}
}
if len(input) > 0 {
if cmd.HasSubcmds() {
directReturn, r, err := parseSubcmd(cmd, helpText, input)
if directReturn {
return r, err
}
if err != nil {
return parseCmdResult{}, err
}
} else {
// if input remains and no available sub commands, it's a error
return parseCmdResult{}, &parseCmdError{
err: fmt.Errorf(
errRemainUnparsed,
strings.Join(input, " "), strings.Join(input, " "),
),
usage: helpText,
}
}
}
// check all arguments are given
// if struct did not define argument, cmd.args will be empty
// below check will pass
if cmd.HasArgs() {
for _, arg := range cmd.args[argsProcessed:] {
if arg.defaultVal != nil {
_, err := arg.parseFn([]string{*arg.defaultVal})
if err != nil {
panic(
"default values are validated before. " +
"Cannot parse error at parse",
)
}
} else if arg.ty == sliceArg {
// sliceArg can be omitted to represent empty slice
} else {
return parseCmdResult{}, &parseCmdError{
err: fmt.Errorf(
errArgNotFound,
arg.cliName, viewNameChain,
),
usage: helpText,
}
}
}
}
// check all required options are given
for optionName, info := range cmd.options {
if _, argFound := encounter[optionName]; !argFound {
if info.defaultVal != nil {
_, err := info.parseFn([]string{*info.defaultVal})
if err != nil {
panic(
"default values are validated before. " +
"Cannot parse error at parse",
)
}
} else {
return parseCmdResult{}, &parseCmdError{
err: fmt.Errorf(
errOptionNotFound,
optionName,
viewNameChain,
),
usage: helpText,
}
}
}
}
return parseCmdResult{
rv: argStructPtr,
helpText: helpText,
}, nil
}
return parse, makeUsageText(viewNameChain, cmd)
}
type mapString[V any] struct {
key string
val V
}
func sortMap[V any](m map[string]V) []mapString[V] {
r := make([]mapString[V], len(m))
i := 0
for k, v := range m {
r[i] = mapString[V]{k, v}
i++
}
sort.Slice(r, func(i, j int) bool {
return r[i].key < r[j].key
})
return r
}
type cliInfo struct {
options map[string]optionInfo
commands map[string]subcmdInfo
args []optionInfo
}
func buildArgAndCommandList(
fieldChain string,
viewNameChain string,
structDef reflect.Type, argStructPtr reflect.Value,
) (cmdInfo, error) {
// (options map[string]optionInfo, commands map[string]subcmdInfo, err error) {
baseCmd := newCmdInfo()
for i := 0; i < argStructPtr.NumField(); i++ {
defField := structDef.Field(i)
defName := defField.Name
defaultVal := func(fieldDef reflect.StructField) *string {
defaultValStr, hasDefault := fieldDef.Tag.Lookup("default")
if hasDefault {
return &defaultValStr
}
return nil
}(defField)
valField := argStructPtr.Field(i)
currentFieldChain := fmt.Sprintf("%s.%s", fieldChain, defName)
isArg, cliName := func() (bool, string) {
optionName := defField.Tag.Get("flag") // for compatibility, use `flag`
argName := defField.Tag.Get("arg")
if argName == "" {
argName = defName
}
if optionName != "" {
return false, optionName
}
return true, argName
}() // name used in cli
usage := defField.Tag.Get("usage")
p, parseDetail := hasCustomParser(valField)
if parseDetail == exampleCannotParse {
return baseCmd, fmt.Errorf(
errNotValidExample,
valField.Type(),
currentFieldChain,
)
}
if parseDetail == validParse || parseDetail == parseOnce {
// has implemented Parse
if err := baseCmd.AddArgOrOption(
cliName,
argOrOption{
isArg: isArg,
defName: defName,
parseFn: p(valField),
usage: usage,
example: customExampleFor(valField),
ty: valArg,
consumeLen: argLen,
defaultVal: defaultVal,
parseDetail: parseDetail,
},
currentFieldChain,
); err != nil {
return baseCmd, err
}
} else {
switch {
case valField.Kind() == reflect.Slice:
ptrToElem := reflect.New(valField.Type().Elem())
parseElemFn, elemExample, parseDetail, err :=
func() (
func(reflect.Value) parseArgFn,
*string, // example
parseStatus, // if type is custom, this contains the details
error,
) {
p, parseDetail := hasCustomParser(
reflect.New(ptrToElem.Type().Elem()).Elem(),
)
if parseDetail == exampleCannotParse {
return nil, nil, exampleCannotParse,
fmt.Errorf(
errNotValidExample,
ptrToElem.Type().Elem(),
currentFieldChain,
)
}
if parseDetail == validParse || parseDetail == parseOnce {
// has implemented Parse
return p, customExampleFor(
reflect.New(ptrToElem.Type().Elem()).Elem(),
), parseDetail, nil
} else {
switch ptrToElem.Elem().Type() {
case reflect.TypeOf(string("")):
strStr := "str"
return parseString, &strStr, notParse, nil
case reflect.TypeOf(bool(false)):
falseStr := "false"
return parseBool, &falseStr, notParse, nil
case reflect.TypeOf(int(0)):
intStr := "0"
return parseInt, &intStr, notParse, nil
case reflect.TypeOf(float64(0)):
float64Str := "3.14"
return parseFloat64, &float64Str, notParse, nil
default:
return nil, nil, notParse, fmt.Errorf(
errNotImplParse,
ptrToElem.Type(),
currentFieldChain,
)
}
}
}()
if err != nil {
return baseCmd, err
}
parseFn, example := func() (parseArgFn, *string) {
if isArg {
parseFn := func(s []string) (parseArgResult, error) {
// slice argument are passed separated by space
if len(s) == 0 {
return parseArgResult{
rv: reflect.MakeSlice(valField.Type(), 0, 0),
}, nil
}
valField.Set(reflect.MakeSlice(
valField.Type(), len(s), len(s),
))
for i, si := range s {
_, err := parseElemFn(valField.Index(i))([]string{si})
if err != nil {
return parseArgResult{}, err
}
}
return parseArgResult{rv: valField}, nil
}
return parseFn, elemExample
} else {
parseFn := func(s []string) (parseArgResult, error) {
str := strings.Join(s, "")
if str == "" {
return parseArgResult{
rv: reflect.MakeSlice(valField.Type(), 0, 0),
}, nil
}
strSlice := strings.Split(str, ",")
valField.Set(reflect.MakeSlice(
valField.Type(), len(strSlice), len(strSlice),
))
for i, si := range strSlice {
// newElem := reflect.New(ptrToElem.Type().Elem()).Elem()
_, err := parseElemFn(valField.Index(i))([]string{si})
if err != nil {
return parseArgResult{}, err
}
}
return parseArgResult{rv: valField}, nil
}
example := func(elemExample *string) *string {
if elemExample != nil {
res := fmt.Sprintf(
"%v,%v,...",
*elemExample, *elemExample,
)
return &res
}
return nil
}(elemExample)
return parseFn, example
}
}()
if err := baseCmd.AddArgOrOption(
cliName,
argOrOption{
isArg: isArg,
defName: defName,
parseFn: parseFn,
usage: usage,
example: example,
ty: sliceArg,
consumeLen: argLen, // only option use this length
defaultVal: defaultVal,
parseDetail: parseDetail,
},
currentFieldChain,
); err != nil {
return baseCmd, err
}
case valField.Type() == reflect.TypeOf(string("")):
if err := baseCmd.AddArgOrOption(
cliName,
argOrOption{
isArg: isArg,
defName: defName,
parseFn: parseString(valField),
usage: usage,
ty: valArg,
consumeLen: argLen,
defaultVal: defaultVal,
},
currentFieldChain,
); err != nil {
return baseCmd, err
}
case valField.Type() == reflect.TypeOf(bool(false)):
if err := baseCmd.AddArgOrOption(
cliName,
argOrOption{
isArg: isArg,
defName: defName,
parseFn: parseBool(valField),
usage: usage,
ty: boolArg,
consumeLen: boolArgLen,
defaultVal: defaultVal,
},
currentFieldChain,
); err != nil {
return baseCmd, err
}
case valField.Type() == reflect.TypeOf(int(0)):
if err := baseCmd.AddArgOrOption(
cliName,
argOrOption{
isArg: isArg,
defName: defName,
parseFn: parseInt(valField),
usage: usage,
ty: valArg,
consumeLen: argLen,
defaultVal: defaultVal,
},
currentFieldChain,
); err != nil {
return baseCmd, err
}
case valField.Type() == reflect.TypeOf(float64(0)):
if err := baseCmd.AddArgOrOption(
cliName,
argOrOption{
isArg: isArg,
defName: defName,
parseFn: parseFloat64(valField),
usage: usage,
ty: valArg,
consumeLen: argLen,
defaultVal: defaultVal,
},
currentFieldChain,
); err != nil {
return baseCmd, err
}
case valField.Kind() == reflect.Pointer: // expect a sub-command definition
// should be *struct{...}
fieldType := valField.Type().Elem() // reflect.Type struct{...}
if fieldType.Kind() != reflect.Struct {
return baseCmd, fmt.Errorf(
errNotStructPtr,
valField.Type(),
currentFieldChain,
)
}
instance := reflect.New(fieldType) // reflect.Value *struct{...}
// usage text of subcommands is not used
parseFn, _ := buildParseCmdFn(
currentFieldChain,
fmt.Sprintf("%s %s", viewNameChain, cliName),
instance,
)
if err := baseCmd.AddCommand(
cliName,
subcmdInfo{
parseFn: parseFn,
defName: defName,
usage: usage,
},
currentFieldChain,
); err != nil {
return baseCmd, err
}
default:
return baseCmd, fmt.Errorf(
errNotImplParse,
valField.Type(),
currentFieldChain,
)
}
}
}
return baseCmd, nil
}
// parseStatus is detailed status for Parse interface
type parseStatus int
const (
notParse parseStatus = iota
validParse
exampleCannotParse
parseOnce
)
// customParser return parseFn for this specific r: reflect.Value
// returns parseFn, exampleCannotParse, error
// if r is Parse, but Example() cannot Parse, return exampleCannotParse = true
func hasCustomParser(r reflect.Value) (
_customParser func(typeSameAsR reflect.Value) parseArgFn,
_status parseStatus,
) {
if !r.CanAddr() {
return nil, notParse
}
if parse, ok := r.Addr().Interface().(Parse); ok {