1 # WARNING TO PAUL: DON'T EDIT THIS FILE use update.py instead.
9 system_string --The string from the command line
11 options_dict -- a dictionary with the key equal to the opition, and
12 a list describing that option. (See below)
17 A tupple. The first item in the tupple is a dictionary containing
18 the arguments for each options. The second is a list of the
21 If invalid options are passed to the module, 0,0 is returned.
25 Your script has the option '--indents', and '--output=file'.
27 You want to give short option names as well:
33 options_dict = {'output': [1, 'o'],
37 options_obj = ParseOptions(
38 system_string = sys.argv,
39 options_dict = options_dict
42 options, arguments = options_obj.parse_options()
49 {indents:None, output:'/home/paul/file'}, ['/home/paul/input']
53 def __init__(self
, system_string
, options_dict
):
54 self
.__system
_string
= system_string
[1:]
55 long_list
= self
.__make
_long
_list
_func
(options_dict
)
57 short_list
= self
.__make
_short
_list
_func
(options_dict
)
59 self
.__legal
_options
= long_list
+ short_list
60 # # print self.__legal_options
61 self
.__short
_long
_dict
= self
.__make
_short
_long
_dict
_func
(options_dict
)
62 # # print self.__short_long_dict
63 self
.__opt
_with
_args
= self
.__make
_options
_with
_arg
_list
(options_dict
)
64 # # print self.__opt_with_args
65 self
.__options
_okay
= 1
69 def __make_long_list_func(self
, options_dict
):
73 options_dict -- the dictionary mapping options to a list
77 a list of legal options
84 keys
= options_dict
.keys()
87 legal_list
.append(key
)
90 def __make_short_list_func(self
, options_dict
):
94 options_dict --the dictionary mapping options to a list
98 a list of legal short options
102 keys
= options_dict
.keys()
104 values
= options_dict
[key
]
106 legal_list
.append('-' + values
[1])
114 def __make_short_long_dict_func(self
, options_dict
):
118 options_dict --the dictionary mapping options to a list
122 a dictionary with keys of short options and values of long options
126 keys
= options_dict
.keys()
128 values
= options_dict
[key
]
130 short
= '-' + values
[1]
132 short_long_dict
[short
] = long
136 return short_long_dict
138 def __make_options_with_arg_list(self
, options_dict
):
142 options_dict --the dictionary mapping options to a list
146 a list of options that take arguments.
150 keys
= options_dict
.keys()
152 values
= options_dict
[key
]
155 opt_with_arg
.append('--' + key
)
162 def __sub_short_with_long(self
):
174 iterate through the system string and replace short options with long options
179 sub_list
= self
.__short
_long
_dict
.keys()
180 for item
in self
.__system
_string
:
182 item
= self
.__short
_long
_dict
[item
]
183 new_string
.append(item
)
188 def __pair_arg_with_option(self
):
198 nothing (changes value of self.__system_string)
202 iterate through the system string, and match arguments with options:
204 old_list = ['--foo', 'bar']
206 new_list = ['--foo=bar'
209 opt_len
= len(self
.__system
_string
)
210 new_system_string
= []
213 for arg
in self
.__system
_string
:
215 # previous value was an option with an argument, so this arg is
216 # actually an argument that has already been added
222 # not an option--an argument
224 new_system_string
.append(arg
)
226 # option and argument already paired
228 new_system_string
.append(arg
)
230 # this option takes an argument
231 if arg
in self
.__opt
_with
_args
:
232 # option is the last in the list
233 if counter
+ 1 > opt_len
:
234 sys
.stderr
.write('option "%s" must take an argument\n' % arg
)
235 new_system_string
.append(arg
)
236 self
.__options
_okay
= 0
238 # the next item in list is also an option
239 if self
.__system
_string
[counter
][0] == '-':
240 sys
.stderr
.write('option "%s" must take an argument\n' % arg
)
241 new_system_string
.append(arg
)
242 self
.__options
_okay
= 0
243 # the next item in the list is the argument
245 new_system_string
.append(arg
+ '=' + self
.__system
_string
[counter
])
247 # this option does not take an argument
249 new_system_string
.append(arg
)
250 return new_system_string
253 def __get_just_options(self
):
267 Iterate through the self.__system string, looking for the last
268 option. The options are everything in the sysem string before the
271 Check to see that the options contain no arguments.
278 for item
in self
.__system
_string
:
285 just_options
= self
.__system
_string
[:highest
+ 1]
286 arguments
= self
.__system
_string
[highest
+ 1:]
289 arguments
= self
.__system
_string
292 for item
in just_options
:
294 sys
.stderr
.write('%s is an argument in an option list\n' % item
)
295 self
.__options
_okay
= 0
296 return just_options
, arguments
299 def __is_legal_option_func(self
):
312 Check each value in the newly creatd options list to see if it
313 matches what the user describes as a legal option.
318 for arg
in self
.__system
_string
:
320 temp_list
= arg
.split('=')
322 if arg
not in self
.__legal
_options
and arg
[0] == '-':
323 illegal_options
.append(arg
)
326 self
.__options
_okay
= 0
327 sys
.stderr
.write('The following options are not permitted:\n')
328 for not_legal
in illegal_options
:
329 sys
.stderr
.write('%s\n' % not_legal
)
332 def __make_options_dict(self
, options
):
336 option
, arg
= item
.split('=')
346 options_dict
[option
] = arg
350 def parse_options(self
):
351 self
.__system
_string
= self
.__sub
_short
_with
_long
()
352 # # print 'subbed list is %s' % self.__system_string
353 self
.__system
_string
= self
.__pair
_arg
_with
_option
()
354 # # print 'list with pairing is %s' % self.__system_string
355 options
, arguments
= self
.__get
_just
_options
()
356 # # print 'options are %s ' % options
357 # # print 'arguments are %s ' % arguments
358 self
.__is
_legal
_option
_func
()
359 if self
.__options
_okay
:
360 options_dict
= self
.__make
_options
_dict
(options
)
361 # # print options_dict
362 return options_dict
, arguments
370 if __name__
== '__main__':
376 test_obj
= ParseOptions(system_string
= sys
.argv
,
377 options_dict
= this_dict
380 options
, the_args
= test_obj
.parse_options()
381 print options
, the_args
384 this_options = ['--foo', '-o']
385 this_opt_with_args = ['--foo']