blob: 7ab46e4052b783f0bf2c93bd9ed9a61433d41f48 [file] [log] [blame]
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00001\section{Standard Module \sectcode{getopt}}
2
3\stmodindex{getopt}
4This module helps scripts to parse the command line arguments in
5\code{sys.argv}.
Guido van Rossum2f666631996-09-11 21:26:29 +00006It supports the same conventions as the \UNIX{}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00007\code{getopt()}
Guido van Rossum470be141995-03-17 16:07:09 +00008function (including the special meanings of arguments of the form
Guido van Rossume8d94a81997-04-02 06:05:07 +00009`\code{-}' and `\code{-}\code{-}').
10% That's to fool latex2html into leaving the two hyphens alone!
11Long options similar to those supported by
Guido van Rossum2f666631996-09-11 21:26:29 +000012GNU software may be used as well via an optional third argument.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000013It defines the function
Guido van Rossum2f666631996-09-11 21:26:29 +000014\code{getopt.getopt(args, options [, long_options])}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000015and the exception
16\code{getopt.error}.
17
18The first argument to
19\code{getopt()}
20is the argument list passed to the script with its first element
21chopped off (i.e.,
22\code{sys.argv[1:]}).
23The second argument is the string of option letters that the
24script wants to recognize, with options that require an argument
25followed by a colon (i.e., the same format that \UNIX{}
26\code{getopt()}
27uses).
Guido van Rossum2f666631996-09-11 21:26:29 +000028The third option, if specified, is a list of strings with the names of
Guido van Rossume8d94a81997-04-02 06:05:07 +000029the long options which should be supported. The leading \code{'-}\code{-'}
Guido van Rossum2f666631996-09-11 21:26:29 +000030characters should not be included in the option name. Options which
31require an argument should be followed by an equal sign (\code{'='}).
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000032The return value consists of two elements: the first is a list of
33option-and-value pairs; the second is the list of program arguments
34left after the option list was stripped (this is a trailing slice of the
35first argument).
36Each option-and-value pair returned has the option as its first element,
37prefixed with a hyphen (e.g.,
38\code{'-x'}),
39and the option argument as its second element, or an empty string if the
40option has no argument.
41The options occur in the list in the same order in which they were
Guido van Rossum2f666631996-09-11 21:26:29 +000042found, thus allowing multiple occurrences. Long and short options may
43be mixed.
44
45An example using only \UNIX{} style options:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000046
47\bcode\begin{verbatim}
48>>> import getopt, string
49>>> args = string.split('-a -b -cfoo -d bar a1 a2')
50>>> args
51['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2']
52>>> optlist, args = getopt.getopt(args, 'abc:d:')
53>>> optlist
54[('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')]
55>>> args
56['a1', 'a2']
57>>>
58\end{verbatim}\ecode
59
Guido van Rossum2f666631996-09-11 21:26:29 +000060Using long option names is equally easy:
61
62\bcode\begin{verbatim}
63>>> s = '--condition=foo --testing --output-file abc.def -x a1 a2'
64>>> args = string.split(s)
65>>> args
66['--condition=foo', '--testing', '--output-file', 'abc.def', '-x', 'a1', 'a2']
67>>> optlist, args = getopt.getopt(args, 'x', [
68... 'condition=', 'output-file=', 'testing'])
69>>> optlist
70[('--condition', 'foo'), ('--testing', ''), ('--output-file', 'abc.def'), ('-x', '')]
71>>> args
72['a1', 'a2']
73>>>
74\end{verbatim}\ecode
75
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000076The exception
Guido van Rossum2f666631996-09-11 21:26:29 +000077\code{getopt.error = 'getopt.error'}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000078is raised when an unrecognized option is found in the argument list or
79when an option requiring an argument is given none.
80The argument to the exception is a string indicating the cause of the
Guido van Rossum2f666631996-09-11 21:26:29 +000081error. For long options, an argument given to an option which does
82not require one will also cause this exception to be raised.