Skip to content

Commit 6d06094

Browse files
committed
Accept a non-list sequence for the long options (request by Jack Jansen).
Because it might be a common mistake to pass a single string, this situation is treated separately. Since we were making a copy of the longopts list anyway, we now use the list() function -- this made it necessary to change all uses of the local variable (and argument) 'list' to something more meaningful, i.e., 'opts'. Also added docstrings (copied from the library manual) and removed the (now redundant) module comments.
1 parent e9bc62d commit 6d06094

File tree

1 file changed

+54
-39
lines changed

1 file changed

+54
-39
lines changed

Lib/getopt.py

+54-39
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,67 @@
1-
# module getopt -- Standard command line processing.
2-
3-
# Function getopt.getopt() has a different interface but provides the
4-
# similar functionality to the Unix getopt() function, with the
5-
# addition of long-option support. (Long option support added by Lars
6-
# Wirzenius <liw@iki.fi>.)
7-
8-
# It has two required arguments: the first should be argv[1:] (it
9-
# doesn't want the script name), the second the string of option
10-
# letters as passed to Unix getopt() (i.e., a string of allowable
11-
# option letters, with options requiring an argument followed by a
12-
# colon).
13-
14-
# The optional third argument, if present, getopt.getopt works similar
15-
# to the GNU getopt_long function (but optional arguments are not
16-
# supported). The third argument should be a list of strings that
17-
# name the long options. If the name ends '=', the argument requires
18-
# an argument.
19-
20-
# It raises the exception getopt.error with a string argument if it
21-
# detects an error.
22-
23-
# It returns two values:
24-
# (1) a list of pairs (option, option_argument) giving the options in
25-
# the order in which they were specified. (I'd use a dictionary
26-
# but applications may depend on option order or multiple
27-
# occurrences.) Boolean options have '' as option_argument.
28-
# (2) the list of remaining arguments (may be empty).
1+
"""Module getopt -- Parser for command line options.
2+
3+
This module helps scripts to parse the command line arguments in
4+
sys.argv. It supports the same conventions as the Unix getopt()
5+
function (including the special meanings of arguments of the form `-'
6+
and `--'). Long options similar to those supported by GNU software
7+
may be used as well via an optional third argument. This module
8+
provides a single function and an exception:
9+
10+
getopt() -- Parse command line options
11+
error -- Exception (string) raised when bad options are found
12+
"""
13+
14+
# Long option support added by Lars Wirzenius <liw@iki.fi>.
2915

3016
import string
3117

3218
error = 'getopt.error'
3319

3420
def getopt(args, shortopts, longopts = []):
35-
list = []
36-
longopts = longopts[:]
21+
"""getopt(args, options[, long_options]) -> opts, args
22+
23+
Parses command line options and parameter list. args is the
24+
argument list to be parsed, without the leading reference to the
25+
running program. Typically, this means "sys.argv[1:]". shortopts
26+
is the string of option letters that the script wants to
27+
recognize, with options that require an argument followed by a
28+
colon (i.e., the same format that Unix getopt() uses). If
29+
specified, longopts is a list of strings with the names of the
30+
long options which should be supported. The leading '--'
31+
characters should not be included in the option name. Options
32+
which require an argument should be followed by an equal sign
33+
('=').
34+
35+
The return value consists of two elements: the first is a list of
36+
(option, value) pairs; the second is the list of program arguments
37+
left after the option list was stripped (this is a trailing slice
38+
of the first argument). Each option-and-value pair returned has
39+
the option as its first element, prefixed with a hyphen (e.g.,
40+
'-x'), and the option argument as its second element, or an empty
41+
string if the option has no argument. The options occur in the
42+
list in the same order in which they were found, thus allowing
43+
multiple occurrences. Long and short options may be mixed.
44+
45+
"""
46+
47+
opts = []
48+
if type(longopts) == type(""):
49+
longopts = [longopts]
50+
else:
51+
longopts = list(longopts)
3752
longopts.sort()
3853
while args and args[0][:1] == '-' and args[0] != '-':
3954
if args[0] == '--':
4055
args = args[1:]
4156
break
4257
if args[0][:2] == '--':
43-
list, args = do_longs(list, args[0][2:], longopts, args[1:])
58+
opts, args = do_longs(opts, args[0][2:], longopts, args[1:])
4459
else:
45-
list, args = do_shorts(list, args[0][1:], shortopts, args[1:])
60+
opts, args = do_shorts(opts, args[0][1:], shortopts, args[1:])
4661

47-
return list, args
62+
return opts, args
4863

49-
def do_longs(list, opt, longopts, args):
64+
def do_longs(opts, opt, longopts, args):
5065
try:
5166
i = string.index(opt, '=')
5267
opt, optarg = opt[:i], opt[i+1:]
@@ -61,8 +76,8 @@ def do_longs(list, opt, longopts, args):
6176
optarg, args = args[0], args[1:]
6277
elif optarg:
6378
raise error, 'option --%s must not have an argument' % opt
64-
list.append(('--' + opt, optarg or ''))
65-
return list, args
79+
opts.append(('--' + opt, optarg or ''))
80+
return opts, args
6681

6782
# Return:
6883
# has_arg?
@@ -81,7 +96,7 @@ def long_has_args(opt, longopts):
8196
return 0, longopts[i]
8297
raise error, 'option --' + opt + ' not recognized'
8398

84-
def do_shorts(list, optstring, shortopts, args):
99+
def do_shorts(opts, optstring, shortopts, args):
85100
while optstring != '':
86101
opt, optstring = optstring[0], optstring[1:]
87102
if short_has_arg(opt, shortopts):
@@ -92,8 +107,8 @@ def do_shorts(list, optstring, shortopts, args):
92107
optarg, optstring = optstring, ''
93108
else:
94109
optarg = ''
95-
list.append(('-' + opt, optarg))
96-
return list, args
110+
opts.append(('-' + opt, optarg))
111+
return opts, args
97112

98113
def short_has_arg(opt, shortopts):
99114
for i in range(len(shortopts)):

0 commit comments

Comments
 (0)