-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathoptArgs.py
84 lines (55 loc) · 1.83 KB
/
optArgs.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
77
78
79
80
81
82
83
#! /usr/bin/env python2.3
__author__ = 'Andreas Eisele <[email protected]>'
__created__ = "Fri Feb 6 12:41:04 2004"
__date__ = ('$Date: 2004/02/09 08:23:13 $').strip("$")
__version__ = '$Revision: 1.2 $'.strip("$")
# purpose of file:
# a convenience wrapper around optparse
# todo:
# provide more informative help text
# support for boolean values
# completed
from optparse import OptionParser, Values
# provide names that can be imported
global options, arguments, optionsParsed
options=Values()
arguments=[]
optionsParsed=False
def key2opt(name):
if len(name)==1: return "-"+name
return "--"+name
def optParse(usage=None, version=None, **kw):
global options, arguments, optionsParsed
if optionsParsed:
raise "optParse cannot be called twice"
else:
optionsParsed=True
op=OptionParser(usage=usage, version=version)
for key,val in kw.items():
keys=[key2opt(k) for k in key.split("__")]
if type(val) == type(1):
otype="int"
elif type(val) == type(1.0):
otype="float"
elif type(val)==type("") and "|" in val:
choices=val.split("|")
defVal=choices[0]
op.add_option(default=defVal, choices=choices, help="one of: %s, [%s]"%(" ".join(choices),defVal), *keys)
continue
elif val==None:
op.add_option(action="store_true", *keys)
continue
else:
otype="string"
op.add_option(default=val, type=otype, help="[%s]"%val,*keys)
(opts, args)=op.parse_args()
for a in args: arguments.append(a)
options._update_loose(opts.__dict__)
def main():
optParse(iOpt__i=1, sOpt__s="a")
global options, arguments
print "iOpt=",options.iOpt
print "sOpt=",options.sOpt
print "arguments=", arguments
if __name__ == "__main__":
main()