-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_args.go
493 lines (420 loc) · 15 KB
/
parse_args.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
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
package main
import (
"fmt"
"path/filepath"
"regexp"
"strconv"
"strings"
"os"
)
type Args struct {
root []string
series []string
movies []string
options AdditionalOptions
}
type AdditionalOptions struct {
keep_ep_nums Option[bool]
starting_ep_num Option[int]
has_season_0 Option[bool]
naming_scheme Option[string]
}
func new_Args() Args {
return Args{
root: make([]string, 0),
series: make([]string, 0),
movies: make([]string, 0),
options: AdditionalOptions{
has_season_0: none[bool](),
keep_ep_nums: none[bool](),
starting_ep_num: none[int](),
naming_scheme: none[string](),
},
}
}
func parse_args(args []string) (Args, error) {
if len(args) < 1 {
return Args{}, fmt.Errorf("not enough arguments")
}
directory_args := map[string]bool{
"--root": true,
"-r": true,
"--series": true,
"-s": true,
"--movies": true,
"-m": true,
}
assigned := map[string]bool {
"--options": false,
}
parsed_args := new_Args()
skip_iter := 0
for i, arg := range args {
// valid values will be skipped
if skip_iter != 0 && i <= skip_iter {
continue
// catch invalid values acting as flags
} else if arg[0] != '-' {
return Args{}, fmt.Errorf("invalid flag: '%s'", arg)
} else if arg == "--help" || arg == "-h" {
if len(args) <= i+1 {
help("")
} else if len(args) > i+1 {
help(args[i+1])
}
return Args{}, fmt.Errorf("safe exit")
} else if arg == "--version" || arg == "-v" {
welcome_msg(version)
return Args{}, fmt.Errorf("safe exit")
} else if directory_args[arg] {
// no value after flag / flag after flag
if len(args) <= i+1 || (len(args) > i+1 && args[i+1][0] == '-') {
return Args{}, fmt.Errorf("missing dir path value for flag '%s'", arg)
// not a valid directory
} else if _, err := filepath.Abs(args[i+1]); err != nil {
return Args{}, err
}
dir, err := filepath.Abs(args[i+1])
if err != nil {
return Args{}, err
}
if arg == "--root" || arg == "-r" {
parsed_args.root = append(parsed_args.root, dir)
} else if arg == "--series" || arg == "-s" {
parsed_args.series = append(parsed_args.series, dir)
} else if arg == "--movies" || arg == "-m" {
parsed_args.movies = append(parsed_args.movies, dir)
}
skip_iter = i + 1
} else if arg == "--season-0" || arg == "-s0" {
if parsed_args.options.has_season_0.is_some() {
return Args{}, fmt.Errorf("only one --season-0 flag is allowed")
}
// use default value
if len(args) <= i+1 || (len(args) > i+1 && args[i+1][0] == '-') {
parsed_args.options.has_season_0 = some[bool](false)
} else if args[i+1] != "all" && args[i+1] != "var" {
return Args{}, fmt.Errorf("invalid value '%s' for flag '%s'. Must be 'all' or 'var", args[i+1], arg)
} else if args[i+1] == "all" {
if len(args) < i+2 || args[i+2][0] == '-' {
return Args{}, fmt.Errorf("all must be followed by 'yes' or 'no' for --season-0. %s is invalid", args[i+2])
} else if args[i+2] != "yes" && args[i+2] != "no" {
return Args{}, fmt.Errorf("all must be followed by 'yes' or 'no' for --season-0. %s is invalid", args[i+2])
}
var value bool
if args[i+2] == "yes" {
value = true
} else {
value = false
}
parsed_args.options.has_season_0 = some[bool](value)
skip_iter = i + 2
} else if args[i+1] == "var" {
parsed_args.options.has_season_0 = none[bool]()
}
} else if arg == "--keep-ep-nums" || arg == "-ken" {
if parsed_args.options.keep_ep_nums.is_some() {
return Args{}, fmt.Errorf("only one --keep-ep-nums flag is allowed")
}
// use default value
if len(args) <= i+1 || (len(args) > i+1 && args[i+1][0] == '-') {
parsed_args.options.keep_ep_nums = some[bool](false)
} else if args[i+1] != "all" && args[i+1] != "var" {
return Args{}, fmt.Errorf("invalid value '%s' for --keep-ep-nums. Must be 'all' or 'var", args[i+1])
} else if args[i+1] == "all" {
if len(args) < i+2 || args[i+2][0] == '-' {
return Args{}, fmt.Errorf("all must be followed by 'yes' or 'no' for --keep-ep-nums. %s is invalid", args[i+2])
} else if args[i+2] != "yes" && args[i+2] != "no" {
return Args{}, fmt.Errorf("all must be followed by 'yes' or 'no' for --keep-ep-nums. %s is invalid", args[i+2])
}
var value bool
if args[i+2] == "yes" {
value = true
} else {
value = false
}
parsed_args.options.keep_ep_nums = some[bool](value)
skip_iter = i + 2
} else if args[i+1] == "var" {
parsed_args.options.has_season_0 = none[bool]()
}
} else if arg == "--starting-ep-num" || arg == "-sen" {
if parsed_args.options.starting_ep_num.is_some() {
return Args{}, fmt.Errorf("only one --starting-ep-num flag is allowed")
}
// use default value
if len(args) <= i+1 || (len(args) > i+1 && args[i+1][0] == '-') {
parsed_args.options.starting_ep_num = some[int](1)
} else if args[i+1] != "all" && args[i+1] != "var" {
return Args{}, fmt.Errorf("invalid value '%s' for --starting-ep-num. Must be 'all' or 'var", args[i+1])
} else if args[i+1] == "all" {
if len(args) < i+2 || args[i+2][0] == '-' {
return Args{}, fmt.Errorf("all must be followed by a positive int for --starting-ep-num. %s is not a valid positive int", args[i+2])
}
value, err := strconv.Atoi(args[i+2])
if err != nil {
return Args{}, fmt.Errorf("all must be followed by a positive int for --starting-ep-num. %s is not a valid positive int", args[i+2])
}
parsed_args.options.starting_ep_num = some[int](value)
skip_iter = i + 2
} else if args[i+1] == "var" {
parsed_args.options.starting_ep_num = none[int]()
}
} else if arg == "--options" || arg == "-o" {
if assigned["--options"] {
return Args{}, fmt.Errorf("only one --options flag is allowed")
}
// all options are assigned `var`
if len(args) <= i+1 || (len(args) > i+1 && (args[i+1][0] == '-' || args[i+1] == "var")) {
assigned["--options"] = true
parsed_args.options.keep_ep_nums = none[bool]()
parsed_args.options.starting_ep_num = none[int]()
parsed_args.options.has_season_0 = none[bool]()
parsed_args.options.naming_scheme = none[string]()
} else if args[i+1] != "default" && args[i+1] != "var" {
return Args{}, fmt.Errorf("invalid value '%s' for --options. Must be 'default' or 'var", args[i+1])
} else if args[i+1] == "default" {
// use default values
assigned["--options"] = true
parsed_args.options.keep_ep_nums = some[bool](false)
parsed_args.options.starting_ep_num = some[int](1)
parsed_args.options.has_season_0 = some[bool](false)
parsed_args.options.naming_scheme = some[string]("default")
}
} else if arg == "--naming-scheme" || arg == "-ns" {
if parsed_args.options.naming_scheme.is_some() {
return Args{}, fmt.Errorf("only one --naming-scheme flag is allowed")
}
if len(args) <= i+1 || (len(args) > i+1 && args[i+1][0] == '-') {
return Args{}, fmt.Errorf("missing value for --naming-scheme")
} else if args[i+1] != "all" && args[i+1] != "var" {
return Args{}, fmt.Errorf("invalid value '%s' for --naming-scheme. Must be 'all' or 'var", args[i+1])
} else if args[i+1] == "all" {
if len(args) < i+2 || args[i+2][0] == '-' {
return Args{}, fmt.Errorf("'all' must be followed by a naming scheme string enclosed in double quotes")
}
err := validate_naming_scheme(args[i+2])
if err != nil {
return Args{}, err
}
parsed_args.options.naming_scheme = some[string](args[i+2])
skip_iter = i + 2
} else if args[i+1] == "var" {
parsed_args.options.naming_scheme = none[string]()
}
} else {
return Args{}, fmt.Errorf("unknown flag: %s", arg)
}
}
err := validate_roots(parsed_args.root, parsed_args.series, parsed_args.movies)
if err != nil {
return Args{}, err
}
if !assigned["--options"] {
// use default values for additional options
if parsed_args.options.has_season_0.is_none() {
parsed_args.options.has_season_0 = some[bool](false)
}
if parsed_args.options.keep_ep_nums.is_none() {
parsed_args.options.keep_ep_nums = some[bool](false)
}
if parsed_args.options.starting_ep_num.is_none() {
parsed_args.options.starting_ep_num = some[int](1)
}
if parsed_args.options.naming_scheme.is_none() {
parsed_args.options.naming_scheme = some[string]("default")
}
}
return parsed_args, nil
}
func validate_naming_scheme(s string) error {
tokens, err := tokenize_naming_scheme(s)
if err != nil {
return err
}
valid_api := regexp.MustCompile(`^season_num$|^episode_num$|^self$`)
valid_parent_api := regexp.MustCompile(`^parent(-parent)*$|^p(-\d+)?$`)
valid_range := regexp.MustCompile(`^\d+(\s*,\s*\d+)?$`)
for _,token := range tokens {
var api, val string
// for api with values
if strings.Contains(token, ":") {
res := strings.SplitN(token, ":", 2)
api, val = strings.TrimSpace(res[0]), strings.TrimSpace(res[1])
} else {
api, val = strings.TrimSpace(token), "none"
}
if !valid_api.MatchString(api) && !valid_parent_api.MatchString(api) {
return fmt.Errorf("invalid api: %s", api)
}
if api == "season_num" || api == "episode_num" {
if val == "none" {
continue
}
if val == "" {
return fmt.Errorf("season_num's value cannot be empty and must be a positive integer")
}
int_val, err := strconv.Atoi(val)
if err != nil {
return fmt.Errorf("%s's value must be a positive integer. '%s' is not a valid integer or 0", api, val)
}
if int_val < 0 {
return fmt.Errorf("%s's value must be a positive integer. '%s' is not a positive integer or 0", api, val)
}
} else if api == "self" || valid_parent_api.MatchString(api) {
if val == "none" {
continue
}
if val == "" {
return fmt.Errorf("%s's value cannot be empty", api)
}
if val[0] == '\'' {
single_closed_string := regexp.MustCompile(`^'[^']*'$`)
if !single_closed_string.MatchString(val) {
return fmt.Errorf("%s is either an unclosed string or contains more than 2 single quotes", val)
}
val_trimmed := strings.Trim(val, "'")
just_whitespace := regexp.MustCompile(`^\s*$`)
if just_whitespace.MatchString(val_trimmed) {
return fmt.Errorf("%s is an empty string (just whitespace/s)", val)
}
if _, err := regexp.Compile(val_trimmed); err != nil {
return fmt.Errorf("invalid regex: %s", val)
} else {
parts := split_regex_by_pipe(val_trimmed)
for _, part := range parts {
if !has_only_one_match_group(part) {
return fmt.Errorf("regex should have only one match group per part (parts are separated by outermost pipes |): %s", val)
}
}
// valid regex
continue
}
} else if !valid_range.MatchString(val) {
return fmt.Errorf("%s's value must be in the format <start>,<end> where <start> and <end> are positive integers and 0. '%s' is not a valid range", api, val)
}
// valid range
var res []string
if strings.Contains(val, ",") {
res = strings.SplitN(val, ",", 2)
} else {
res = []string{val, val}
}
begin, end := strings.TrimSpace(res[0]), strings.TrimSpace(res[1])
if begin > end {
return fmt.Errorf("%s is an invalid range. begin (%s) must be less than or equal to end (%s)", val, begin, end)
}
}
}
return nil
}
func tokenize_naming_scheme(s string) ([]string, error) {
is_token := false
builder := strings.Builder{}
naming_scheme := make([]string, 0)
for i, c := range s {
if is_token && i+1 == len(s) && c != '>' {
return nil, fmt.Errorf("reached end of string but still in an unclosed api: '<%s%s'", builder.String(), string(c))
}
// start of token
if c == '<' && !is_token {
is_token = true
builder.Reset()
continue
// end of token
} else if c == '>' && is_token {
is_token = false
naming_scheme = append(naming_scheme, builder.String())
builder.Reset()
continue
}
if is_token {
_, err := builder.WriteRune(c)
if err != nil {
return nil, err
}
}
}
return naming_scheme, nil
}
func validate_roots(root []string, series []string, movies []string) error {
// must at least have one of any
if len(root) == 0 && len(series) == 0 && len(movies) == 0 {
return fmt.Errorf("must specify at least one root directory")
}
// check if exists
for _, r := range root {
if _, err := os.Stat(r); err != nil {
return fmt.Errorf("root directory %s does not exist", r)
}
}
for _, r := range series {
if _, err := os.Stat(r); err != nil {
return fmt.Errorf("series directory %s does not exist", r)
}
}
for _, r := range movies {
if _, err := os.Stat(r); err != nil {
return fmt.Errorf("movies directory %s does not exist", r)
}
}
// check if any of the series and movies directories are subdirectories of a root directory OR vice versa
// and in turn checking if any of the series and movies directories are duplicates of root directories
for _, r := range root {
for _, s := range series {
if strings.EqualFold(filepath.Dir(s), r) {
return fmt.Errorf("series directory %s is a subdirectory of root directory %s", s, r)
} else if strings.EqualFold(filepath.Dir(r), s) {
return fmt.Errorf("root directory %s is a subdirectory of series directory %s", r, s)
} else if strings.EqualFold(s, r) {
return fmt.Errorf("series directory %s is a duplicate of root directory %s", s, r)
}
}
for _, m := range movies {
if strings.EqualFold(filepath.Dir(m), r) {
return fmt.Errorf("movies directory %s is a subdirectory of root directory %s", m, r)
} else if strings.EqualFold(filepath.Dir(r), m) {
return fmt.Errorf("root directory %s is a subdirectory of movies directory %s", r, m)
} else if strings.EqualFold(m, r) {
return fmt.Errorf("movies directory %s is a duplicate of root directory %s", m, r)
}
}
}
// check if any of the series and movies directories are subdirectories of each other
// and in turn checking if any of the series and movies directories are duplicates of each other
for _, s := range series {
for _, m := range movies {
if strings.EqualFold(filepath.Dir(m), s) {
return fmt.Errorf("series directory %s is a subdirectory of movies directory %s", s, m)
} else if strings.EqualFold(filepath.Dir(s), m) {
return fmt.Errorf("movies directory %s is a subdirectory of series directory %s", m, s)
} else if strings.EqualFold(s, m) {
return fmt.Errorf("series directory %s is a duplicate of movies directory %s", s, m)
}
}
}
// check if there are any duplicates in each individually
for i, r := range root {
for _, r1 := range root[i+1:] {
if strings.EqualFold(r, r1) {
return fmt.Errorf("there are multiple root directories that share the same path: %s", r)
}
}
}
for i, s := range series {
for _, s1 := range series[i+1:] {
if strings.EqualFold(s, s1) {
return fmt.Errorf("there are multiple series directories that share the same path: %s", s)
}
}
}
for i, m := range movies {
for _, m1 := range movies[i+1:] {
if strings.EqualFold(m, m1) {
return fmt.Errorf("there are multiple movies directories that share the same path: %s", m)
}
}
}
// all done
return nil
}