-
Notifications
You must be signed in to change notification settings - Fork 5
/
options.py
46 lines (40 loc) · 1.41 KB
/
options.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
import sys
import getopt
class Options(object):
def __init__(self, mandatory, optional=[], switches=[]):
self.opts = {}
self.__mand = mandatory
self.__optn = optional
self.__swtc = switches
def __getattr__(self, attribute):
if attribute in self.opts:
return self.opts[attribute]
return None
def usage(self, notes=""):
msg = "usage: %s" % (sys.argv[0])
for option in self.__mand:
msg += " --%s <%sname>" % (option, option)
for option in self.__optn:
msg += " [--%s <%sname>]" % (option, option)
for option in self.__swtc:
msg += " [--%s]" % (option)
if notes != "":
msg += "\nnotes: "
msg += notes
print(msg)
def parse(self):
opts = []
for option in self.__mand:
opts.append(option + "=")
for option in self.__optn:
opts.append(option + "=")
for option in self.__swtc:
opts.append(option)
results = getopt.getopt(sys.argv[1:], "", opts)
if len(results[1]) > 0:
raise RuntimeError("unknown argument(s) '%s'" % (results[1]))
for opt in results[0]:
self.opts[opt[0][2:]] = opt[1]
for option in self.__mand:
if option not in self.opts:
raise RuntimeError("mandatory option '--%s' not found" % (option))