forked from eerimoq/argparse_addons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargparse_addons.py
executable file
·32 lines (23 loc) · 998 Bytes
/
argparse_addons.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
import argparse
__version__ = '0.6.0'
class Integer:
def __init__(self, minimum=None, maximum=None):
self.minimum = minimum
self.maximum = maximum
def __call__(self, string):
value = int(string, 0)
if self.minimum is not None and self.maximum is not None:
if not self.minimum <= value <= self.maximum:
raise argparse.ArgumentTypeError(
'{} is not in the range {}..{}'.format(string, self.minimum, self.maximum))
elif self.minimum is not None:
if value < self.minimum:
raise argparse.ArgumentTypeError(
'{} is not in the range {}..inf'.format(string, self.minimum))
elif self.maximum is not None:
if value > self.maximum:
raise argparse.ArgumentTypeError(
'{} is not in the range -inf..{}'.format(string, self.maximum))
return value
def __repr__(self):
return 'integer'