-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcommandline.py
More file actions
164 lines (142 loc) · 5.67 KB
/
commandline.py
File metadata and controls
164 lines (142 loc) · 5.67 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
## November 8, 2006
## Sean Curtis
##
## Class to handle command line arguments.
##
## It assumes command line arguments come in the following forms:
## -param
## -param arg
## -param arg1 arg2 ... argN
## -param must be a variable which starts with an alpha character
##
## The input is the list that sys.argv provides (minus the sys.argv[0])
## Provides a common interface for parsing such an argument list and extracting values
##
## Does NOT allow for duplicate params
class ParamManager:
"""Manages command line arguments and parameters"""
def __init__(self, args = None ):
self.params = {}
if ( args != None ):
self.parseArgs( args )
def __clear( self ):
"""Clears the set"""
self.params = {}
def error( self, msg = "Encountered error parsing arguments" ):
"""Clears the set"""
self.__clear()
raise IOError, msg
def parseArgs( self, args ):
"""Parse the argument list and set up accessors"""
self.__clear()
currParam = None
currArg = []
for i in range( len(args) ):
token = args[i]
if ( token.startswith( '-' ) ):
try:
currArg.append( float(token) ) # valid number is NOT a new param
if ( not currParam ):
self.error("Trying to define a value without having a parameter: %s" % (token))
except ValueError: # if not a valid number, it must be a new param
if ( currParam ):
if ( not currArg ): # if no current arguments, simply store a list of True
self.params[currParam] = (True, )
else:
self.params[currParam] = list(currArg)
currArg = []
currParam = token[1:]
if ( self.params.has_key(currParam) ):
self.error("Parameter defined twice")
else:
if ( not currParam ):
self.error("Trying to define a value without having a parameter: %s" % (token))
currArg.append( token )
if ( currParam ):
if ( not currArg ):
currArg = [True]
self.params[currParam] = list(currArg)
def __getitem__( self, key ):
"""Returns a value if the parameter is defined, None otherwise"""
if self.params.has_key( key ):
return self.params[key]
## Class to handle SIMPLE command line arguments.
##
## Similar to the ParamManger, except it only allows
## unary and binary arguments such as:
## -param1
## -param1 -arg1 -param2 -arg2
class SimpleParamManager:
"""Manages command line arguments and parameters"""
def __init__(self, args = None, defaults = None ):
self.params = {}
if ( args != None ):
self.parseArgs( args, defaults )
def __clear( self ):
"""Clears the set"""
self.params = {}
def error( self, msg = "Encountered error parsing arguments" ):
"""Clears the set"""
self.__clear()
raise IOError, msg
def parseArgs( self, args, defaults ):
"""Parse the argument list and set up accessors"""
self.__clear()
if ( defaults ):
for key, value in defaults.items():
self.params[ key ] = value
i = 0
while ( i < len( args ) ):
token = args[i]
if ( token.startswith( '-' ) ):
currParam = token[1:]
token = args[i + 1]
try:
val = float( token )
isNumber = True
except:
isNumber = False
if ( token.startswith( '-' ) and not isNumber ):
# unary argument - it is SUPPOSED to be binary if it is already entered -- do nothing and use default
if ( not self.params.has_key( currParam ) ):
self.params[ currParam ] = True
i += 1
else:
self.params[ currParam ] = token
i += 2
else:
self.error( 'Expected parameter, found %s' % token )
def __getitem__( self, key ):
"""Returns one of three things: None (if key isn't present)(unless a default was provided), a string if there was a key and an argument, a boolean if it was a unary parameter"""
if self.params.has_key( key ):
return self.params[key]
else:
return None
if __name__ == "__main__":
pMan = ParamManager()
print "\nValid arguments test"
args = ['-in', '-front', '17.3', '-back', '10', '-heights', '3', 'test', 'bob']
pMan.parseArgs( args )
testArgs = ['in', 'front', 'junk', 'heights']
for tArg in testArgs:
val = pMan[tArg]
if ( val != None ):
print "arg %s had value %s" % (tArg, val )
else:
print "arg %s was not defined" % (tArg)
print "\nInvalid: setting value without param"
args = [ '-1', '-in', '-front', '17.3', '-back', '10', '-heights', '3', 'test', 'bob']
try:
pMan.parseArgs( args )
except IOError, e:
print e
else:
print "!!! THIS SHOULDN'T PRINT!!!"
print "\nInvalid: two duplicate parameters"
args = [ '-in', '-front', '17.3', '-back', '10', '-heights', '3', 'test', '-in', 'bob']
try:
pMan.parseArgs( args )
except IOError, e:
print e
else:
print "!!! THIS SHOULDN'T PRINT!!!"