-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.v
292 lines (254 loc) · 6.32 KB
/
parse.v
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
module vargus
// parse_flags is the main flag parser
// it parses all flags in the osargs
fn (c &Commander) parse_flags(osargs []string, gflags []FlagArgs, cfg CommandConfig) ([]string, []FlagArgs) {
mut flags := []FlagArgs{}
mut args := osargs.clone()
mut all_flags := c.flags.clone()
// append
all_flags << gflags
// extract values
for i in osargs {
for ic, cmd in all_flags {
mut x := cmd
mut found := false
mut flag_value := ''
mut f_op := ''
long := '--$cmd.name'
short := '-$cmd.short_arg'
if i.starts_with(long) || i.starts_with(short) {
// check if equals sign is used
if i.contains("=") {
y := args[args.index(i)].split('=')
flag := y[0]
flag_value = y[1]
if flag == long || flag == short {
// set found
found = true
f_op = '='
// remove from osargs
args.delete(args.index(i))
}
} else {
val := args[args.index(i) + 1] or {
''
}
if i == long || i == short {
// simple validation for set values on flags
flag_value = args[args.index(val)] or {
if x.data_type != .boolean {
// show blank error
if cfg.errors.use_custom_blank {
cfg.errors.blank(i)
} else {
c.blank_err(i)
}
exit(1)
}
''
}
// set found
found = true
// remove from osargs
if val != '' {
if x.data_type == .boolean{
// remove value if valid bool
if bool_validator(val) {
args.delete(args.index(val))
}
} else {
args.delete(args.index(val))
}
}
if i in args {
args.delete(args.index(i))
} else {
// show blank error
if cfg.errors.use_custom_blank {
cfg.errors.blank(osargs[osargs.index(i) - 1])
} else {
c.blank_err(osargs[osargs.index(i) - 1])
}
exit(1)
}
}
}
// if flag is present / parsed
if found {
// get value
x.value = c.parse_value(flag_value, x.data_type, i, f_op, cfg)
// append new flag w/ value to flags
flags << x
// remove old flag w/ value from all
all_flags.delete(ic)
}
}
}
}
prsd_helper_flags := c.parse_helper(all_flags, cfg)
// append parsed flags from helper
flags << prsd_helper_flags
// another checker for flags
c.validate_final_args(args)
// return the final args and flags parsed
return args, flags
}
// parse_value is a value parser and validator
// it checks if set value's data_type is similar to flag's
fn (c &Commander) parse_value(val string, dtype FlagDataType, flag string, flag_op string, cfg CommandConfig) string {
mut value := ''
mut valid := false
// validate values
match dtype {
.string_var {
if cfg.validators.use_custom_string_var {
valid = cfg.validators.string_var(val)
} else {
valid = true
}
if valid {
value = val
} else {
if cfg.errors.use_custom_value {
cfg.errors.value(flag, 'string')
} else {
c.value_err(flag, 'string')
}
exit(1)
}
}
.integer {
if cfg.validators.use_custom_integer {
valid = cfg.validators.integer(val)
} else {
valid = int_validator(val)
}
if valid {
value = val
} else {
if cfg.errors.use_custom_value {
cfg.errors.value(flag, 'int')
} else {
c.value_err(flag, 'int')
}
exit(1)
}
}
.float {
if cfg.validators.use_custom_float {
valid = cfg.validators.float(val)
} else {
valid = float_validator(val)
}
if valid {
value = val
} else {
if cfg.errors.use_custom_value {
cfg.errors.value(flag, 'float')
} else {
c.value_err(flag, 'float')
}
exit(1)
}
}
.boolean {
if cfg.validators.use_custom_boolean {
valid = cfg.validators.boolean(val)
} else {
valid = bool_validator(val)
}
if valid {
value = val
} else {
if flag_op == '=' {
// if the value set is not equal to true or false,
// show value error
if cfg.errors.use_custom_value {
cfg.errors.value(flag, 'bool')
} else {
c.value_err(flag, 'bool')
}
exit(1)
} else {
// if the value set is not equal to true or false,
// parse it but next arg will be parsed to args
value = 'true'
}
}
}
}
// return value
return value
}
// parse_helper is a helper to the main parser
// it verifies values of flags and checks if they are required or not
fn (c &Commander) parse_helper(flags []FlagArgs, cfg CommandConfig) []FlagArgs {
mut fl := flags.clone()
for mut i in fl {
if i.value == '' {
// if required show error
if i.required == true {
if cfg.errors.use_custom_required {
cfg.errors.required(i.name, i.short_arg)
} else {
c.required_err(i.name, i.short_arg)
}
exit(1)
}
// otherwise, set value with default
i.value = i.default_value
}
}
return fl
}
// parse_config parses the config set if there is
fn (c &Commander) parse_config(p_config CommandConfig) CommandConfig {
mut cfg := p_config
if c.config.use_custom_help {
cfg.custom_help = c.config.custom_help
}
// custom error messages
if c.config.errors.use_custom_required {
cfg.errors.required = c.config.errors.required
}
if c.config.errors.use_custom_value {
cfg.errors.value = c.config.errors.value
}
if c.config.errors.use_custom_blank {
cfg.errors.blank = c.config.errors.blank
}
if c.config.errors.use_custom_unknown {
cfg.errors.unknown = c.config.errors.unknown
}
if c.config.errors.use_custom_command {
cfg.errors.command = c.config.errors.command
}
// end custom error messages
// custom flag validators
if c.config.validators.use_custom_integer {
cfg.validators.integer = c.config.validators.integer
}
if c.config.validators.use_custom_string_var {
cfg.validators.string_var = c.config.validators.string_var
}
if c.config.validators.use_custom_float {
cfg.validators.float = c.config.validators.float
}
if c.config.validators.use_custom_boolean {
cfg.validators.boolean = c.config.validators.boolean
}
// return new config
return cfg
}
// parse_subcommands parses the subcommands into a new list
fn parse_subcommands(commands []&Commander) []HelpSubcommands {
mut h_commands := []HelpSubcommands{}
for i in commands {
h_commands << HelpSubcommands{
command: i.command
long_desc: i.long_desc
short_desc: i.short_desc
}
}
return h_commands
}