forked from SeattleTestbed/seattlelib_v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargparse.r2py
175 lines (147 loc) · 5.69 KB
/
argparse.r2py
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
165
166
167
168
169
170
171
172
173
174
"""
<Program Name>
argparse.r2py
<Started>
April 27, 2009
<Author>
Konstantin Pik
<Purpose>
Helper file that provides several functions to check for command line
arguments. Similar to getopt()/getopts().
<Sample Usage>
argparse_scan_str(callargs, '-ip')
returns False if no -ip flag
returns string containing ip (parameter following -ip flag)
argparse_scan_bool(callargs, '--local')
returns False if no --local flag is set
returns True if --local flag is set
argparse_scan_int(callargs, '/port')
returns False if no /port flag is set
returns a string (ensures castable to int) following the
Note: False is the default return value for all the functions if a
default return value is not specified on function call.
Note: See individual functions for detailed information.
"""
def argparse_scan_bool(args_array, flag_name, default_value = False):
"""
<Purpose>
This function checks to see if a FLAG (flag_name) was passed as an
argument.
Note: Careful that another parameter will not have the same name as the
flag you're looking for.
<Arguments>
args_array: Complete array of arguments.
flag_name: Flag we're looking for. Can have any naming format,
e.g.: --local or /local or -local
default_value: 'False' if not specified. The default value.
<Exceptions>
None known.
<Side Effects>
None.
<Returns>
Returns True if the flag_name was found in the list of arguments.
Returns default_value if it was not.
<Sample Usage>
if scan_bool(callargs, '--local'): # we have the argument!
... your code ...
"""
for i in range(0, len(args_array)):
if args_array[i].lower() == flag_name:
return True
return default_value # should technically always be False
def argparse_scan_str(args_array, flag_name, default_value = False):
"""
<Purpose>
This function checks to see if a FLAG (flag_name) exists, and then takes
the very next "word" and returns it as the argument.
Note: Careful that another parameter will not have the same name as the
flag you're looking for.
<Arguments>
args_array: Complete array of arguments.
flag_name: Flag we're looking for. Can have any naming format,
e.g.: --local or /local or -local
default_value: 'False' if not specified. The default value.
<Exceptions>
Exception is raised if there is an array out of bounds error, or some
unexpected formatting error (just in case?)
<Side Effects>
None.
<Returns>
If the flag exists, then the value directly following it will be returned
as a string. If the value does not exist, then the function returns the
default_value.
<Sample Usage>
temp_ip = scan_str(callargs, '--ip'):
if temp_ip: # we have an IP!
ip = temp_ip
... your code ...
"""
for i in range(0, len(args_array)):
if args_array[i].lower() == flag_name:
try:
return_value = str(callargs[i + 1])
except IndexError, e:
# This means we're expecting a value and there isn't one..
log(e,'\n')
raise Exception("Expected value for argument '"+callargs[i]+"'")
except Exception, e:
# Some other type of error
log(e,'\n')
raise Exception("Invalid value ("+str(callargs[i + 1])+") specified"+ \
" for parameter "+flag_name)
else:
return return_value
return default_value
def argparse_scan_int(args_array, flag_name, default_value = False):
"""
<Purpose>
This function checks to see if a FLAG (flag_name) exists, and then takes
the very next "word" and returns it as the argument. This function is
different from scan_str as it will expect an integer to follow the
flag name, and will raise an error if the value following is NOT an
integer.
Note: Careful that another parameter will not have the same name as the
flag you're looking for.
<Arguments>
args_array: Complete array of arguments.
flag_name: Flag we're looking for. Can have any naming format,
e.g.: --local or /local or -local
default_value: 'False' if not specified. The default value.
<Exceptions>
Exception is raised if there is an array out of bounds error, or the value
following directly after the flag is not an integer.
<Side Effects>
None.
<Returns>
If the flag exists, then the value directly following it will be returned
as a string (but guarantee to be able to cast to integer type). If the
value does not exist, then the function returns default_value.
<Sample Usage>
temp_port = scan_port(callargs, '/port'):
if temp_port: # we have a port!
port = temp_port
... your code ...
"""
# scan through all the arguments
for i in range(0, len(args_array)):
# case insensitive compare the flag to what we're looking
if args_array[i].lower() == flag_name:
# the try loop checks that it's capable of being cast to an int
try:
return_value = int(callargs[i + 1])
except IndexError, e:
# Expected value, but there are no more values left in args_array
log(e,'\n')
raise Exception("Expected value for argument '"+callargs[i]+"'")
except Exception, e:
# If we get here, it typically means the value is not castable to int
# as we expect, and so we error out.
log(e,'\n')
raise Exception("Invalid value ("+str(callargs[i + 1])+") specified"+ \
" for parameter "+flag_name)
else:
# return callargs[X] and not return_value because we say we return a
# string from this function.
return callargs[i + 1]
return default_value