annotate Lib/getopt.py @ 4566:6403c94de50d legacy-trunk

Added safeguard against failure in __del__.
author Guido van Rossum <guido@python.org>
date Wed, 27 Nov 1996 19:39:58 +0000
parents 56909781a741
children fb4b4d069cc7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 # module getopt -- Standard command line processing.
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 # Function getopt.getopt() has a different interface but provides the
4280
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
4 # similar functionality to the Unix getopt() function, with the
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
5 # addition of long-option support. (Long option support added by Lars
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
6 # Wirzenius <liw@iki.fi>.)
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
7
4280
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
8 # It has two required arguments: the first should be argv[1:] (it
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
9 # doesn't want the script name), the second the string of option
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
10 # letters as passed to Unix getopt() (i.e., a string of allowable
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
11 # option letters, with options requiring an argument followed by a
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
12 # colon).
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
13
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
14 # The optional third argument, if present, getopt.getopt works similar
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
15 # to the GNU getopt_long function (but optional arguments are not
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
16 # supported). The third argument should be a list of strings that
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
17 # name the long options. If the name ends '=', the argument requires
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
18 # an argument.
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
19
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 # It raises the exception getopt.error with a string argument if it
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 # detects an error.
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
22
4280
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
23 # It returns two values:
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 # (1) a list of pairs (option, option_argument) giving the options in
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 # the order in which they were specified. (I'd use a dictionary
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 # but applications may depend on option order or multiple
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 # occurrences.) Boolean options have '' as option_argument.
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 # (2) the list of remaining arguments (may be empty).
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
29
4251
ac3fbb6771f5 Rewritten by Lars Wizenius to add long options
Guido van Rossum <guido@python.org>
parents: 564
diff changeset
30 import string
ac3fbb6771f5 Rewritten by Lars Wizenius to add long options
Guido van Rossum <guido@python.org>
parents: 564
diff changeset
31
4280
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
32 error = 'getopt.error'
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
33
4251
ac3fbb6771f5 Rewritten by Lars Wizenius to add long options
Guido van Rossum <guido@python.org>
parents: 564
diff changeset
34 def getopt(args, shortopts, longopts = []):
4280
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
35 list = []
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
36 longopts = longopts[:]
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
37 longopts.sort()
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
38 while args and args[0][:1] == '-' and args[0] != '-':
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
39 if args[0] == '--':
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
40 args = args[1:]
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
41 break
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
42 if args[0][:2] == '--':
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
43 list, args = do_longs(list, args[0][2:], longopts, args[1:])
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
44 else:
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
45 list, args = do_shorts(list, args[0][1:], shortopts, args[1:])
4251
ac3fbb6771f5 Rewritten by Lars Wizenius to add long options
Guido van Rossum <guido@python.org>
parents: 564
diff changeset
46
4280
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
47 return list, args
4251
ac3fbb6771f5 Rewritten by Lars Wizenius to add long options
Guido van Rossum <guido@python.org>
parents: 564
diff changeset
48
ac3fbb6771f5 Rewritten by Lars Wizenius to add long options
Guido van Rossum <guido@python.org>
parents: 564
diff changeset
49 def do_longs(list, opt, longopts, args):
4280
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
50 try:
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
51 i = string.index(opt, '=')
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
52 opt, optarg = opt[:i], opt[i+1:]
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
53 except ValueError:
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
54 optarg = None
4251
ac3fbb6771f5 Rewritten by Lars Wizenius to add long options
Guido van Rossum <guido@python.org>
parents: 564
diff changeset
55
4280
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
56 has_arg, opt = long_has_args(opt, longopts)
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
57 if has_arg:
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
58 if optarg is None:
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
59 if not args:
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
60 raise error, 'option --%s requires argument' % opt
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
61 optarg, args = args[0], args[1:]
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
62 elif optarg:
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
63 raise error, 'option --%s must not have an argument' % opt
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
64 list.append(('--' + opt, optarg or ''))
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
65 return list, args
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
66
4251
ac3fbb6771f5 Rewritten by Lars Wizenius to add long options
Guido van Rossum <guido@python.org>
parents: 564
diff changeset
67 # Return:
ac3fbb6771f5 Rewritten by Lars Wizenius to add long options
Guido van Rossum <guido@python.org>
parents: 564
diff changeset
68 # has_arg?
ac3fbb6771f5 Rewritten by Lars Wizenius to add long options
Guido van Rossum <guido@python.org>
parents: 564
diff changeset
69 # full option name
ac3fbb6771f5 Rewritten by Lars Wizenius to add long options
Guido van Rossum <guido@python.org>
parents: 564
diff changeset
70 def long_has_args(opt, longopts):
4280
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
71 optlen = len(opt)
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
72 for i in range(len(longopts)):
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
73 x, y = longopts[i][:optlen], longopts[i][optlen:]
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
74 if opt != x:
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
75 continue
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
76 if y != '' and y != '=' and i+1 < len(longopts):
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
77 if opt == longopts[i+1][:optlen]:
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
78 raise error, 'option --%s not a unique prefix' % opt
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
79 if longopts[i][-1:] in ('=', ):
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
80 return 1, longopts[i][:-1]
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
81 return 0, longopts[i]
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
82 raise error, 'option --' + opt + ' not recognized'
4251
ac3fbb6771f5 Rewritten by Lars Wizenius to add long options
Guido van Rossum <guido@python.org>
parents: 564
diff changeset
83
ac3fbb6771f5 Rewritten by Lars Wizenius to add long options
Guido van Rossum <guido@python.org>
parents: 564
diff changeset
84 def do_shorts(list, optstring, shortopts, args):
4280
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
85 while optstring != '':
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
86 opt, optstring = optstring[0], optstring[1:]
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
87 if short_has_arg(opt, shortopts):
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
88 if optstring == '':
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
89 if not args:
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
90 raise error, 'option -%s requires argument' % opt
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
91 optstring, args = args[0], args[1:]
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
92 optarg, optstring = optstring, ''
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
93 else:
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
94 optarg = ''
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
95 list.append(('-' + opt, optarg))
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
96 return list, args
4251
ac3fbb6771f5 Rewritten by Lars Wizenius to add long options
Guido van Rossum <guido@python.org>
parents: 564
diff changeset
97
ac3fbb6771f5 Rewritten by Lars Wizenius to add long options
Guido van Rossum <guido@python.org>
parents: 564
diff changeset
98 def short_has_arg(opt, shortopts):
4280
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
99 for i in range(len(shortopts)):
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
100 if opt == shortopts[i] != ':':
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
101 return shortopts[i+1:i+2] == ':'
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
102 raise error, 'option -%s not recognized' % opt
4251
ac3fbb6771f5 Rewritten by Lars Wizenius to add long options
Guido van Rossum <guido@python.org>
parents: 564
diff changeset
103
ac3fbb6771f5 Rewritten by Lars Wizenius to add long options
Guido van Rossum <guido@python.org>
parents: 564
diff changeset
104 if __name__ == '__main__':
4280
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
105 import sys
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
106 print getopt(sys.argv[1:], "a:b", ["alpha=", "beta"])