-
Notifications
You must be signed in to change notification settings - Fork 0
/
args_parser.py
517 lines (421 loc) · 18.2 KB
/
args_parser.py
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
'''
Created on Jul 19, 2015
@author: Tommi Unruh
'''
import re
import copy
class ModeArgsParser(object):
'''
classdocs
'''
KEY_MODE = "mode"
KEY_ORDER = "order"
KEY_EXPLANATION = "key_explanation"
KEY_ARGS_OPTIONAL = "optional_args"
KEY_ARGS_NECESSARY = "necessary_args"
KEY_ARGS_OPTIONAL_WVAL = "optional_args_w_value"
KEY_ARGS_NECESSARY_WVAL = "necessary_args_w_value"
def __init__(self):
'''
Constructor
'''
self.combinations = {}
def addArgumentsCombination(self, mode, necessary_args=None,
optional_args=None, order=None,
explanation=None):
"""
Prepare a dictionary of necessary and optional values,
with and without values respectively.
"""
self.combinations[mode] = {
self.KEY_ORDER: [],
self.KEY_EXPLANATION: None,
self.KEY_ARGS_OPTIONAL: [],
self.KEY_ARGS_NECESSARY: [],
self.KEY_ARGS_OPTIONAL_WVAL: [],
self.KEY_ARGS_NECESSARY_WVAL: [],
}
# Parse necessary arguments.
if necessary_args:
# Parse short versions first
for s_arg, l_arg in necessary_args:
# If a key ends in "=", we expect it to
# be a key-value pair.
if s_arg:
if s_arg[-1] == "=":
(self.combinations[mode]
[self.KEY_ARGS_NECESSARY_WVAL].append(
[s_arg[:-1], l_arg]
))
else:
# Key does not end in "=".
(self.combinations[mode]
[self.KEY_ARGS_NECESSARY].append(
[s_arg, l_arg]
))
elif not l_arg:
# s_arg and l_arg are both None, which is not correct.
raise NoneTypeCombinationException()
# Parse optional arguments.
if optional_args:
# Parse short versions first
for s_arg, l_arg in optional_args:
# If a key ends in "=", we expect it to
# be a key-value pair.
if s_arg:
if s_arg[-1] == "=":
(self.combinations[mode]
[self.KEY_ARGS_OPTIONAL_WVAL].append(
[s_arg[:-1], l_arg]
))
else:
# Key does not end in "=".
(self.combinations[mode]
[self.KEY_ARGS_OPTIONAL].append(
[s_arg, l_arg]
))
elif not l_arg:
# s_arg and l_arg are both None, which is not correct.
raise NoneTypeCombinationException()
# Setup order of arguments.
# This is important for returning the results.
# Arguments on the command line can be mixed up!
if order:
self.combinations[mode][self.KEY_ORDER] = order
else:
# No order specified, so build the default one:
# Necessary arguments first, as specified. Then optional ones.
if necessary_args:
for s_arg, l_arg in necessary_args:
if s_arg[-1] == "=":
self.combinations[mode][self.KEY_ORDER].append(
s_arg[:-1]
)
else:
self.combinations[mode][self.KEY_ORDER].append(
s_arg
)
if optional_args:
for s_arg, l_arg in optional_args:
if s_arg[-1] == "=":
self.combinations[mode][self.KEY_ORDER].append(
s_arg[:-1]
)
else:
self.combinations[mode][self.KEY_ORDER].append(
s_arg
)
if explanation:
self.combinations[mode][self.KEY_EXPLANATION] = explanation
# Create a duplicate of combinations as a helper variable.
# It is necessary to construct the usage() message.
self.combinations_helper = copy.deepcopy(self.combinations)
def parseMode(self, arg):
"""
Check if mode ('arg') is implemented.
"""
mode = None
arg = arg.strip()
if arg[0] == "-":
raise WrongFormatException(arg)
else:
# Check if this mode is available.
for key in self.combinations:
if key == arg:
mode = arg
break
if mode:
return mode
else:
raise WrongModeException(arg)
def parseArgs(self, mode, args):
# Expects args[0] to be a mode value,
# i.e. it should not have a minus sign in front of it.
mode = self.parseMode(mode)
return self.getOpts(mode, args)
def getOpts(self, mode, args):
"""
Parse args and return them in order, as specified by self.combinations.
"""
# Remark: re_short_option will also match long options.
# Therefore, look for long options first, then for short options.
re_long_option = re.compile("--([a-zA-Z]+)")
re_short_option = re.compile("-([a-zA-Z]+)")
result = {}
skip = False
parsed_vals = {}
for i, _ in enumerate(args):
if not skip:
key = None
full_key = None
# Check for long option.
long_hit = re_long_option.match(args[i])
if long_hit:
key = long_hit.group(1)
full_key = long_hit.group(0)
else:
# No long option found, check for short option.
short_hit = re_short_option.match(args[i])
if short_hit:
key = short_hit.group(1)
full_key = short_hit.group(0)
if not key:
# No short, no long option found.
raise WrongFormatException(args[i])
val = self.parseNextKeyValue(args, i)
if val:
skip = True
# Check if key-val pair is correct for this command.
is_permitted = self.argPermitted(full_key, val, mode)
if is_permitted:
result[key] = val
else:
skip = False
# Are necessary arguments still missing?
if self.isMissingArgs(self.combinations[mode]):
raise MissingParameterException(self.combinations[mode])
# Add mode to result
parsed_vals[self.KEY_MODE] = mode
# Bring arguments in order.
# for elem in self.combinations[mode][self.KEY_ORDER]:
# if elem in result:
for key in result:
parsed_vals[key] = result[key]
return parsed_vals
def parseNextKeyValue(self, args, i):
"""
Check next argument for a given value for this key.
"""
val = None
if len(args) > i + 1:
parsed_val = args[i+1]
if len(parsed_val) > 1 and parsed_val[0:2] != "--" and parsed_val[0] != "-":
val = parsed_val
elif len(parsed_val) == 1 and parsed_val != "-":
val = parsed_val
return val
def isMissingArgs(self, combination):
if (
combination[self.KEY_ARGS_NECESSARY] or
combination[self.KEY_ARGS_NECESSARY_WVAL]
):
return True
def argPermitted(self, key, val, mode):
"""
Check if a given key-val pair is correctly specified.
If so, remove it from the combination dictionary, so that
it will be ignored for further parsing.
"""
KEY_SHORT = 0
KEY_LONG = 1
combination = self.combinations[mode]
found_permitted_arg = False
orig_key = key
key_type = -1
# clear key from leading minuses. (e.g. --abc or -abc = abc)
if key[0] == "-":
key = key[1:]
key_type = KEY_SHORT
if key[0] == "-":
key = key[1:]
key_type = KEY_LONG
# Check if value is permitted in keys which do not need a value.
for i, combinations_key in enumerate(
combination[self.KEY_ARGS_NECESSARY]
):
if (
key_type == KEY_SHORT and combinations_key[KEY_SHORT] == key or
key_type == KEY_LONG and combinations_key[KEY_LONG] == key
):
# Key found.
# Was a value given?
if val:
raise UnneccessaryValueException(orig_key)
else:
combination[self.KEY_ARGS_NECESSARY].pop(i)
found_permitted_arg = True
if not found_permitted_arg:
# Check if value is permitted in keys which do need a value.
for i, combinations_key in enumerate(
combination[self.KEY_ARGS_NECESSARY_WVAL]
):
if (
key_type == KEY_SHORT and combinations_key[KEY_SHORT] == key or
key_type == KEY_LONG and combinations_key[KEY_LONG] == key
):
# Key found.
# Was a value given?
if val:
combination[self.KEY_ARGS_NECESSARY_WVAL].pop(i)
found_permitted_arg = True
else:
raise MissingValueException(orig_key)
if not found_permitted_arg:
# Check if value is permitted in optional keys
# which do not need a value.
for i, combinations_key in enumerate(
combination[self.KEY_ARGS_OPTIONAL]
):
if (
key_type == KEY_SHORT and combinations_key[KEY_SHORT] == key or
key_type == KEY_LONG and combinations_key[KEY_LONG] == key
):
# Key found.
# Was a value given?
if val:
raise UnneccessaryValueException(orig_key)
else:
combination[self.KEY_ARGS_OPTIONAL].pop(i)
found_permitted_arg = True
if not found_permitted_arg:
# Check if value is permitted in optional keys
# which do need a value.
for i, combinations_key in enumerate(
combination[self.KEY_ARGS_OPTIONAL_WVAL]
):
if (
key_type == KEY_SHORT and combinations_key[KEY_SHORT] == key or
key_type == KEY_LONG and combinations_key[KEY_LONG] == key
):
# Key found.
# Was a value given?
if val:
combination[self.KEY_ARGS_OPTIONAL_WVAL].pop(i)
found_permitted_arg = True
else:
raise MissingValueException(orig_key)
if not found_permitted_arg:
raise WrongParameterException(mode, orig_key)
return found_permitted_arg
def printHelp(self, arg0):
"""
Print usage.
"""
# Construct usage string
usage = (
"Usage: python " + str(arg0) + " MODE necessary_arg0, necessary_arg1"
", .. optional_arg0, optional_arg1, ...\n"
)
# Print all modes.
modes = "\nMODES: "
for key in self.combinations_helper:
modes += str(key) + ", "
modes = modes[:-2] + "\n"
args = "\nMODE ARGS [OPTIONAL_ARGS]:\n"
# Construct mode-argument combination-strings.
for mode in self.combinations_helper:
counter = 0
arg = "\t" + mode + "\t\t"
for key in self.combinations_helper[mode][self.KEY_ARGS_NECESSARY_WVAL]:
arg += "-" + str(key[0])
if key[1]:
arg += "/--" + str(key[1])
arg += " arg" + str(counter) + " "
counter += 1
for key in self.combinations_helper[mode][self.KEY_ARGS_NECESSARY]:
arg += "-" + str(key[0])
if key[1]:
arg += "/--" + str(key[1]) + " "
if (
self.combinations_helper[mode][self.KEY_ARGS_OPTIONAL_WVAL] or
self.combinations_helper[mode][self.KEY_ARGS_OPTIONAL]
):
arg += "["
for key in self.combinations_helper[mode][self.KEY_ARGS_OPTIONAL_WVAL]:
arg += "-" + str(key[0])
if key[1]:
arg += "/--" + str(key[1])
arg += " arg" + str(counter) + ", "
counter += 1
for key in self.combinations_helper[mode][self.KEY_ARGS_OPTIONAL]:
arg += "-" + str(key[0])
if key[1]:
arg += "/--" + str(key[1]) + ", "
if (
self.combinations_helper[mode][self.KEY_ARGS_OPTIONAL_WVAL] or
self.combinations_helper[mode][self.KEY_ARGS_OPTIONAL]
):
arg = arg[:-2] + "]"
args += arg + "\n"
# Also print explanations for each mode.
explanations = "\nDESCRIPTION:\n"
tabulator = "\t"
for key in self.combinations_helper:
if self.combinations_helper[key][self.KEY_EXPLANATION]:
explanation = "Mode: " + str(key) + "\n" + tabulator
explanation += self.combinations_helper[key][self.KEY_EXPLANATION]
explanations += explanation + "\n\n"
print (usage + modes + args + explanations),
class WrongModeException(BaseException):
def __init__(self, val=None):
self.val = val
def __str__(self):
if self.val:
return "Mode '%s' is not implemented." % self.val
else:
return "Given mode is not implemented."
class WrongFormatException(BaseException):
def __init__(self, val=None):
self.val = val
def __str__(self):
if self.val:
return "Argument '%s' is malformed." % self.val
else:
return "An argument is malformed."
class NoneTypeCombinationException(BaseException):
def __str__(self):
return "Combination cannot contain combination [None, None]."
class MissingValueException(BaseException):
def __init__(self, val=None):
self.val = val
def __str__(self):
if self.val:
return "You did not specify a value for key '%s'." % self.val
else:
return "You did not specify a necessary value."
class MissingParameterException(BaseException):
def __init__(self, combinations=None):
self.combinations = combinations
def __str__(self):
KEY_ARGS_NECESSARY = "necessary_args"
KEY_ARGS_NECESSARY_WVAL = "necessary_args_w_value"
if self.combinations:
missing = ""
for _list in self.combinations[KEY_ARGS_NECESSARY]:
if _list[1] != None:
missing += "-%s/--%s, " % (_list[0], _list[1])
else:
missing += "-%s, " % (_list[0])
for _list in self.combinations[KEY_ARGS_NECESSARY_WVAL]:
if _list[1] != None:
missing += "-%s/--%s, " % (_list[0], _list[1])
else:
missing += "-%s, " % (_list[0])
missing = missing[:-2]
return "Missing parameters: %s" % missing
else:
return "Missing parameters. Aborting..."
class UnneccessaryValueException(BaseException):
def __init__(self, val=None):
self.val = val
def __str__(self):
if self.val:
return (
"You did specify a value for key '%s',"
" but it does not need one." % self.val
)
else:
return (
"You did specify a value for a key,"
" which does not need one."
)
class WrongParameterException(BaseException):
def __init__(self, mode, param):
self.mode = mode
self.param = param
def __str__(self):
return (
"Parameter '%s' is not allowed for command '%s'." % (
self.param, self.mode
)
)