Repository navigation
Expand file tree
/
Copy pathargs.go
More file actions
405 lines (345 loc) · 10.1 KB
/
Copy pathargs.go
File metadata and controls
405 lines (345 loc) · 10.1 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
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
package main
import (
"fmt"
"strings"
)
// Command constants define supported resources, actions, and flags.
const (
resourceWorkbook = "workbook"
resourceSheet = "sheet"
resourceCell = "cell"
resourceRange = "range"
actionInfo = "info"
actionList = "list"
actionRead = "read"
actionSet = "set"
actionClear = "clear"
flagPretty = "--pretty"
flagSheet = "--sheet"
flagCell = "--cell"
flagRange = "--range"
flagValue = "--value"
flagFormula = "--formula"
)
// parsedArgs holds a validated CLI command.
type parsedArgs struct {
resource string
action string
file string
sheet string
cell string
cellRange string
value string
formula string
valueSet bool
formulaSet bool
pretty bool
}
// parseError describes a usage error from argument parsing.
type parseError struct {
message string
pretty bool
}
// Error returns the parse error message.
func (e parseError) Error() string {
return e.message
}
// commandSpec describes valid flags for a command.
type commandSpec struct {
resource string
action string
allowSheet bool
allowCell bool
allowRange bool
allowValue bool
allowFormula bool
requireSheet bool
requireCell bool
requireRange bool
requireValueOrFormula bool
}
// argParser incrementally parses CLI arguments.
type argParser struct {
args []string
spec commandSpec
cmd parsedArgs
file string
seenPretty bool
seenSheet bool
seenCell bool
seenRange bool
seenValue bool
seenFormula bool
}
// parseArgs parses and validates CLI arguments.
func parseArgs(args []string) (parsedArgs, error) {
parser := argParser{args: args}
return parser.parse()
}
// parse parses the configured argument slice.
func (p *argParser) parse() (parsedArgs, error) {
spec, err := p.parseCommandSpec()
if err != nil {
return parsedArgs{}, err
}
p.spec = spec
p.cmd = parsedArgs{
resource: spec.resource,
action: spec.action,
}
if err := p.parseTail(); err != nil {
return parsedArgs{}, err
}
p.cmd.file = p.file
if err := p.validate(); err != nil {
return parsedArgs{}, err
}
if err := p.normalize(); err != nil {
return parsedArgs{}, err
}
return p.cmd, nil
}
// parseCommandSpec resolves the command resource and action.
func (p *argParser) parseCommandSpec() (commandSpec, error) {
if len(p.args) == 0 {
return commandSpec{}, parseError{message: "missing command"}
}
if len(p.args) == 1 {
message := fmt.Sprintf("missing action for %q", p.args[0])
return commandSpec{}, parseError{message: message}
}
spec, ok := lookupCommand(p.args[0], p.args[1])
if ok {
return spec, nil
}
message := fmt.Sprintf("unknown command: %s %s", p.args[0], p.args[1])
return commandSpec{}, parseError{message: message}
}
// parseTail parses the file argument and trailing flags.
func (p *argParser) parseTail() error {
for i := 2; i < len(p.args); i++ {
arg := p.args[i]
if !isFlagToken(arg) {
if p.file != "" {
return parseError{message: fmt.Sprintf("unexpected argument: %s", arg), pretty: p.cmd.pretty}
}
p.file = arg
continue
}
next, err := p.parseFlag(i)
if err != nil {
return err
}
i = next
}
return nil
}
// parseFlag parses one supported flag at the given index.
func (p *argParser) parseFlag(index int) (int, error) {
switch p.args[index] {
case flagPretty:
if p.seenPretty {
return 0, parseError{message: fmt.Sprintf("duplicate flag: %s", flagPretty), pretty: p.cmd.pretty}
}
p.seenPretty = true
p.cmd.pretty = true
return index, nil
case flagSheet:
if p.seenSheet {
return 0, parseError{message: fmt.Sprintf("duplicate flag: %s", flagSheet), pretty: p.cmd.pretty}
}
value, next, err := readFlagValue(p.args, index, false)
if err != nil {
return 0, parseError{message: err.Error(), pretty: p.cmd.pretty}
}
p.seenSheet = true
p.cmd.sheet = value
return next, nil
case flagCell:
if p.seenCell {
return 0, parseError{message: fmt.Sprintf("duplicate flag: %s", flagCell), pretty: p.cmd.pretty}
}
value, next, err := readFlagValue(p.args, index, false)
if err != nil {
return 0, parseError{message: err.Error(), pretty: p.cmd.pretty}
}
p.seenCell = true
p.cmd.cell = value
return next, nil
case flagRange:
if p.seenRange {
return 0, parseError{message: fmt.Sprintf("duplicate flag: %s", flagRange), pretty: p.cmd.pretty}
}
value, next, err := readFlagValue(p.args, index, false)
if err != nil {
return 0, parseError{message: err.Error(), pretty: p.cmd.pretty}
}
p.seenRange = true
p.cmd.cellRange = value
return next, nil
case flagValue:
if p.seenValue {
return 0, parseError{message: fmt.Sprintf("duplicate flag: %s", flagValue), pretty: p.cmd.pretty}
}
value, next, err := readFlagValue(p.args, index, true)
if err != nil {
return 0, parseError{message: err.Error(), pretty: p.cmd.pretty}
}
p.seenValue = true
p.cmd.value = value
p.cmd.valueSet = true
return next, nil
case flagFormula:
if p.seenFormula {
return 0, parseError{message: fmt.Sprintf("duplicate flag: %s", flagFormula), pretty: p.cmd.pretty}
}
value, next, err := readFlagValue(p.args, index, true)
if err != nil {
return 0, parseError{message: err.Error(), pretty: p.cmd.pretty}
}
p.seenFormula = true
p.cmd.formula = value
p.cmd.formulaSet = true
return next, nil
default:
return 0, parseError{message: fmt.Sprintf("unknown flag: %s", p.args[index]), pretty: p.cmd.pretty}
}
}
// validate checks parsed arguments against the command spec.
func (p *argParser) validate() error {
if p.cmd.file == "" {
return parseError{message: "missing file", pretty: p.cmd.pretty}
}
if p.seenSheet && !p.spec.allowSheet {
return parseError{message: fmt.Sprintf("flag not allowed: %s", flagSheet), pretty: p.cmd.pretty}
}
if p.seenCell && !p.spec.allowCell {
return parseError{message: fmt.Sprintf("flag not allowed: %s", flagCell), pretty: p.cmd.pretty}
}
if p.seenRange && !p.spec.allowRange {
return parseError{message: fmt.Sprintf("flag not allowed: %s", flagRange), pretty: p.cmd.pretty}
}
if p.seenValue && !p.spec.allowValue {
return parseError{message: fmt.Sprintf("flag not allowed: %s", flagValue), pretty: p.cmd.pretty}
}
if p.seenFormula && !p.spec.allowFormula {
return parseError{message: fmt.Sprintf("flag not allowed: %s", flagFormula), pretty: p.cmd.pretty}
}
if p.spec.requireSheet && p.cmd.sheet == "" {
return parseError{message: fmt.Sprintf("missing %s", flagSheet), pretty: p.cmd.pretty}
}
if p.spec.requireCell && p.cmd.cell == "" {
return parseError{message: fmt.Sprintf("missing %s", flagCell), pretty: p.cmd.pretty}
}
if p.spec.requireRange && p.cmd.cellRange == "" {
return parseError{message: fmt.Sprintf("missing %s", flagRange), pretty: p.cmd.pretty}
}
if p.spec.requireValueOrFormula {
if p.cmd.valueSet && p.cmd.formulaSet {
return parseError{message: fmt.Sprintf("cannot combine %s and %s", flagValue, flagFormula), pretty: p.cmd.pretty}
}
if !p.cmd.valueSet && !p.cmd.formulaSet {
return parseError{message: fmt.Sprintf("missing %s or %s", flagValue, flagFormula), pretty: p.cmd.pretty}
}
}
return nil
}
// normalize canonicalizes parsed cell and range references.
func (p *argParser) normalize() error {
if p.cmd.cell != "" {
normalized, err := normalizeCellRef(p.cmd.cell)
if err != nil {
return parseError{message: err.Error(), pretty: p.cmd.pretty}
}
p.cmd.cell = normalized
}
if p.cmd.cellRange != "" {
normalized, err := parseCellRange(p.cmd.cellRange)
if err != nil {
return parseError{message: err.Error(), pretty: p.cmd.pretty}
}
p.cmd.cellRange = normalized.ref
}
if p.cmd.formulaSet {
p.cmd.formula = strings.TrimPrefix(p.cmd.formula, "=")
if p.cmd.formula == "" {
return parseError{message: fmt.Sprintf("empty %s", flagFormula), pretty: p.cmd.pretty}
}
}
return nil
}
// lookupCommand returns the spec for a supported command.
func lookupCommand(resource, action string) (commandSpec, bool) {
switch {
case resource == resourceWorkbook && action == actionInfo:
return commandSpec{resource: resource, action: action}, true
case resource == resourceSheet && action == actionList:
return commandSpec{resource: resource, action: action}, true
case resource == resourceSheet && action == actionInfo:
return commandSpec{
resource: resource,
action: action,
allowSheet: true,
requireSheet: true,
}, true
case resource == resourceCell && action == actionRead:
return commandSpec{
resource: resource,
action: action,
allowSheet: true,
allowCell: true,
requireSheet: true,
requireCell: true,
}, true
case resource == resourceCell && action == actionSet:
return commandSpec{
resource: resource,
action: action,
allowSheet: true,
allowCell: true,
allowValue: true,
allowFormula: true,
requireCell: true,
requireValueOrFormula: true,
}, true
case resource == resourceCell && action == actionClear:
return commandSpec{
resource: resource,
action: action,
allowSheet: true,
allowCell: true,
requireCell: true,
}, true
case resource == resourceRange && action == actionRead:
return commandSpec{
resource: resource,
action: action,
allowSheet: true,
allowRange: true,
requireSheet: true,
requireRange: true,
}, true
case resource == resourceRange && action == actionClear:
return commandSpec{
resource: resource,
action: action,
allowSheet: true,
allowRange: true,
requireRange: true,
}, true
default:
return commandSpec{}, false
}
}
// readFlagValue reads the value following a flag token.
func readFlagValue(args []string, index int, allowFlagTokenValue bool) (string, int, error) {
next := index + 1
if next >= len(args) || (!allowFlagTokenValue && isFlagToken(args[next])) {
return "", 0, fmt.Errorf("missing value for %s", args[index])
}
return args[next], next, nil
}
// isFlagToken reports whether an argument looks like a flag.
func isFlagToken(arg string) bool {
return len(arg) > 0 && arg[0] == '-'
}