blob: c626b3c1de589a1c9e335a736d8b99a4fe1a65db [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 Rossum2f666631996-09-11 21:26:29 +00009\samp{-} and \samp{--}). Long options similar to those supported by
10GNU software may be used as well via an optional third argument.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000011It defines the function
Guido van Rossum2f666631996-09-11 21:26:29 +000012\code{getopt.getopt(args, options [, long_options])}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000013and the exception
14\code{getopt.error}.
15
16The first argument to
17\code{getopt()}
18is the argument list passed to the script with its first element
19chopped off (i.e.,
20\code{sys.argv[1:]}).
21The second argument is the string of option letters that the
22script wants to recognize, with options that require an argument
23followed by a colon (i.e., the same format that \UNIX{}
24\code{getopt()}
25uses).
Guido van Rossum2f666631996-09-11 21:26:29 +000026The third option, if specified, is a list of strings with the names of
27the long options which should be supported. The leading \code{'--'}
28characters should not be included in the option name. Options which
29require an argument should be followed by an equal sign (\code{'='}).
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000030The return value consists of two elements: the first is a list of
31option-and-value pairs; the second is the list of program arguments
32left after the option list was stripped (this is a trailing slice of the
33first argument).
34Each option-and-value pair returned has the option as its first element,
35prefixed with a hyphen (e.g.,
36\code{'-x'}),
37and the option argument as its second element, or an empty string if the
38option has no argument.
39The options occur in the list in the same order in which they were
Guido van Rossum2f666631996-09-11 21:26:29 +000040found, thus allowing multiple occurrences. Long and short options may
41be mixed.
42
43An example using only \UNIX{} style options:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000044
45\bcode\begin{verbatim}
46>>> import getopt, string
47>>> args = string.split('-a -b -cfoo -d bar a1 a2')
48>>> args
49['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2']
50>>> optlist, args = getopt.getopt(args, 'abc:d:')
51>>> optlist
52[('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')]
53>>> args
54['a1', 'a2']
55>>>
56\end{verbatim}\ecode
57
Guido van Rossum2f666631996-09-11 21:26:29 +000058Using long option names is equally easy:
59
60\bcode\begin{verbatim}
61>>> s = '--condition=foo --testing --output-file abc.def -x a1 a2'
62>>> args = string.split(s)
63>>> args
64['--condition=foo', '--testing', '--output-file', 'abc.def', '-x', 'a1', 'a2']
65>>> optlist, args = getopt.getopt(args, 'x', [
66... 'condition=', 'output-file=', 'testing'])
67>>> optlist
68[('--condition', 'foo'), ('--testing', ''), ('--output-file', 'abc.def'), ('-x', '')]
69>>> args
70['a1', 'a2']
71>>>
72\end{verbatim}\ecode
73
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000074The exception
Guido van Rossum2f666631996-09-11 21:26:29 +000075\code{getopt.error = 'getopt.error'}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000076is raised when an unrecognized option is found in the argument list or
77when an option requiring an argument is given none.
78The argument to the exception is a string indicating the cause of the
Guido van Rossum2f666631996-09-11 21:26:29 +000079error. For long options, an argument given to an option which does
80not require one will also cause this exception to be raised.