annotate Lib/getopt.py @ 25838:8540887bcec6 2.2

Backport of 2.183: Fix memory leak in add_subclass() found by NealN with valgrind.
author Guido van Rossum <guido@python.org>
date Fri, 18 Oct 2002 13:42:21 +0000
parents 5ab9a65e2346
children a9dd5e6d6805
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11708
4a23cc49e2b0 Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents: 11615
diff changeset
1 """Parser for command line options.
4280
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
2
9489
db7cca09beaf Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
3 This module helps scripts to parse the command line arguments in
db7cca09beaf Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
4 sys.argv. It supports the same conventions as the Unix getopt()
db7cca09beaf Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
5 function (including the special meanings of arguments of the form `-'
db7cca09beaf Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
6 and `--'). Long options similar to those supported by GNU software
db7cca09beaf Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
7 may be used as well via an optional third argument. This module
db7cca09beaf Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
8 provides a single function and an exception:
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
9
9489
db7cca09beaf Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
10 getopt() -- Parse command line options
11615
bb1f43f25ce0 Contribution from Gerrit Holl:
Guido van Rossum <guido@python.org>
parents: 9489
diff changeset
11 GetoptError -- exception (class) raised with 'opt' attribute, which is the
bb1f43f25ce0 Contribution from Gerrit Holl:
Guido van Rossum <guido@python.org>
parents: 9489
diff changeset
12 option involved with the exception.
9489
db7cca09beaf Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
13 """
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
14
9489
db7cca09beaf Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
15 # Long option support added by Lars Wirzenius <liw@iki.fi>.
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
16
11852
89b52895310c Gerrit Holl's patch to move attribution from the docstring to a
Fred Drake <fdrake@acm.org>
parents: 11708
diff changeset
17 # Gerrit Holl <gerrit@nl.linux.org> moved the string-based exceptions
89b52895310c Gerrit Holl's patch to move attribution from the docstring to a
Fred Drake <fdrake@acm.org>
parents: 11708
diff changeset
18 # to class-based exceptions.
89b52895310c Gerrit Holl's patch to move attribution from the docstring to a
Fred Drake <fdrake@acm.org>
parents: 11708
diff changeset
19
16072
bae889f44cd1 more __all__ updates
Skip Montanaro <skip@pobox.com>
parents: 15763
diff changeset
20 __all__ = ["GetoptError","error","getopt"]
bae889f44cd1 more __all__ updates
Skip Montanaro <skip@pobox.com>
parents: 15763
diff changeset
21
11615
bb1f43f25ce0 Contribution from Gerrit Holl:
Guido van Rossum <guido@python.org>
parents: 9489
diff changeset
22 class GetoptError(Exception):
bb1f43f25ce0 Contribution from Gerrit Holl:
Guido van Rossum <guido@python.org>
parents: 9489
diff changeset
23 opt = ''
bb1f43f25ce0 Contribution from Gerrit Holl:
Guido van Rossum <guido@python.org>
parents: 9489
diff changeset
24 msg = ''
15763
35e08dd37df1 GetoptError is always initialized with exactly two parameters, so simplify
Fred Drake <fdrake@acm.org>
parents: 15674
diff changeset
25 def __init__(self, msg, opt):
35e08dd37df1 GetoptError is always initialized with exactly two parameters, so simplify
Fred Drake <fdrake@acm.org>
parents: 15674
diff changeset
26 self.msg = msg
35e08dd37df1 GetoptError is always initialized with exactly two parameters, so simplify
Fred Drake <fdrake@acm.org>
parents: 15674
diff changeset
27 self.opt = opt
35e08dd37df1 GetoptError is always initialized with exactly two parameters, so simplify
Fred Drake <fdrake@acm.org>
parents: 15674
diff changeset
28 Exception.__init__(self, msg, opt)
11615
bb1f43f25ce0 Contribution from Gerrit Holl:
Guido van Rossum <guido@python.org>
parents: 9489
diff changeset
29
bb1f43f25ce0 Contribution from Gerrit Holl:
Guido van Rossum <guido@python.org>
parents: 9489
diff changeset
30 def __str__(self):
bb1f43f25ce0 Contribution from Gerrit Holl:
Guido van Rossum <guido@python.org>
parents: 9489
diff changeset
31 return self.msg
bb1f43f25ce0 Contribution from Gerrit Holl:
Guido van Rossum <guido@python.org>
parents: 9489
diff changeset
32
bb1f43f25ce0 Contribution from Gerrit Holl:
Guido van Rossum <guido@python.org>
parents: 9489
diff changeset
33 error = GetoptError # backward compatibility
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
34
4251
ac3fbb6771f5 Rewritten by Lars Wizenius to add long options
Guido van Rossum <guido@python.org>
parents: 564
diff changeset
35 def getopt(args, shortopts, longopts = []):
9489
db7cca09beaf Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
36 """getopt(args, options[, long_options]) -> opts, args
db7cca09beaf Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
37
db7cca09beaf Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
38 Parses command line options and parameter list. args is the
db7cca09beaf Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
39 argument list to be parsed, without the leading reference to the
db7cca09beaf Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
40 running program. Typically, this means "sys.argv[1:]". shortopts
db7cca09beaf Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
41 is the string of option letters that the script wants to
db7cca09beaf Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
42 recognize, with options that require an argument followed by a
db7cca09beaf Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
43 colon (i.e., the same format that Unix getopt() uses). If
db7cca09beaf Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
44 specified, longopts is a list of strings with the names of the
db7cca09beaf Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
45 long options which should be supported. The leading '--'
db7cca09beaf Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
46 characters should not be included in the option name. Options
db7cca09beaf Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
47 which require an argument should be followed by an equal sign
db7cca09beaf Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
48 ('=').
db7cca09beaf Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
49
db7cca09beaf Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
50 The return value consists of two elements: the first is a list of
db7cca09beaf Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
51 (option, value) pairs; the second is the list of program arguments
db7cca09beaf Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
52 left after the option list was stripped (this is a trailing slice
db7cca09beaf Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
53 of the first argument). Each option-and-value pair returned has
db7cca09beaf Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
54 the option as its first element, prefixed with a hyphen (e.g.,
db7cca09beaf Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
55 '-x'), and the option argument as its second element, or an empty
db7cca09beaf Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
56 string if the option has no argument. The options occur in the
db7cca09beaf Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
57 list in the same order in which they were found, thus allowing
db7cca09beaf Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
58 multiple occurrences. Long and short options may be mixed.
db7cca09beaf Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
59
db7cca09beaf Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
60 """
db7cca09beaf Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
61
db7cca09beaf Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
62 opts = []
db7cca09beaf Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
63 if type(longopts) == type(""):
db7cca09beaf Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
64 longopts = [longopts]
db7cca09beaf Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
65 else:
db7cca09beaf Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
66 longopts = list(longopts)
15667
3d55bf65699a Fix for SF bug
Tim Peters <tim.peters@gmail.com>
parents: 11853
diff changeset
67 while args and args[0].startswith('-') and args[0] != '-':
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4280
diff changeset
68 if args[0] == '--':
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4280
diff changeset
69 args = args[1:]
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4280
diff changeset
70 break
21365
5ab9a65e2346 Wrapped a long line.
Fred Drake <fdrake@acm.org>
parents: 16072
diff changeset
71 if args[0].startswith('--'):
9489
db7cca09beaf Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
72 opts, args = do_longs(opts, args[0][2:], longopts, args[1:])
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4280
diff changeset
73 else:
9489
db7cca09beaf Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
74 opts, args = do_shorts(opts, 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
75
9489
db7cca09beaf Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
76 return opts, args
4251
ac3fbb6771f5 Rewritten by Lars Wizenius to add long options
Guido van Rossum <guido@python.org>
parents: 564
diff changeset
77
9489
db7cca09beaf Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
78 def do_longs(opts, opt, longopts, args):
4280
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
79 try:
11852
89b52895310c Gerrit Holl's patch to move attribution from the docstring to a
Fred Drake <fdrake@acm.org>
parents: 11708
diff changeset
80 i = opt.index('=')
4280
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
81 except ValueError:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4280
diff changeset
82 optarg = None
15667
3d55bf65699a Fix for SF bug
Tim Peters <tim.peters@gmail.com>
parents: 11853
diff changeset
83 else:
3d55bf65699a Fix for SF bug
Tim Peters <tim.peters@gmail.com>
parents: 11853
diff changeset
84 opt, optarg = opt[:i], opt[i+1:]
4251
ac3fbb6771f5 Rewritten by Lars Wizenius to add long options
Guido van Rossum <guido@python.org>
parents: 564
diff changeset
85
4280
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
86 has_arg, opt = long_has_args(opt, longopts)
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
87 if has_arg:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4280
diff changeset
88 if optarg is None:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4280
diff changeset
89 if not args:
11615
bb1f43f25ce0 Contribution from Gerrit Holl:
Guido van Rossum <guido@python.org>
parents: 9489
diff changeset
90 raise GetoptError('option --%s requires argument' % opt, opt)
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4280
diff changeset
91 optarg, args = args[0], args[1:]
4280
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
92 elif optarg:
11615
bb1f43f25ce0 Contribution from Gerrit Holl:
Guido van Rossum <guido@python.org>
parents: 9489
diff changeset
93 raise GetoptError('option --%s must not have an argument' % opt, opt)
9489
db7cca09beaf Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
94 opts.append(('--' + opt, optarg or ''))
db7cca09beaf Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
95 return opts, args
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
96
4251
ac3fbb6771f5 Rewritten by Lars Wizenius to add long options
Guido van Rossum <guido@python.org>
parents: 564
diff changeset
97 # Return:
ac3fbb6771f5 Rewritten by Lars Wizenius to add long options
Guido van Rossum <guido@python.org>
parents: 564
diff changeset
98 # has_arg?
ac3fbb6771f5 Rewritten by Lars Wizenius to add long options
Guido van Rossum <guido@python.org>
parents: 564
diff changeset
99 # full option name
ac3fbb6771f5 Rewritten by Lars Wizenius to add long options
Guido van Rossum <guido@python.org>
parents: 564
diff changeset
100 def long_has_args(opt, longopts):
15674
75c3a64847c4 getopt used to sort the long option names, in an attempt to simplify
Tim Peters <tim.peters@gmail.com>
parents: 15667
diff changeset
101 possibilities = [o for o in longopts if o.startswith(opt)]
75c3a64847c4 getopt used to sort the long option names, in an attempt to simplify
Tim Peters <tim.peters@gmail.com>
parents: 15667
diff changeset
102 if not possibilities:
15667
3d55bf65699a Fix for SF bug
Tim Peters <tim.peters@gmail.com>
parents: 11853
diff changeset
103 raise GetoptError('option --%s not recognized' % opt, opt)
3d55bf65699a Fix for SF bug
Tim Peters <tim.peters@gmail.com>
parents: 11853
diff changeset
104 # Is there an exact match?
3d55bf65699a Fix for SF bug
Tim Peters <tim.peters@gmail.com>
parents: 11853
diff changeset
105 if opt in possibilities:
3d55bf65699a Fix for SF bug
Tim Peters <tim.peters@gmail.com>
parents: 11853
diff changeset
106 return 0, opt
3d55bf65699a Fix for SF bug
Tim Peters <tim.peters@gmail.com>
parents: 11853
diff changeset
107 elif opt + '=' in possibilities:
3d55bf65699a Fix for SF bug
Tim Peters <tim.peters@gmail.com>
parents: 11853
diff changeset
108 return 1, opt
3d55bf65699a Fix for SF bug
Tim Peters <tim.peters@gmail.com>
parents: 11853
diff changeset
109 # No exact match, so better be unique.
3d55bf65699a Fix for SF bug
Tim Peters <tim.peters@gmail.com>
parents: 11853
diff changeset
110 if len(possibilities) > 1:
3d55bf65699a Fix for SF bug
Tim Peters <tim.peters@gmail.com>
parents: 11853
diff changeset
111 # XXX since possibilities contains all valid continuations, might be
3d55bf65699a Fix for SF bug
Tim Peters <tim.peters@gmail.com>
parents: 11853
diff changeset
112 # nice to work them into the error msg
3d55bf65699a Fix for SF bug
Tim Peters <tim.peters@gmail.com>
parents: 11853
diff changeset
113 raise GetoptError('option --%s not a unique prefix' % opt, opt)
3d55bf65699a Fix for SF bug
Tim Peters <tim.peters@gmail.com>
parents: 11853
diff changeset
114 assert len(possibilities) == 1
3d55bf65699a Fix for SF bug
Tim Peters <tim.peters@gmail.com>
parents: 11853
diff changeset
115 unique_match = possibilities[0]
3d55bf65699a Fix for SF bug
Tim Peters <tim.peters@gmail.com>
parents: 11853
diff changeset
116 has_arg = unique_match.endswith('=')
3d55bf65699a Fix for SF bug
Tim Peters <tim.peters@gmail.com>
parents: 11853
diff changeset
117 if has_arg:
3d55bf65699a Fix for SF bug
Tim Peters <tim.peters@gmail.com>
parents: 11853
diff changeset
118 unique_match = unique_match[:-1]
3d55bf65699a Fix for SF bug
Tim Peters <tim.peters@gmail.com>
parents: 11853
diff changeset
119 return has_arg, unique_match
4251
ac3fbb6771f5 Rewritten by Lars Wizenius to add long options
Guido van Rossum <guido@python.org>
parents: 564
diff changeset
120
9489
db7cca09beaf Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
121 def do_shorts(opts, optstring, shortopts, args):
4280
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
122 while optstring != '':
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4280
diff changeset
123 opt, optstring = optstring[0], optstring[1:]
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4280
diff changeset
124 if short_has_arg(opt, shortopts):
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4280
diff changeset
125 if optstring == '':
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4280
diff changeset
126 if not args:
21365
5ab9a65e2346 Wrapped a long line.
Fred Drake <fdrake@acm.org>
parents: 16072
diff changeset
127 raise GetoptError('option -%s requires argument' % opt,
5ab9a65e2346 Wrapped a long line.
Fred Drake <fdrake@acm.org>
parents: 16072
diff changeset
128 opt)
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4280
diff changeset
129 optstring, args = args[0], args[1:]
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4280
diff changeset
130 optarg, optstring = optstring, ''
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4280
diff changeset
131 else:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4280
diff changeset
132 optarg = ''
9489
db7cca09beaf Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
133 opts.append(('-' + opt, optarg))
db7cca09beaf Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
134 return opts, args
4251
ac3fbb6771f5 Rewritten by Lars Wizenius to add long options
Guido van Rossum <guido@python.org>
parents: 564
diff changeset
135
ac3fbb6771f5 Rewritten by Lars Wizenius to add long options
Guido van Rossum <guido@python.org>
parents: 564
diff changeset
136 def short_has_arg(opt, shortopts):
4280
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
137 for i in range(len(shortopts)):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4280
diff changeset
138 if opt == shortopts[i] != ':':
21365
5ab9a65e2346 Wrapped a long line.
Fred Drake <fdrake@acm.org>
parents: 16072
diff changeset
139 return shortopts.startswith(':', i+1)
11615
bb1f43f25ce0 Contribution from Gerrit Holl:
Guido van Rossum <guido@python.org>
parents: 9489
diff changeset
140 raise GetoptError('option -%s not recognized' % opt, opt)
4251
ac3fbb6771f5 Rewritten by Lars Wizenius to add long options
Guido van Rossum <guido@python.org>
parents: 564
diff changeset
141
ac3fbb6771f5 Rewritten by Lars Wizenius to add long options
Guido van Rossum <guido@python.org>
parents: 564
diff changeset
142 if __name__ == '__main__':
4280
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
143 import sys
56909781a741 Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents: 4251
diff changeset
144 print getopt(sys.argv[1:], "a:b", ["alpha=", "beta"])