generated from sraphs/go-starter
/
flagparser.go
139 lines (118 loc) · 2.63 KB
/
flagparser.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
package flag
import (
"fmt"
"reflect"
"regexp"
"strings"
)
// Parse parses the command-line flag arguments into a map,
// using the type information in element to discriminate whether a flag is supposed to be a bool,
// and other such ambiguities.
func Parse(args []string, element interface{}) (map[string]string, error) {
f := flagSet{
flagTypes: getFlagTypes(element),
args: args,
values: make(map[string]string),
keys: make(map[string]string),
}
for {
seen, err := f.parseOne()
if seen {
continue
}
if err == nil {
break
}
return nil, err
}
return f.values, nil
}
type flagSet struct {
flagTypes map[string]reflect.Kind
args []string
values map[string]string
keys map[string]string
}
func (f *flagSet) parseOne() (bool, error) {
if len(f.args) == 0 {
return false, nil
}
s := f.args[0]
if len(s) < 2 || s[0] != '-' {
return false, nil
}
numMinuses := 1
if s[1] == '-' {
numMinuses++
if len(s) == 2 { // "--" terminates the flags
f.args = f.args[1:]
return false, nil
}
}
name := s[numMinuses:]
if len(name) == 0 || name[0] == '-' || name[0] == '=' {
return false, fmt.Errorf("bad flag syntax: %s", s)
}
// it's a flag. does it have an argument?
f.args = f.args[1:]
hasValue := false
value := ""
for i := 1; i < len(name); i++ { // equals cannot be first
if name[i] == '=' {
value = name[i+1:]
hasValue = true
name = name[0:i]
break
}
}
if hasValue {
f.setValue(name, value)
return true, nil
}
flagType := f.getFlagType(name)
if flagType == reflect.Bool || flagType == reflect.Ptr {
f.setValue(name, "true")
return true, nil
}
if len(f.args) > 0 {
// value is the next arg
hasValue = true
value, f.args = f.args[0], f.args[1:]
}
if !hasValue {
return false, fmt.Errorf("flag needs an argument: -%s", name)
}
f.setValue(name, value)
return true, nil
}
func (f *flagSet) setValue(name, value string) {
srcKey := DefaultRootName + name
neutralKey := strings.ToLower(srcKey)
key, ok := f.keys[neutralKey]
if !ok {
f.keys[neutralKey] = srcKey
key = srcKey
}
v, ok := f.values[key]
if ok && f.getFlagType(name) == reflect.Slice {
f.values[key] = v + "," + value
return
}
f.values[key] = value
}
func (f *flagSet) getFlagType(name string) reflect.Kind {
neutral := strings.ToLower(name)
kind, ok := f.flagTypes[neutral]
if ok {
return kind
}
for n, k := range f.flagTypes {
if strings.Contains(n, MapNamePlaceholder) {
p := strings.NewReplacer(".", `\.`, MapNamePlaceholder, `([^.]+)`).Replace(n)
if regexp.MustCompile(p).MatchString(neutral) {
return k
}
}
}
return reflect.Invalid
}