-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.go
More file actions
378 lines (350 loc) · 11 KB
/
Copy pathparse.go
File metadata and controls
378 lines (350 loc) · 11 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
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
package cli
import (
"fmt"
"slices"
"strconv"
"strings"
)
type parseError struct {
message string
}
func (value parseError) Error() string { return value.message }
func (value parseError) Unwrap() error { return ErrInvocation }
type builtinState struct {
helpSet bool
help bool
versionSet bool
version bool
}
func (parser Parser) Parse(arguments []string) (Invocation, error) {
if !parser.available() {
return Invocation{}, ErrDefinition
}
if !validInput(arguments) {
return Invocation{}, ErrInvocation
}
var storage [maxInvocationOptions]optionValue
root := storage[:len(parser.definition.options)]
builtins := builtinState{}
remaining, rootArguments, err := parser.parseOptions(arguments, nil, root, &builtins)
if err != nil {
return Invocation{}, err
}
return parser.resolveRootInvocation(root, &builtins, remaining, rootArguments)
}
func (parser Parser) resolveRootInvocation(
root []optionValue,
builtins *builtinState,
remaining []string,
rootArguments bool,
) (Invocation, error) {
if builtins.help {
return parser.helpInvocation(remaining)
}
if builtins.version {
return versionInvocation(remaining)
}
if rootArguments && parser.definition.arguments.Maximum != 0 {
return parser.rootInvocation(root, remaining)
}
if len(remaining) == 0 {
return Invocation{action: ActionHelp}, nil
}
if remaining[0] == helpName {
return parser.parseBuiltinInvocation(helpName, root, builtins, remaining[1:])
}
if (remaining[0] == versionName || remaining[0] == versionAlias) && parser.definition.version {
return parser.parseBuiltinInvocation(versionName, root, builtins, remaining[1:])
}
if command, found := parser.definition.command(remaining[0]); found {
return parser.parseCommandInvocation(command, root, builtins, remaining[1:])
}
if parser.definition.arguments.Maximum != 0 {
return parser.rootInvocation(root, remaining)
}
return Invocation{}, invocationError("unknown command %q for %q", remaining[0], parser.definition.name)
}
func (parser Parser) parseBuiltinInvocation(
name string,
root []optionValue,
builtins *builtinState,
arguments []string,
) (Invocation, error) {
remaining, _, err := parser.parseOptions(arguments, nil, root, builtins)
if err != nil {
return Invocation{}, err
}
if builtins.help {
return parser.commandHelpInvocation(name, remaining)
}
if builtins.version {
return versionInvocation(remaining)
}
if name == helpName {
return parser.helpInvocation(remaining)
}
return versionInvocation(remaining)
}
func (parser Parser) parseCommandInvocation(
command Command,
root []optionValue,
builtins *builtinState,
arguments []string,
) (Invocation, error) {
values := root[:len(root)+len(command.Options)]
remaining, _, err := parser.parseOptions(arguments, command.Options, values, builtins)
if err != nil {
return Invocation{}, err
}
if builtins.help {
return parser.commandHelpInvocation(command.Name, remaining)
}
if builtins.version {
return versionInvocation(remaining)
}
if err = validateRequiredOptions(command.Options, values[len(root):]); err != nil {
return Invocation{}, err
}
if err = validateArgumentCount(command.Name, command.Arguments, len(remaining)); err != nil {
return Invocation{}, err
}
result := Invocation{
rootOptions: parser.definition.options, localOptions: command.Options, action: ActionRun, command: command.Name,
}
retainArguments(&result, remaining)
retainOverrides(&result, values)
return result, nil
}
func (parser Parser) rootInvocation(root []optionValue, arguments []string) (Invocation, error) {
if err := validateArgumentCount(parser.definition.name, parser.definition.arguments, len(arguments)); err != nil {
return Invocation{}, err
}
result := Invocation{rootOptions: parser.definition.options, action: ActionRun}
retainArguments(&result, arguments)
retainOverrides(&result, root)
return result, nil
}
func (parser Parser) parseOptions(
arguments []string,
local []Option,
values []optionValue,
builtins *builtinState,
) ([]string, bool, error) {
for index := 0; index < len(arguments); index++ {
argument := arguments[index]
if argument == "--" {
return arguments[index+1:], true, nil
}
name, inline, hasValue, option, err := splitOption(argument)
if err != nil {
return nil, false, err
}
if !option {
return arguments[index:], false, nil
}
if handled, builtinErr := parser.setBuiltin(name, inline, hasValue, builtins); handled {
if builtinErr != nil {
return nil, false, builtinErr
}
continue
}
definition, valueIndex, found := findOption(name, parser.definition.options, local, true)
if !found {
return nil, false, invocationError("flag provided but not defined: -%s", name)
}
if definition.Kind == ValueString && !hasValue {
index++
if index == len(arguments) {
return nil, false, invocationError("option --%s requires a value", definition.Name)
}
inline, hasValue = arguments[index], true
}
if err = setOption(definition, &values[valueIndex], inline, hasValue); err != nil {
return nil, false, err
}
}
return nil, false, nil
}
func splitOption(argument string) (name, value string, hasValue, option bool, err error) {
if len(argument) < 2 || argument[0] != '-' {
return "", "", false, false, nil
}
start := 1
if argument[1] == '-' {
start = 2
}
if start == len(argument) || argument[start] == '-' || argument[start] == '=' {
return "", "", false, false, invocationError("bad flag syntax")
}
name, value, hasValue = strings.Cut(argument[start:], "=")
return name, value, hasValue, true, nil
}
func (parser Parser) setBuiltin(name, value string, hasValue bool, state *builtinState) (bool, error) {
switch name {
case helpName, helpAlias:
return true, setBoolean(helpName, value, hasValue, &state.help, &state.helpSet)
case versionName, versionAlias:
if parser.definition.version {
return true, setBoolean(versionName, value, hasValue, &state.version, &state.versionSet)
}
}
return false, nil
}
func setBoolean(name, value string, hasValue bool, destination, set *bool) error {
if *set {
return invocationError("option --%s provided more than once", name)
}
parsed := true
var err error
if hasValue {
parsed, err = strconv.ParseBool(value)
if err != nil {
return invocationError("invalid value for --%s", name)
}
}
*destination, *set = parsed, true
return nil
}
func findOption(name string, root, local []Option, shortAliases bool) (Option, int, bool) {
for index, option := range root {
if name == option.Name || shortAliases && option.Short != "" && name == option.Short {
return option, index, true
}
}
for index, option := range local {
if name == option.Name || shortAliases && option.Short != "" && name == option.Short {
return option, len(root) + index, true
}
}
return Option{}, 0, false
}
func setOption(definition Option, state *optionValue, value string, hasValue bool) error {
if definition.Kind == ValueBool {
return setBoolean(definition.Name, value, hasValue, &state.boolean, &state.set)
}
if state.set {
return invocationError("option --%s provided more than once", definition.Name)
}
if !hasValue || value == "" {
return invocationError("invalid value for --%s", definition.Name)
}
state.text = value
state.set = true
return nil
}
func retainOverrides(invocation *Invocation, values []optionValue) {
count := 0
for _, value := range values {
if value.set {
count++
}
}
invocation.overrideCount = count
if count == 0 {
return
}
if count <= len(invocation.inlineOverrides) {
result := invocation.inlineOverrides[:0]
for index, value := range values {
if value.set {
result = append(result, optionOverride{text: value.text, index: uint16(index), boolean: value.boolean})
}
}
return
}
invocation.overrides = make([]optionOverride, 0, count)
for index, value := range values {
if value.set {
invocation.overrides = append(invocation.overrides, optionOverride{text: value.text, index: uint16(index), boolean: value.boolean})
}
}
}
func retainArguments(invocation *Invocation, arguments []string) {
invocation.argumentCount = len(arguments)
if len(arguments) <= len(invocation.inlineArguments) {
copy(invocation.inlineArguments[:], arguments)
return
}
invocation.arguments = slices.Clone(arguments)
}
func validateRequiredOptions(options []Option, values []optionValue) error {
for index, option := range options {
if option.Required && !values[index].set {
return invocationError("required option --%s is missing", option.Name)
}
}
return nil
}
func validateArgumentCount(command string, arguments Arguments, received int) error {
if received >= arguments.Minimum && received <= arguments.Maximum {
return nil
}
return argumentCountError(command, arguments, received)
}
func argumentCountError(command string, arguments Arguments, received int) error {
switch {
case arguments.Maximum == 0:
return invocationError("command %q accepts no arguments", command)
case arguments.Minimum == arguments.Maximum:
return invocationError("command %q requires %d %s but received %d", command, arguments.Minimum, argumentWord(arguments.Minimum), received)
case arguments.Minimum == 0:
return invocationError("command %q accepts at most %d %s but received %d", command, arguments.Maximum, argumentWord(arguments.Maximum), received)
default:
return invocationError("command %q accepts %d to %d arguments but received %d", command, arguments.Minimum, arguments.Maximum, received)
}
}
func argumentWord(count int) string {
if count == 1 {
return "argument"
}
return "arguments"
}
func (parser Parser) helpInvocation(arguments []string) (Invocation, error) {
if len(arguments) == 0 {
return Invocation{action: ActionHelp}, nil
}
if len(arguments) != 1 {
return Invocation{}, invocationError("help accepts at most one command")
}
return parser.commandHelpInvocation(arguments[0], nil)
}
func (parser Parser) commandHelpInvocation(command string, arguments []string) (Invocation, error) {
if len(arguments) != 0 {
return Invocation{}, invocationError("help for %q accepts no arguments", command)
}
selected, found := parser.helpSubject(command)
if !found {
return Invocation{}, invocationError("unknown command %q for %q", command, parser.definition.name)
}
return Invocation{action: ActionHelp, command: selected.Name}, nil
}
func versionInvocation(arguments []string) (Invocation, error) {
if len(arguments) != 0 {
return Invocation{}, invocationError("version accepts no arguments")
}
return Invocation{action: ActionVersion}, nil
}
func (definition *admittedDefinition) command(name string) (Command, bool) {
for _, command := range definition.commands {
if command.Name == name {
return command, true
}
}
return Command{}, false
}
func validInput(arguments []string) bool {
if len(arguments) > MaxInputArguments {
return false
}
bytes := 0
for _, argument := range arguments {
if len(argument) > MaxInputBytes-bytes || !validText(argument) {
return false
}
bytes += len(argument)
}
return true
}
func invocationError(format string, arguments ...any) error {
return parseError{message: fmt.Sprintf(format, arguments...)}
}