forked from thatisuday/clapper
-
Notifications
You must be signed in to change notification settings - Fork 1
/
clapper.go
772 lines (651 loc) · 20 KB
/
clapper.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
// MIT License
// Copyright (c) 2020 Uday Hiwarale
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// Package clapper processes the command-line arguments of getopt(3) syntax.
// This package provides the ability to process the root command, sub commands,
// command-line arguments and command-line flags.
package clapper
// TODO descriptions for help
import (
"fmt"
"reflect"
"strconv"
"strings"
"time"
)
/***********************************************/
// UnknownCommand represents an error when command-line arguments contain an unregistered command.
type UnknownCommand struct {
Name string
}
func (e UnknownCommand) Error() string {
return fmt.Sprintf("unknown command %s found in the arguments", e.Name)
}
type BadArgument struct {
Arg *Arg
Message string
}
func (e BadArgument) Error() string {
return fmt.Sprintf("%s %s", e.Arg.Name, e.Message)
}
// UnknownFlag represents an error when command-line arguments contain an unregistered flag.
type UnknownFlag struct {
Name string
}
func (e UnknownFlag) Error() string {
return fmt.Sprintf("unknown flag %s found in the arguments", e.Name)
}
/*---------------------*/
// Registry holds the configuration of the registered commands.
type Registry map[string]*CommandConfig
// NewRegistry returns new instance of the "Registry"
func NewRegistry() Registry {
return make(Registry)
}
// Register method registers a command.
// The "name" argument should be a simple string.
// If "name" is an empty string, it is considered as a root command.
// If a command is already registered, the registered `*CommandConfig` object is returned.
// If the command is already registered, second return value will be `true`.
func (registry Registry) Register(name string) (*CommandConfig, bool) {
// remove all whitespaces
commandName := removeWhitespaces(name)
// check if command is already registered, if found, return existing entry
if _commandConfig, ok := registry[commandName]; ok {
return _commandConfig, true
}
// construct new `CommandConfig` object
commandConfig := &CommandConfig{
Name: commandName,
Flags: make(map[string]*Flag),
flagsShort: make(map[string]string),
Args: make(map[string]*Arg),
ArgNames: make([]string, 0),
}
// add entry to the registry
registry[commandName] = commandConfig
return commandConfig, false
}
// Parse method parses command-line arguments and returns an appropriate "*CommandConfig" object registered in the registry.
// If command is not registered, it return `ErrorUnknownCommand` error.
// If there is an error parsing a flag, it can return an `ErrorUnknownFlag` or `ErrorUnsupportedFlag` error.
func (registry Registry) Parse(values []string) (*CommandConfig, error) {
// command name
var commandName string
// command-line argument values to process
valuesToProcess := values
// check if command is a root command
if isRootCommand(values, registry) {
commandName = "" // root command name
} else {
commandName, valuesToProcess = nextValue(values)
}
// format command-line argument values
valuesToProcess = formatCommandValues(valuesToProcess)
// check for invalid flag structure
for _, val := range valuesToProcess {
if isFlag(val) && isUnknownFlag(val) {
return nil, UnknownFlag{val}
}
}
// if command is not registered, return `ErrorUnknownCommand` error
if _, ok := registry[commandName]; !ok {
return nil, UnknownCommand{commandName}
}
// get `CommandConfig` object from the registry
commandConfig := registry[commandName]
// process all command-line arguments (except command name)
for {
// get current command-line argument value
var value string
value, valuesToProcess = nextValue(valuesToProcess)
// if `value` is empty, break the loop
if len(value) == 0 {
break
}
// if the thing is a flag, process as a flag; otherwise, process as an arg
if isFlag(value) {
// trim `-` characters from the `value`
name := strings.TrimLeft(value, "-")
// get flag object stored in the `commandConfig`
var flag *Flag
var isBool bool
// check if flag is short or long
if isShortFlag(value) {
if _, ok := commandConfig.flagsShort[name]; !ok {
return nil, UnknownFlag{value}
}
// get long flag name
flagName := commandConfig.flagsShort[name]
flag = commandConfig.Flags[flagName]
_, isBool = flag.defaultValue.(bool)
// there is no argument; just set the value
if isBool {
flag.value = true
}
} else {
// If it's a long flag, the check for bool (which has no value)
var isInv bool
if strings.HasPrefix(name, "no-") {
name = name[3:]
isInv = true
}
flag = commandConfig.Flags[name]
if flag == nil {
return nil, UnknownFlag{value}
}
_, isBool = flag.defaultValue.(bool)
if isBool {
flag.value = !isInv
}
if isInv {
if !isBool {
return nil, BadArgument{&flag.Arg, "non-bool flag"}
}
}
}
var err error
if !isBool {
if nextValue, nextValuesToProcess := nextValue(valuesToProcess); len(nextValue) != 0 && !isFlag(nextValue) {
if flag.value, err = convert(nextValue, flag.defaultValue); err != nil {
return nil, err
}
valuesToProcess = nextValuesToProcess
} else if len(nextValue) == 0 {
return nil, BadArgument{&flag.Arg, "parameter requires an argument, none was provided"}
}
}
if err := validateParams(&flag.Arg); err != nil {
return nil, err
}
} else {
// process as argument
var arg *Arg
//var err error
for index, argName := range commandConfig.ArgNames {
// get argument object stored in the `commandConfig`
arg = commandConfig.Args[argName]
var conval interface{}
var err error
if conval, err = convert(value, arg.defaultValue); err != nil {
return nil, err
}
var slice reflect.Value
if arg.value == nil {
if arg.isVariadic {
slice = reflect.MakeSlice(reflect.SliceOf(reflect.TypeOf(conval)), 0, 0)
rval := reflect.New(slice.Type())
rval.Elem().Set(slice)
sp := reflect.ValueOf(rval.Interface())
svp := sp.Elem()
arg.value = svp.Interface()
} else {
arg.value = conval
break
}
}
// if last argument is a variadic argument, append values
if (index == len(commandConfig.ArgNames)-1) && arg.isVariadic {
slice = reflect.ValueOf(arg.value)
rval := reflect.New(slice.Type())
rval.Elem().Set(slice)
sp := reflect.ValueOf(rval.Interface())
svp := sp.Elem()
svp.Set(reflect.Append(svp, reflect.ValueOf(conval)))
arg.value = svp.Interface()
}
}
if err := validateParams(arg); err != nil {
return nil, err
}
}
}
return commandConfig, nil
}
func convert(i string, defaults interface{}) (interface{}, error) {
var rv interface{}
var err error
// The default could be an array of allowed values, and if so,
// get one of the elements so we can test the type
p := reflect.TypeOf(defaults)
if p.Kind() == reflect.Slice {
p = p.Elem()
}
timeKind := reflect.TypeOf(time.Now()).Kind()
durationKind := reflect.TypeOf(time.Second).Kind()
switch p.Kind() {
case reflect.Bool:
rv, err = strconv.ParseBool(i)
case reflect.String:
return i, nil
case reflect.Int:
rv, err = strconv.Atoi(i)
case timeKind:
rv, err = time.Parse("2006-01-02 03:04", i)
case durationKind:
rv, err = time.ParseDuration(i)
case reflect.Float64:
rv, err = strconv.ParseFloat(i, 64)
default:
}
return rv, err
}
// validate the a.value(s) against the a.defaultValue(s)
// If a.value is a value, it must match the type of a.defaultValue; or,
// if a.defaultValue is an array, a.value must be in a.defaultValue.
//
// If a.value is an array, every element must be of type a.defaultValue; or,
// if a.defaultValue is an array, every element in a.value mut be found in a.defaultValue.
func validateParams(a *Arg) error {
if a.value == nil {
return BadArgument{a, "parameter requires argument"}
}
p := reflect.TypeOf(a.value)
pv := reflect.ValueOf(a.value)
// if a.value is an array, check each element against a.defaultValues
if p.Kind() == reflect.Slice {
for i := 0; i < pv.Len(); i++ {
v := pv.Index(i).Interface()
if !validateElement(v, a.defaultValue) {
return BadArgument{a, fmt.Sprintf("illegal value %v, must be %v", v, a.defaultValue)}
}
}
return nil
} else {
// if a.value is not an array, test it against a.defaultValue
if !validateElement(a.value, a.defaultValue) {
return BadArgument{a, fmt.Sprintf("illegal value %v, must be %v", a.value, a.defaultValue)}
}
}
return nil
}
// validates a single argument against a (possible) array
func validateElement(val interface{}, vals interface{}) bool {
p := reflect.TypeOf(vals)
pv := reflect.ValueOf(vals)
// if vals is an array, val must be in it
if p.Kind() == reflect.Slice {
for i := 0; i < pv.Len(); i++ {
v := pv.Index(i).Interface()
if val == v {
return true
}
}
return false
} else {
// Both val and vals are not arrays
return reflect.TypeOf(val) == reflect.TypeOf(vals)
}
}
/*---------------------*/
// CommandConfig type holds the structure and values of the command-line arguments of command.
type CommandConfig struct {
// name of the sub-command ("" for the root command)
Name string
// command-line flags
Flags map[string]*Flag
// mapping of the short flag names with long flag names
flagsShort map[string]string
// registered command argument values
Args map[string]*Arg
// list of the argument names (for ordered iteration)
ArgNames []string
}
// AddArg registers an argument configuration with the command.
//
// - The `name` argument represents the name of the argument.
// - If value of the `name` argument ends with `...` suffix, then it is a
// variadic argument.
// - If the argument is already registered, second return value will be `true`.
// - Variadic argument can accept multiple argument values and it should be the
// last registered argument.
// - Values of a variadic argument will be returned as an array.
// - All arguments without a default value must be registered first.
// - If an argument with given `name` is already registered, then argument
// registration is skipped and registered `*Arg` object returned.
// - The `defaultValue` argument represents the default value of the argument,
// and determines the type of the argument value.
//
// Supported types are:
//
// - int
// - string
// - float64
// - bool
// - time.Time
// - time.Duration
//
// If the provided defaultValue is an array of one of the above types, then that
// array defines a set of legal values; any provided parameter that doesn't
// match a value in the array will result in a parse error.
func (commandConfig *CommandConfig) AddArg(name string, defaultValue interface{}) *Arg {
// clean argument values
name = removeWhitespaces(name)
// return if argument is already registered
if _arg, ok := commandConfig.Args[name]; ok {
return _arg
}
rv := Arg{Name: name}
if v, ok := defaultValue.(string); ok {
rv.defaultValue = trimWhitespaces(v)
} else {
rv.defaultValue = defaultValue
}
// check if argument is variadic
if ok, argName := isVariadicArgument(name); ok {
rv.Name = argName // change argument name
rv.isVariadic = true
}
// register argument with the command-config
commandConfig.Args[rv.Name] = &rv
// store argument name (for ordered iteration)
commandConfig.ArgNames = append(commandConfig.ArgNames, rv.Name)
return &rv
}
// AddFlag method registers a command-line flag with the command.
//
// The rules for flags are the same as for args, but in addition:
//
// - The `name` argument is the long-name of the flag and it should not start
// with `--` prefix.
// - The `shortName` argument is the short-name of the flag and it should not
// start with `-` prefix.
// - A boolean flag doesn't accept an input value such as `--flag=<value>` and
// its default value is "true".
// - If the `name` value starts with `no-` prefix, then it is considered as an
// inverted flag.
// - Regardless of the default, boolean flags are true if provided, and false if
// inverted
// - Registering a non-boolean inverted flag will produce an error
// - Boolean flag defaults are preserved, but have no effect on the `AsBool()` result.
func (commandConfig *CommandConfig) AddFlag(name string, shortName string, defaultValue interface{}) (*Flag, error) {
// clean argument values
name = removeWhitespaces(name)
isInverted := strings.HasPrefix(name, "no-")
if isInverted {
name = name[3:]
}
if _, ok := defaultValue.(bool); !ok && isInverted {
return nil, fmt.Errorf("non-boolean arguments can not be inverted")
}
// return if flag is already registered
if _flag, ok := commandConfig.Flags[name]; ok {
return _flag, nil
}
rv := Flag{
ShortName: removeWhitespaces(shortName),
}
rv.Name = name
rv.defaultValue = defaultValue
switch v := defaultValue.(type) {
case bool:
if isInverted {
rv.defaultValue = !v
} else {
rv.defaultValue = v
}
case string:
rv.defaultValue = trimWhitespaces(v)
}
// check if argument is variadic
if ok, argName := isVariadicArgument(name); ok {
rv.Name = argName // change argument name
rv.isVariadic = true
}
// short flag name should be only one character long
l := len(rv.ShortName)
switch {
case l > 1:
return nil, fmt.Errorf("short names must be one character")
case l == 1:
if isInverted {
return nil, fmt.Errorf("inverted flags may not have short versions")
}
rv.ShortName = rv.ShortName[:1]
commandConfig.flagsShort[rv.ShortName] = rv.Name
}
// register flag with the command-config
commandConfig.Flags[rv.Name] = &rv
return &rv, nil
}
// Arg type holds the structured information about an argument.
type Arg struct {
// name of the argument
Name string
isVariadic bool
defaultValue interface{}
value interface{}
}
func (a Arg) AsInt() int {
if v, ok := a.value.(int); ok {
return v
} else {
v, _ = a.defaultValue.(int)
return v
}
}
func (a Arg) AsTime() time.Time {
if v, ok := a.value.(time.Time); ok {
return v
} else {
v, _ := a.defaultValue.(time.Time)
return v
}
}
func (a Arg) AsDuration() time.Duration {
if v, ok := a.value.(time.Duration); ok {
return v
} else {
v, _ = a.defaultValue.(time.Duration)
return v
}
}
func (a Arg) AsBool() bool {
if v, ok := a.value.(bool); ok {
return v
} else {
v, _ = a.defaultValue.(bool)
return v
}
}
func (a Arg) AsString() string {
if v, ok := a.value.(string); ok {
return v
} else {
v, _ = a.defaultValue.(string)
return v
}
}
func (a Arg) AsFloat() float64 {
if v, ok := a.value.(float64); ok {
return v
} else {
v, _ = a.defaultValue.(float64)
return v
}
}
func (a Arg) AsInts() []int {
if v, ok := a.value.([]int); ok {
return v
} else {
v, _ = a.defaultValue.([]int)
return v
}
}
func (a Arg) AsTimes() []time.Time {
if v, ok := a.value.([]time.Time); ok {
return v
} else {
v, _ = a.defaultValue.([]time.Time)
return v
}
}
func (a Arg) AsDurations() []time.Duration {
if v, ok := a.value.([]time.Duration); ok {
return v
} else {
v, _ = a.defaultValue.([]time.Duration)
return v
}
}
func (a Arg) AsBools() []bool {
if v, ok := a.value.([]bool); ok {
return v
} else {
v, _ = a.defaultValue.([]bool)
return v
}
}
func (a Arg) AsStrings() []string {
if v, ok := a.value.([]string); ok {
return v
} else {
v, _ = a.defaultValue.([]string)
return v
}
}
func (a Arg) AsFloats() []float64 {
if v, ok := a.value.([]float64); ok {
return v
} else {
v, _ = a.defaultValue.([]float64)
return v
}
}
// Flag type holds the structured information about a flag.
type Flag struct {
Arg
// short name of the flag
ShortName string
}
/***********************************************
PRIVATE FUNCTIONS AND VARIABLES
***********************************************/
// format command-line argument values
func formatCommandValues(values []string) (formatted []string) {
formatted = make([]string, 0)
for _, presplit := range values {
for _, value := range detectSplitCombined(presplit) {
// split a value by `=`
if isFlag(value) {
parts := strings.Split(value, "=")
for _, part := range parts {
if strings.Trim(part, " ") != "" {
formatted = append(formatted, part)
}
}
} else {
formatted = append(formatted, value)
}
}
}
fmt.Printf("formatted = %v\n", formatted)
return
}
// detectSplitCombined checks whether the argument is a combined flag and breaks
// it apart if it is. E.g., for declared flags `-a` and `-b`, the provided
// argument `-ab` will be broken in two.
func detectSplitCombined(s string) []string {
// Don't try to process assignments
if strings.Contains(s, "=") {
return []string{s}
}
// If it's a long-form argument, then no split
if strings.HasPrefix(s, "--") {
return []string{s}
}
if strings.HasPrefix(s, "-") {
parts := strings.Split(s, "")
for i, p := range parts {
if i == 0 {
continue
}
parts[i] = "-" + p
}
return parts[1:]
}
return []string{s}
}
// check if value is a flag
func isFlag(value string) bool {
return len(value) >= 2 && strings.HasPrefix(value, "-")
}
// check if value is a short flag
func isShortFlag(value string) bool {
return isFlag(value) && len(value) == 2 && !strings.HasPrefix(value, "--")
}
// check if flag is unsupported
func isUnknownFlag(value string) bool {
// a flag should be at least two characters log
if len(value) >= 2 {
// if short flag, it should start with `-` but not with `--`
if len(value) == 2 {
return !strings.HasPrefix(value, "-") || strings.HasPrefix(value, "--")
}
// if long flag, it should start with `--` and not with `---`
return !strings.HasPrefix(value, "--") || strings.HasPrefix(value, "---")
}
return false
}
// check if value ends with `...` sufix
func isVariadicArgument(value string) (bool, string) {
if !isFlag(value) && strings.HasSuffix(value, "...") {
return true, strings.TrimRight(value, "...") // trim `...` suffix
}
return false, ""
}
// check if values corresponds to the root command
func isRootCommand(values []string, registry Registry) bool {
// FALSE: if the root command is not registered
if _, ok := registry[""]; !ok {
return false
}
// TRUE: if all `values` are empty or the first `value` is a flag
if len(values) == 0 || isFlag(values[0]) {
return true
}
// get root `CommandConfig` value from the registry
rootCommandConfig := registry[""]
// TRUE: if the first value is not a registered command
// and some arguments are registered for the root command
if _, ok := registry[values[0]]; len(rootCommandConfig.Args) > 0 && !ok {
return true
}
return false
}
// return next value and remaining values of a slice of strings
func nextValue(slice []string) (v string, newSlice []string) {
if len(slice) == 0 {
v, newSlice = "", make([]string, 0)
return
}
v = slice[0]
if len(slice) > 1 {
newSlice = slice[1:]
} else {
newSlice = make([]string, 0)
}
return
}
// trim whitespaces from a value
func trimWhitespaces(value string) string {
return strings.Trim(value, "")
}
// remove whitespaces from a value
func removeWhitespaces(value string) string {
return strings.ReplaceAll(value, " ", "")
}