blob: e8b16a31e20fd3b37772fd7a4ba77bdc3157fb0e [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{getopt} ---
Fred Drakef8ca7d82000-10-10 17:03:45 +00002 Parser for command line options}
Fred Drakeb91e9341998-07-23 17:59:49 +00003
Fred Drakef8ca7d82000-10-10 17:03:45 +00004\declaremodule{standard}{getopt}
5\modulesynopsis{Portable parser for command line options; support both
6 short and long option names.}
Fred Drakeb91e9341998-07-23 17:59:49 +00007
Fred Drakea44d7401998-03-10 03:36:00 +00008
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00009This module helps scripts to parse the command line arguments in
10\code{sys.argv}.
Fred Drakea44d7401998-03-10 03:36:00 +000011It supports the same conventions as the \UNIX{} \cfunction{getopt()}
Guido van Rossum470be141995-03-17 16:07:09 +000012function (including the special meanings of arguments of the form
Guido van Rossume8d94a81997-04-02 06:05:07 +000013`\code{-}' and `\code{-}\code{-}').
14% That's to fool latex2html into leaving the two hyphens alone!
15Long options similar to those supported by
Guido van Rossum2f666631996-09-11 21:26:29 +000016GNU software may be used as well via an optional third argument.
Fred Drakea44d7401998-03-10 03:36:00 +000017This module provides a single function and an exception:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000018
Fred Drakea44d7401998-03-10 03:36:00 +000019\begin{funcdesc}{getopt}{args, options\optional{, long_options}}
20Parses command line options and parameter list. \var{args} is the
21argument list to be parsed, without the leading reference to the
22running program. Typically, this means \samp{sys.argv[1:]}.
23\var{options} is the string of option letters that the script wants to
24recognize, with options that require an argument followed by a colon
Fred Drakea8e484c2000-08-11 19:55:06 +000025(\character{:}; i.e., the same format that \UNIX{}
26\cfunction{getopt()} uses).
27
Fred Drake0aa811c2001-10-20 04:24:09 +000028\note{Unlike GNU \cfunction{getopt()}, after a non-option
Moshe Zadka2bd0d882001-04-11 07:33:08 +000029argument, all further arguments are considered also non-options.
Fred Drake0aa811c2001-10-20 04:24:09 +000030This is similar to the way non-GNU \UNIX{} systems work.}
Moshe Zadka2bd0d882001-04-11 07:33:08 +000031
Fred Drakea8e484c2000-08-11 19:55:06 +000032\var{long_options}, if specified, must be a list of strings with the
33names of the long options which should be supported. The leading
Fred Drakea44d7401998-03-10 03:36:00 +000034\code{'-}\code{-'} characters should not be included in the option
Fred Drakea8e484c2000-08-11 19:55:06 +000035name. Long options which require an argument should be followed by an
Fred Drake45b1d6a2001-01-08 16:05:51 +000036equal sign (\character{=}). To accept only long options,
37\var{options} should be an empty string. Long options on the command
38line can be recognized so long as they provide a prefix of the option
39name that matches exactly one of the accepted options. For example,
Raymond Hettinger565ea5a2004-10-02 11:02:59 +000040if \var{long_options} is \code{['foo', 'frob']}, the option
Fred Drake45b1d6a2001-01-08 16:05:51 +000041\longprogramopt{fo} will match as \longprogramopt{foo}, but
42\longprogramopt{f} will not match uniquely, so \exception{GetoptError}
43will be raised.
Fred Drakea44d7401998-03-10 03:36:00 +000044
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000045The return value consists of two elements: the first is a list of
Fred Drakea44d7401998-03-10 03:36:00 +000046\code{(\var{option}, \var{value})} pairs; the second is the list of
47program arguments left after the option list was stripped (this is a
Fred Drakea8e484c2000-08-11 19:55:06 +000048trailing slice of \var{args}). Each option-and-value pair returned
49has the option as its first element, prefixed with a hyphen for short
50options (e.g., \code{'-x'}) or two hyphens for long options (e.g.,
51\code{'-}\code{-long-option'}), and the option argument as its second
52element, or an empty string if the option has no argument. The
53options occur in the list in the same order in which they were found,
54thus allowing multiple occurrences. Long and short options may be
55mixed.
Fred Drakea44d7401998-03-10 03:36:00 +000056\end{funcdesc}
57
Martin v. Löwis446a25f2002-06-06 10:58:36 +000058\begin{funcdesc}{gnu_getopt}{args, options\optional{, long_options}}
59This function works like \function{getopt()}, except that GNU style
60scanning mode is used by default. This means that option and
61non-option arguments may be intermixed. The \function{getopt()}
62function stops processing options as soon as a non-option argument is
63encountered.
64
65If the first character of the option string is `+', or if the
66environment variable POSIXLY_CORRECT is set, then option processing
67stops as soon as a non-option argument is encountered.
Fred Drake40558fe2006-01-20 03:30:36 +000068
69\versionadded{2.3}
Martin v. Löwis446a25f2002-06-06 10:58:36 +000070\end{funcdesc}
71
Fred Drake76e6da31999-12-21 22:50:05 +000072\begin{excdesc}{GetoptError}
Fred Drakea44d7401998-03-10 03:36:00 +000073This is raised when an unrecognized option is found in the argument
74list or when an option requiring an argument is given none.
75The argument to the exception is a string indicating the cause of the
76error. For long options, an argument given to an option which does
Fred Drake76e6da31999-12-21 22:50:05 +000077not require one will also cause this exception to be raised. The
78attributes \member{msg} and \member{opt} give the error message and
79related option; if there is no specific option to which the exception
80relates, \member{opt} is an empty string.
Fred Drake293f77a2001-04-18 03:18:57 +000081
82\versionchanged[Introduced \exception{GetoptError} as a synonym for
83 \exception{error}]{1.6}
Fred Drake76e6da31999-12-21 22:50:05 +000084\end{excdesc}
85
86\begin{excdesc}{error}
87Alias for \exception{GetoptError}; for backward compatibility.
Fred Drakea44d7401998-03-10 03:36:00 +000088\end{excdesc}
89
Guido van Rossum2f666631996-09-11 21:26:29 +000090
91An example using only \UNIX{} style options:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000092
Fred Drake19479911998-02-13 06:58:54 +000093\begin{verbatim}
Fred Drakea8e484c2000-08-11 19:55:06 +000094>>> import getopt
95>>> args = '-a -b -cfoo -d bar a1 a2'.split()
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000096>>> args
97['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2']
98>>> optlist, args = getopt.getopt(args, 'abc:d:')
99>>> optlist
100[('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')]
101>>> args
102['a1', 'a2']
Fred Drake19479911998-02-13 06:58:54 +0000103\end{verbatim}
Fred Drakea44d7401998-03-10 03:36:00 +0000104
Guido van Rossum2f666631996-09-11 21:26:29 +0000105Using long option names is equally easy:
106
Fred Drake19479911998-02-13 06:58:54 +0000107\begin{verbatim}
Guido van Rossum2f666631996-09-11 21:26:29 +0000108>>> s = '--condition=foo --testing --output-file abc.def -x a1 a2'
Fred Drakea8e484c2000-08-11 19:55:06 +0000109>>> args = s.split()
Guido van Rossum2f666631996-09-11 21:26:29 +0000110>>> args
111['--condition=foo', '--testing', '--output-file', 'abc.def', '-x', 'a1', 'a2']
112>>> optlist, args = getopt.getopt(args, 'x', [
113... 'condition=', 'output-file=', 'testing'])
114>>> optlist
Fred Drakea44d7401998-03-10 03:36:00 +0000115[('--condition', 'foo'), ('--testing', ''), ('--output-file', 'abc.def'), ('-x',
116 '')]
Guido van Rossum2f666631996-09-11 21:26:29 +0000117>>> args
118['a1', 'a2']
Fred Drakea8e484c2000-08-11 19:55:06 +0000119\end{verbatim}
120
121In a script, typical usage is something like this:
122
123\begin{verbatim}
124import getopt, sys
125
126def main():
127 try:
Raymond Hettinger6e887bb2003-04-29 04:35:36 +0000128 opts, args = getopt.getopt(sys.argv[1:], "ho:v", ["help", "output="])
Fred Drakea8e484c2000-08-11 19:55:06 +0000129 except getopt.GetoptError:
130 # print help information and exit:
131 usage()
132 sys.exit(2)
133 output = None
Raymond Hettinger6e887bb2003-04-29 04:35:36 +0000134 verbose = False
Fred Drakea8e484c2000-08-11 19:55:06 +0000135 for o, a in opts:
Raymond Hettinger6e887bb2003-04-29 04:35:36 +0000136 if o == "-v":
137 verbose = True
Fred Drakea8e484c2000-08-11 19:55:06 +0000138 if o in ("-h", "--help"):
139 usage()
140 sys.exit()
141 if o in ("-o", "--output"):
142 output = a
143 # ...
144
145if __name__ == "__main__":
146 main()
Fred Drake19479911998-02-13 06:58:54 +0000147\end{verbatim}
Skip Montanarodb8d1c22004-01-26 19:30:21 +0000148
149\begin{seealso}
Fred Drakeee3c6072004-01-26 19:40:18 +0000150 \seemodule{optparse}{More object-oriented command line option parsing.}
Skip Montanarodb8d1c22004-01-26 19:30:21 +0000151\end{seealso}