-
Notifications
You must be signed in to change notification settings - Fork 5
/
option_parser.py
executable file
·61 lines (44 loc) · 1.26 KB
/
option_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
import optparse
delimiter = '-'
def floatable(str):
try:
float(str)
return True
except ValueError:
return False
def intable(str):
try:
int(str)
return True
except ValueError:
return False
def process_floats(option, opt_str, value, parser):
assert value is None
value = {}
for arg in parser.rargs:
# stop on --foo like options
if arg[:2] == "--" and len(arg) > 2:
break
# stop on -a, but not on -3 or -3.0
if arg[:1] == "-" and len(arg) > 1 and not floatable(arg):
break
tokens = arg.split("=")
value[tokens[0]] = float(tokens[1])
del parser.rargs[:len(value)]
setattr(parser.values, option.dest, value)
return
def process_ints(option, opt_str, value, parser):
assert value is None
value = {}
for arg in parser.rargs:
# stop on --foo like options
if arg[:2] == "--" and len(arg) > 2:
break
# stop on -a, but not on -3 or -3.0
if arg[:1] == "-" and len(arg) > 1 and not int(arg):
break
tokens = arg.split("=")
value[tokens[0]] = int(tokens[1])
del parser.rargs[:len(value)]
setattr(parser.values, option.dest, value)
return