-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargparse.py
76 lines (64 loc) · 2.51 KB
/
argparse.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
# Argument parsing utilities
# Imports
import copy
import argparse
# Custom argparse type representing a bounded int
class IntRange:
def __init__(self, imin=None, imax=None):
self.imin = imin
self.imax = imax
def __call__(self, arg):
try:
value = int(arg)
except ValueError:
raise self.exception()
if (self.imin is not None and value < self.imin) or (self.imax is not None and value > self.imax):
raise self.exception()
return value
def exception(self):
if self.imin is not None and self.imax is not None:
return argparse.ArgumentTypeError(f"Must be an integer in the range [{self.imin}, {self.imax}]")
elif self.imin is not None:
return argparse.ArgumentTypeError(f"Must be an integer >= {self.imin}")
elif self.imax is not None:
return argparse.ArgumentTypeError(f"Must be an integer <= {self.imax}")
else:
return argparse.ArgumentTypeError("Must be an integer")
# Custom argparse type representing a bounded float
class FloatRange:
def __init__(self, imin=None, imax=None):
self.imin = imin
self.imax = imax
def __call__(self, arg):
try:
value = float(arg)
except ValueError:
raise self.exception()
if (self.imin is not None and value < self.imin) or (self.imax is not None and value > self.imax):
raise self.exception()
return value
def exception(self):
if self.imin is not None and self.imax is not None:
return argparse.ArgumentTypeError(f"Must be an float in the range [{self.imin}, {self.imax}]")
elif self.imin is not None:
return argparse.ArgumentTypeError(f"Must be a float >= {self.imin}")
elif self.imax is not None:
return argparse.ArgumentTypeError(f"Must be a float <= {self.imax}")
else:
return argparse.ArgumentTypeError("Must be an float")
# Custom argparse action to append data to a list as tuples
class AppendData(argparse.Action):
# noinspection PyShadowingBuiltins
def __init__(self, option_strings, dest, key, nargs=0, const=None, default=None, type=None, required=False, help=None, metavar=None):
super(AppendData, self).__init__(option_strings=option_strings, dest=dest, nargs=nargs, const=const, default=default, type=type, required=required, help=help, metavar=metavar)
self.key = key
def __call__(self, parser, namespace, values, option_string=None):
if getattr(namespace, self.dest, None) is None:
setattr(namespace, self.dest, [])
items = copy.copy(getattr(namespace, self.dest))
if not values:
items.append((self.key, None))
else:
items.append((self.key, values))
setattr(namespace, self.dest, items)
# EOF