8 system_string --The string from the command line
10 options_dict -- a dictionary with the key equal to the opition, and
11 a list describing that option. (See below)
16 A tupple. The first item in the tupple is a dictionary containing
17 the arguments for each options. The second is a list of the
20 If invalid options are passed to the module, 0,0 is returned.
24 Your script has the option '--indents', and '--output=file'.
26 You want to give short option names as well:
32 options_dict = {'output': [1, 'o'],
36 options_obj = ParseOptions(
37 system_string = sys.argv,
38 options_dict = options_dict
41 options, arguments = options_obj.parse_options()
48 {indents:None, output:'/home/paul/file'}, ['/home/paul/input']
52 def __init__(self
, system_string
, options_dict
):
53 self
.__system
_string
= system_string
[1:]
54 long_list
= self
.__make
_long
_list
_func
(options_dict
)
56 short_list
= self
.__make
_short
_list
_func
(options_dict
)
58 self
.__legal
_options
= long_list
+ short_list
59 # # print self.__legal_options
60 self
.__short
_long
_dict
= self
.__make
_short
_long
_dict
_func
(options_dict
)
61 # # print self.__short_long_dict
62 self
.__opt
_with
_args
= self
.__make
_options
_with
_arg
_list
(options_dict
)
63 # # print self.__opt_with_args
64 self
.__options
_okay
= 1
68 def __make_long_list_func(self
, options_dict
):
72 options_dict -- the dictionary mapping options to a list
76 a list of legal options
83 keys
= options_dict
.keys()
86 legal_list
.append(key
)
89 def __make_short_list_func(self
, options_dict
):
93 options_dict --the dictionary mapping options to a list
97 a list of legal short options
101 keys
= options_dict
.keys()
103 values
= options_dict
[key
]
105 legal_list
.append('-' + values
[1])
113 def __make_short_long_dict_func(self
, options_dict
):
117 options_dict --the dictionary mapping options to a list
121 a dictionary with keys of short options and values of long options
125 keys
= options_dict
.keys()
127 values
= options_dict
[key
]
129 short
= '-' + values
[1]
131 short_long_dict
[short
] = long
135 return short_long_dict
137 def __make_options_with_arg_list(self
, options_dict
):
141 options_dict --the dictionary mapping options to a list
145 a list of options that take arguments.
149 keys
= options_dict
.keys()
151 values
= options_dict
[key
]
154 opt_with_arg
.append('--' + key
)
161 def __sub_short_with_long(self
):
173 iterate through the system string and replace short options with long options
178 sub_list
= self
.__short
_long
_dict
.keys()
179 for item
in self
.__system
_string
:
181 item
= self
.__short
_long
_dict
[item
]
182 new_string
.append(item
)
187 def __pair_arg_with_option(self
):
197 nothing (changes value of self.__system_string)
201 iterate through the system string, and match arguments with options:
203 old_list = ['--foo', 'bar']
205 new_list = ['--foo=bar'
208 opt_len
= len(self
.__system
_string
)
209 new_system_string
= []
212 for arg
in self
.__system
_string
:
214 # previous value was an option with an argument, so this arg is
215 # actually an argument that has already been added
221 # not an option--an argument
223 new_system_string
.append(arg
)
225 # option and argument already paired
227 new_system_string
.append(arg
)
229 # this option takes an argument
230 if arg
in self
.__opt
_with
_args
:
231 # option is the last in the list
232 if counter
+ 1 > opt_len
:
233 sys
.stderr
.write('option "%s" must take an argument\n' % arg
)
234 new_system_string
.append(arg
)
235 self
.__options
_okay
= 0
237 # the next item in list is also an option
238 if self
.__system
_string
[counter
][0] == '-':
239 sys
.stderr
.write('option "%s" must take an argument\n' % arg
)
240 new_system_string
.append(arg
)
241 self
.__options
_okay
= 0
242 # the next item in the list is the argument
244 new_system_string
.append(arg
+ '=' + self
.__system
_string
[counter
])
246 # this option does not take an argument
248 new_system_string
.append(arg
)
249 return new_system_string
252 def __get_just_options(self
):
266 Iterate through the self.__system string, looking for the last
267 option. The options are everything in the sysem string before the
270 Check to see that the options contain no arguments.
277 for item
in self
.__system
_string
:
284 just_options
= self
.__system
_string
[:highest
+ 1]
285 arguments
= self
.__system
_string
[highest
+ 1:]
288 arguments
= self
.__system
_string
291 for item
in just_options
:
293 sys
.stderr
.write('%s is an argument in an option list\n' % item
)
294 self
.__options
_okay
= 0
295 return just_options
, arguments
298 def __is_legal_option_func(self
):
311 Check each value in the newly creatd options list to see if it
312 matches what the user describes as a legal option.
317 for arg
in self
.__system
_string
:
319 temp_list
= arg
.split('=')
321 if arg
not in self
.__legal
_options
and arg
[0] == '-':
322 illegal_options
.append(arg
)
325 self
.__options
_okay
= 0
326 sys
.stderr
.write('The following options are not permitted:\n')
327 for not_legal
in illegal_options
:
328 sys
.stderr
.write('%s\n' % not_legal
)
331 def __make_options_dict(self
, options
):
335 option
, arg
= item
.split('=')
345 options_dict
[option
] = arg
349 def parse_options(self
):
350 self
.__system
_string
= self
.__sub
_short
_with
_long
()
351 # # print 'subbed list is %s' % self.__system_string
352 self
.__system
_string
= self
.__pair
_arg
_with
_option
()
353 # # print 'list with pairing is %s' % self.__system_string
354 options
, arguments
= self
.__get
_just
_options
()
355 # # print 'options are %s ' % options
356 # # print 'arguments are %s ' % arguments
357 self
.__is
_legal
_option
_func
()
358 if self
.__options
_okay
:
359 options_dict
= self
.__make
_options
_dict
(options
)
360 # # print options_dict
361 return options_dict
, arguments
369 if __name__
== '__main__':
375 test_obj
= ParseOptions(system_string
= sys
.argv
,
376 options_dict
= this_dict
379 options
, the_args
= test_obj
.parse_options()
380 print options
, the_args
383 this_options = ['--foo', '-o']
384 this_opt_with_args = ['--foo']