-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharghelpers.py
68 lines (56 loc) · 2.05 KB
/
arghelpers.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
import argparse
import array
class ArrayRangeAction(argparse.Action):
def __init__(self, *args, typecode='I', **kwargs):
super().__init__(*args, **kwargs)
assert typecode.isupper(), 'no unsigned types allowed'
self.typecode = typecode
def __call__(self, parser, namespace, arg_value, option_string=None):
dest = getattr(namespace, self.dest)
if not dest:
dest = array.array(self.typecode)
for arg in arg_value.split(','):
low, sep, high = arg.partition('-')
try:
if not sep:
dest.append(int(arg, 0))
else:
# range is inclusive
dest.extend(range(int(low, 0), int(high, 0) + 1))
except OverflowError:
raise argparse.ArgumentError(self,
'value(s) must be >= 0 and <= {}'
.format
((1 << dest.itemsize * 8) - 1))
except ValueError:
raise argparse.ArgumentError(self, 'invalid range')
setattr(namespace, self.dest, dest)
class ListAction(argparse.Action):
def __call__(self, parser, namespace, arg_value, option_string=None):
dest = getattr(namespace, self.dest)
if not dest:
dest = []
if isinstance(arg_value, list):
dest.extend(arg_value)
else:
dest.extend(arg_value.split(','))
setattr(namespace, self.dest, dest)
def integer_type(s):
try:
return int(s, 0)
except ValueError:
raise argparse.ArgumentTypeError('{} cannot be converted to an integer'
.format(s))
def true_false_other_type(s):
b = s.lower()
if b == 'true':
return True
elif b == 'false':
return False
else:
return s
def convert_arg_line_to_args(arg_line):
for arg in arg_line.split():
if not arg.strip():
continue
yield arg