removed obsolete issues (many of them fixed with AE)
[docutils.git] / sandbox / paultremblay / docutils_nest / docutils_nest / options_trem.py
blob6cf60a3dbd6ee947e6588363bf738430751af67c
1 import sys
2 class ParseOptions:
4 """
6 Requires:
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)
14 Returns:
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
18 arguments.
20 If invalid options are passed to the module, 0,0 is returned.
22 Examples:
24 Your script has the option '--indents', and '--output=file'.
26 You want to give short option names as well:
28 --i and -o=file
30 Use this:
32 options_dict = {'output': [1, 'o'],
33 'indents': [0, 'i']
36 options_obj = ParseOptions(
37 system_string = sys.argv,
38 options_dict = options_dict
41 options, arguments = options_obj.parse_options()
42 print options
43 print arguments
45 The result will be:
48 {indents:None, output:'/home/paul/file'}, ['/home/paul/input']
51 """
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)
55 # # print long_list
56 short_list = self.__make_short_list_func(options_dict)
57 # # print short_list
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):
69 """
70 Required:
72 options_dict -- the dictionary mapping options to a list
74 Returns:
76 a list of legal options
81 """
82 legal_list = []
83 keys = options_dict.keys()
84 for key in keys:
85 key = '--' + key
86 legal_list.append(key)
87 return legal_list
89 def __make_short_list_func(self, options_dict):
90 """
91 Required:
93 options_dict --the dictionary mapping options to a list
95 Returns:
97 a list of legal short options
99 """
100 legal_list = []
101 keys = options_dict.keys()
102 for key in keys:
103 values = options_dict[key]
104 try:
105 legal_list.append('-' + values[1])
106 except IndexError:
107 pass
109 return legal_list
113 def __make_short_long_dict_func(self, options_dict):
115 Required:
117 options_dict --the dictionary mapping options to a list
119 Returns:
121 a dictionary with keys of short options and values of long options
124 short_long_dict = {}
125 keys = options_dict.keys()
126 for key in keys:
127 values = options_dict[key]
128 try:
129 short = '-' + values[1]
130 long = '--' + key
131 short_long_dict[short] = long
132 except IndexError:
133 pass
135 return short_long_dict
137 def __make_options_with_arg_list(self, options_dict):
139 Required:
141 options_dict --the dictionary mapping options to a list
143 Returns:
145 a list of options that take arguments.
148 opt_with_arg = []
149 keys = options_dict.keys()
150 for key in keys:
151 values = options_dict[key]
152 try:
153 if values[0]:
154 opt_with_arg.append('--' + key)
155 except IndexError:
156 pass
158 return opt_with_arg
161 def __sub_short_with_long(self):
163 Required:
165 nothing
167 Returns:
169 a new system string
171 Logic:
173 iterate through the system string and replace short options with long options
177 new_string = []
178 sub_list = self.__short_long_dict.keys()
179 for item in self.__system_string:
180 if item in sub_list:
181 item = self.__short_long_dict[item]
182 new_string.append(item)
183 return new_string
187 def __pair_arg_with_option(self):
191 Required:
193 nothing
195 Returns
197 nothing (changes value of self.__system_string)
199 Logic:
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 = []
210 counter = 0
211 slurp_value = 0
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
216 counter += 1
217 if slurp_value:
218 slurp_value = 0
219 continue
221 # not an option--an argument
222 if arg[0] != '-':
223 new_system_string.append(arg)
225 # option and argument already paired
226 elif '=' in arg:
227 new_system_string .append(arg)
228 else:
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
236 else:
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
243 else:
244 new_system_string.append(arg + '=' + self.__system_string[counter])
245 slurp_value = 1
246 # this option does not take an argument
247 else:
248 new_system_string.append(arg)
249 return new_system_string
252 def __get_just_options(self):
256 Requires:
258 nothing
260 Returns:
262 list of options
264 Logic:
266 Iterate through the self.__system string, looking for the last
267 option. The options are everything in the sysem string before the
268 last option.
270 Check to see that the options contain no arguments.
274 highest = 0
275 counter = 0
276 found_options = 0
277 for item in self.__system_string:
278 if item[0] == '-':
279 highest = counter
280 found_options = 1
281 counter += 1
283 if found_options:
284 just_options = self.__system_string[:highest + 1]
285 arguments = self.__system_string[highest + 1:]
286 else:
287 just_options = []
288 arguments = self.__system_string
290 if found_options:
291 for item in just_options:
292 if item[0] != '-':
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):
301 Requires:
303 nothing
305 Returns:
307 nothing
309 Logic:
311 Check each value in the newly creatd options list to see if it
312 matches what the user describes as a legal option.
316 illegal_options = []
317 for arg in self.__system_string:
318 if '=' in arg:
319 temp_list = arg.split('=')
320 arg = temp_list[0]
321 if arg not in self.__legal_options and arg[0] == '-':
322 illegal_options.append(arg)
324 if illegal_options:
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):
332 options_dict = {}
333 for item in options:
334 if '=' in item:
335 option, arg = item.split('=')
336 else:
337 option = item
338 arg = None
340 if option[0] == '-':
341 option = option[1:]
342 if option[0] == '-':
343 option = option[1:]
345 options_dict[option] = arg
347 return options_dict
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
363 else:
364 return 0,0
369 if __name__ == '__main__':
370 this_dict = {
371 'indents': [0, 'i'],
372 'output': [1, 'o'],
373 'test3': [1, 't'],
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']