-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.go
More file actions
283 lines (249 loc) · 7.14 KB
/
Copy pathparse.go
File metadata and controls
283 lines (249 loc) · 7.14 KB
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
package commandergo
import (
"fmt"
"os"
"regexp"
"strconv"
)
var (
reLongOpt = regexp.MustCompile(`^--([a-zA-Z][a-zA-Z-]*)(?:=(.+))?$`)
reShortOpt = regexp.MustCompile(`^-([a-zA-Z]+)(?:=(.+))?$`)
reCommand = regexp.MustCompile(`^([a-zA-Z][a-zA-Z\d]*)$`)
FLAG_END = "--"
)
func parseValue(s string) Varaint {
if i, err := strconv.Atoi(s); err == nil {
return Varaint{value: i}
}
if f, err := strconv.ParseFloat(s, 64); err == nil {
return Varaint{value: f}
}
if s == "true" {
return Varaint{value: true}
}
if s == "false" {
return Varaint{value: false}
}
return Varaint{value: s}
}
func (c *Command) findOptionByName(name string) *Option {
if opt, ok := c._options.get(name); ok {
return opt
}
return nil
}
func (c *Command) findOptionByAlias(alias string) *Option {
for _, opt := range c._options {
if opt.alias == alias {
return opt
}
}
return nil
}
type parseOption struct {
strict bool
}
func (c *Command) parse(args []string, parseOpt *parseOption) error {
ctx := newContext(c)
declareArgLen := len(c._arguments)
flagEnd := false
for i := 0; i < len(args); i++ {
token := args[i]
// 如果是--,就直接跳过
if token == FLAG_END {
flagEnd = true
continue
}
// flag end后的所有token都作为参数
if flagEnd {
ctx.parsedArgs = append(ctx.parsedArgs, parseValue(token))
continue
}
// 如果命中子命令,就直接交给子命令解析
if m := reCommand.FindStringSubmatch(token); m != nil {
if sub, ok := c._subCommands.get(m[1]); ok {
return sub.parse(args[i+1:], parseOpt)
}
}
// --name 或 --name=value
if m := reLongOpt.FindStringSubmatch(token); m != nil {
name, inlineVal := m[1], m[2]
opt := c.findOptionByName(name)
if opt == nil {
if parseOpt.strict {
return fmt.Errorf("unknown option: %v", token)
} else {
fmt.Fprintf(os.Stderr, "warning: unknown option: --%s\n", name)
continue
}
}
// option不需要值的情况:不接收cli值,也不接收默认值
if opt.valueName == "" {
ctx.parsedOpts[name] = Varaint{value: true}
if inlineVal != "" && parseOpt.strict {
return fmt.Errorf("option %v does not accept a value", token)
}
continue
}
// 如果有直接的值,就直接解析
if inlineVal != "" {
ctx.parsedOpts[name] = parseValue(inlineVal)
continue
}
// 如果cli没有传值,尝试解析后一个token做为值,token尝试失败再尝试使用默认值
if nextI := i + 1; nextI < len(args) {
nextToken := args[nextI]
// 检验下一个token
if nextToken != FLAG_END &&
!reLongOpt.MatchString(nextToken) &&
!reShortOpt.MatchString(nextToken) {
ctx.parsedOpts[name] = parseValue(nextToken)
i = nextI
continue
}
}
// 没有下一个token,或者下一个token检验失败,尝试使用默认值
if !opt.defaultValue.IsEmpty() {
ctx.parsedOpts[name] = opt.defaultValue
continue
}
// 以上都失败,如果这个值是必选的,直接结束
if opt.valueRequired {
return fmt.Errorf("option %v requires a value", token)
} else {
ctx.parsedOpts[name] = Varaint{value: true}
continue
}
}
// -f 或 -f=value 或 -abc
if m := reShortOpt.FindStringSubmatch(token); m != nil {
aliases, inlineVal := m[1], m[2]
// 多别名合并:前 n-1 个只能是布尔
for j := 0; j < len(aliases)-1; j++ {
a := string(aliases[j])
opt := c.findOptionByAlias(a)
// 严格模式,如果别名不存在,就直接结束
if opt == nil {
if parseOpt.strict {
return fmt.Errorf("unknown option alias: -%s", a)
} else {
fmt.Fprintf(os.Stderr, "warning: unknown option: -%s\n", a)
continue
}
}
// option不需要值的情况:不接收cli值,也不接收默认值
if opt.valueName == "" {
ctx.parsedOpts[opt.name] = Varaint{value: true}
continue
}
// 尝试使用默认值
if !opt.defaultValue.IsEmpty() {
ctx.parsedOpts[opt.name] = opt.defaultValue
continue
}
// 以上都失败,如果这个值是必选的,直接结束
if opt.valueRequired {
return fmt.Errorf("option %v requires a value", token)
} else {
ctx.parsedOpts[opt.name] = Varaint{value: true}
continue
}
}
// 最后一个可带值
last := string(aliases[len(aliases)-1])
opt := c.findOptionByAlias(last)
// 严格模式,如果别名不存在,就直接结束
if opt == nil {
if parseOpt.strict {
return fmt.Errorf("unknown option alias: -%s", last)
} else {
fmt.Fprintf(os.Stderr, "warning: unknown option: -%s\n", last)
continue
}
}
// option不需要值的情况:不接收cli值,也不接收默认值
if opt.valueName == "" {
ctx.parsedOpts[opt.name] = Varaint{value: true}
if inlineVal != "" && parseOpt.strict {
return fmt.Errorf("option %v does not accept a value", token)
}
continue
}
// 如果有直接的值,就直接解析
if inlineVal != "" {
ctx.parsedOpts[opt.name] = parseValue(inlineVal)
continue
}
// 如果cli没有传值,尝试解析后一个token做为值,token尝试失败再尝试使用默认值
if nextI := i + 1; nextI < len(args) {
nextToken := args[nextI]
// 检验下一个token
if nextToken != FLAG_END &&
!reLongOpt.MatchString(nextToken) &&
!reShortOpt.MatchString(nextToken) {
ctx.parsedOpts[opt.name] = parseValue(nextToken)
i = nextI
continue
}
}
// 没有下一个token,或者下一个token检验失败,尝试使用默认值
if !opt.defaultValue.IsEmpty() {
ctx.parsedOpts[opt.name] = opt.defaultValue
continue
}
// 以上都失败,如果这个值是必选的,直接结束
if opt.valueRequired {
return fmt.Errorf("option %v requires a value", token)
} else {
ctx.parsedOpts[opt.name] = Varaint{value: true}
continue
}
}
ctx.parsedArgs = append(ctx.parsedArgs, parseValue(token))
}
checkError := func() error {
// 位置参数:按 _arguments 顺序填入
if parseArgLen := len(ctx.parsedArgs); declareArgLen == 0 && parseArgLen > 0 {
return fmt.Errorf("argument is not expected, but got %v", parseArgLen)
} else if declareArgLen > 0 && parseArgLen > declareArgLen && !c._arguments[declareArgLen-1].multiValue {
return fmt.Errorf("%v arguments is expected, but got %v", declareArgLen, parseArgLen)
}
return nil
}
if parseOpt.strict {
if err := checkError(); err != nil {
return err
}
}
for i := len(ctx.parsedArgs); i < len(c._arguments); i++ {
arg := c._arguments[i]
if arg.defaultValue.IsEmpty() {
continue
}
ctx.parsedArgs = append(ctx.parsedArgs, arg.defaultValue)
}
// help / version 优先,不触发 action
if _, ok := ctx.parsedOpts["help"]; ok {
if c.customHelpFn != nil {
return c.customHelpFn(ctx)
}
fmt.Print(c.helpText())
return nil
}
if _, ok := ctx.parsedOpts["version"]; ok {
fmt.Println(c.version)
return nil
}
// 检查必填 argument
for i, arg := range c._arguments {
if arg.valueRequired {
if i >= len(ctx.Args()) {
return fmt.Errorf("argument <%s> is required", arg.name)
}
}
}
if c.actionFn != nil {
return c.actionFn(ctx)
}
return nil
}