-
Notifications
You must be signed in to change notification settings - Fork 8
/
resolver.go
242 lines (220 loc) · 5.13 KB
/
resolver.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
package flag
import (
"fmt"
"os"
"reflect"
)
var envParser = os.Getenv
type resolver struct {
LastSet *FlagSet
}
func (r *resolver) fromDefault(f *Flag) []string {
if !isSlicePtr(f.Ptr) {
return []string{fmt.Sprint(f.Default)}
}
refval := reflect.ValueOf(f.Default)
vals := make([]string, 0, refval.Len())
for i, l := 0, refval.Len(); i < l; i++ {
val := fmt.Sprint(refval.Index(i).Interface())
if val != "" {
vals = append(vals, val)
}
}
return vals
}
func (r *resolver) fromEnv(f *Flag) []string {
val := envParser(f.Env)
if val == "" {
return nil
}
var vals []string
if isSlicePtr(f.Ptr) {
vals = splitAndTrimSpace(val, f.ValSep)
} else {
vals = []string{val}
}
return vals
}
func (r *resolver) applyVals(f *Flag, vals ...string) error {
for _, val := range vals {
err := applyValToPtr(f.Names, f.Ptr, val, f.Selects)
if err != nil {
return err
}
}
return nil
}
func (r *resolver) applyEnvAndDefault(f *FlagSet, applied map[*Flag]bool) error {
for i := range f.flags {
flag := &f.flags[i]
if applied[flag] {
continue
}
applied[flag] = true
var vals []string
if flag.Env != "" {
vals = r.fromEnv(flag)
}
if len(vals) == 0 && flag.Default != nil {
vals = r.fromDefault(flag)
}
err := r.applyVals(flag, vals...)
if err != nil {
return err
}
}
return nil
}
func (r *resolver) resolveFlags(f *FlagSet, context []string, args []argument) error {
var positional []*Flag
for i := range f.flags {
if f.flags[i].Names == flagNamePositional {
positional = append(positional, &f.flags[i])
}
}
var (
applied = make(map[*Flag]bool)
flag *Flag
err error
positionalIndex int
applyValue = func(flag *Flag, val string) error {
applied[flag] = true
return r.applyVals(flag, val)
}
applyLastFlag = func() error {
if flag == nil {
return nil
}
return newErrorf(errFlagValueNotProvided, "flag value is not provided: %v.%s", context, flag.Names)
}
hasFlag = func(args []argument) bool {
for i := range args {
if args[i].Type == argumentFlag {
return true
}
}
return false
}
appendNonFlagArg = func(arg argument, args []argument) error {
if (positionalIndex >= len(positional) && f.self.ArgsPtr == nil) ||
(!f.self.ArgsAnywhere && hasFlag(args[1:])) {
return newErrorf(errNonFlagValue, "unexpected non-flag value: %v %s", context, arg.Value)
}
if positionalIndex < len(positional) {
err = applyValue(positional[positionalIndex], arg.Value)
if err != nil {
return err
}
positionalIndex++
return nil
}
slice := *f.self.ArgsPtr
slice = append(slice, arg.Value)
*f.self.ArgsPtr = slice
return nil
}
)
for i, arg := range args {
switch arg.Type {
case argumentFlag:
err = applyLastFlag()
if err != nil {
return err
}
flag = f.searchFlag(arg.Value)
if flag == nil {
return newErrorf(errFlagNotFound, "unsupported flag: %v.%s", context, arg.Value)
}
if applied[flag] && !isSlicePtr(flag.Ptr) {
return newErrorf(errDuplicateFlagParsed, "duplicated flag: %v.%s", context, flag.Names)
}
if arg.AttachValid {
// directly consume flag attached value
err = applyValue(flag, arg.Attached)
if err != nil {
return err
}
flag = nil
} else if isBoolPtr(flag.Ptr) {
// bool flag should not consume next value to not affect positional or non flag parsing
err = applyValue(flag, "true")
if err != nil {
return err
}
flag = nil
}
case argumentValue:
if flag == nil {
err = appendNonFlagArg(args[i], args[i:])
if err != nil {
return err
}
} else {
err = applyValue(flag, arg.Value)
if err != nil {
return err
}
flag = nil
}
default:
panic("unreachable")
}
}
err = applyLastFlag()
if err != nil {
return err
}
//if positionalIndex < len(positional) {
// var names []string
// for i := positionalIndex; i < len(positional); i++ {
// names = append(names, positional[i].Arglist)
// }
// return newErrorf(errPositionalFlagNotProvided, "flag not provided: %v.%v", context, names)
//}
return r.applyEnvAndDefault(f, applied)
}
func (r *resolver) resolveSet(f *FlagSet, context []string, args *scanArgs) (lastSubset *FlagSet, err error) {
context = append(context, f.self.Names)
err = r.resolveFlags(f, context, args.Flags[1:])
if err != nil {
return nil, err
}
for sub, subArgs := range args.Sets {
set := &f.subsets[f.subsetIndexes[sub]]
err = r.applyVals(&set.self, "true")
if err != nil {
return nil, err
}
last, err := r.resolveSet(set, context, subArgs)
if err != nil {
return nil, err
}
if sub == args.FirstSubset {
lastSubset = last
if lastSubset == nil {
lastSubset = set
}
}
}
if lastSubset == nil {
lastSubset = f
}
return lastSubset, nil
}
func (r *resolver) resolve(f *FlagSet, args *scanArgs) error {
var err error
r.LastSet, err = r.resolveSet(f, nil, args)
return err
}
func (r *resolver) reset(f *FlagSet) {
if f.self.ArgsPtr != nil {
*f.self.ArgsPtr = nil
}
resetPtrVal(f.self.Ptr)
for i := range f.flags {
resetPtrVal(f.flags[i].Ptr)
}
for i := range f.subsets {
r.reset(&f.subsets[i])
}
}