blob: 7fe950ffa045783896ac5b4cc9d0ed4b9fa2dd29 [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,
40it \var{long_options} is \code{['foo', 'frob']}, the option
41\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.
68\end{funcdesc}
69
Fred Drake76e6da31999-12-21 22:50:05 +000070\begin{excdesc}{GetoptError}
Fred Drakea44d7401998-03-10 03:36:00 +000071This is raised when an unrecognized option is found in the argument
72list or when an option requiring an argument is given none.
73The argument to the exception is a string indicating the cause of the
74error. For long options, an argument given to an option which does
Fred Drake76e6da31999-12-21 22:50:05 +000075not require one will also cause this exception to be raised. The
76attributes \member{msg} and \member{opt} give the error message and
77related option; if there is no specific option to which the exception
78relates, \member{opt} is an empty string.
Fred Drake293f77a2001-04-18 03:18:57 +000079
80\versionchanged[Introduced \exception{GetoptError} as a synonym for
81 \exception{error}]{1.6}
Fred Drake76e6da31999-12-21 22:50:05 +000082\end{excdesc}
83
84\begin{excdesc}{error}
85Alias for \exception{GetoptError}; for backward compatibility.
Fred Drakea44d7401998-03-10 03:36:00 +000086\end{excdesc}
87
Guido van Rossum2f666631996-09-11 21:26:29 +000088
89An example using only \UNIX{} style options:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000090
Fred Drake19479911998-02-13 06:58:54 +000091\begin{verbatim}
Fred Drakea8e484c2000-08-11 19:55:06 +000092>>> import getopt
93>>> args = '-a -b -cfoo -d bar a1 a2'.split()
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000094>>> args
95['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2']
96>>> optlist, args = getopt.getopt(args, 'abc:d:')
97>>> optlist
98[('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')]
99>>> args
100['a1', 'a2']
Fred Drake19479911998-02-13 06:58:54 +0000101\end{verbatim}
Fred Drakea44d7401998-03-10 03:36:00 +0000102
Guido van Rossum2f666631996-09-11 21:26:29 +0000103Using long option names is equally easy:
104
Fred Drake19479911998-02-13 06:58:54 +0000105\begin{verbatim}
Guido van Rossum2f666631996-09-11 21:26:29 +0000106>>> s = '--condition=foo --testing --output-file abc.def -x a1 a2'
Fred Drakea8e484c2000-08-11 19:55:06 +0000107>>> args = s.split()
Guido van Rossum2f666631996-09-11 21:26:29 +0000108>>> args
109['--condition=foo', '--testing', '--output-file', 'abc.def', '-x', 'a1', 'a2']
110>>> optlist, args = getopt.getopt(args, 'x', [
111... 'condition=', 'output-file=', 'testing'])
112>>> optlist
Fred Drakea44d7401998-03-10 03:36:00 +0000113[('--condition', 'foo'), ('--testing', ''), ('--output-file', 'abc.def'), ('-x',
114 '')]
Guido van Rossum2f666631996-09-11 21:26:29 +0000115>>> args
116['a1', 'a2']
Fred Drakea8e484c2000-08-11 19:55:06 +0000117\end{verbatim}
118
119In a script, typical usage is something like this:
120
121\begin{verbatim}
122import getopt, sys
123
124def main():
125 try:
Raymond Hettinger6e887bb2003-04-29 04:35:36 +0000126 opts, args = getopt.getopt(sys.argv[1:], "ho:v", ["help", "output="])
Fred Drakea8e484c2000-08-11 19:55:06 +0000127 except getopt.GetoptError:
128 # print help information and exit:
129 usage()
130 sys.exit(2)
131 output = None
Raymond Hettinger6e887bb2003-04-29 04:35:36 +0000132 verbose = False
Fred Drakea8e484c2000-08-11 19:55:06 +0000133 for o, a in opts:
Raymond Hettinger6e887bb2003-04-29 04:35:36 +0000134 if o == "-v":
135 verbose = True
Fred Drakea8e484c2000-08-11 19:55:06 +0000136 if o in ("-h", "--help"):
137 usage()
138 sys.exit()
139 if o in ("-o", "--output"):
140 output = a
141 # ...
142
143if __name__ == "__main__":
144 main()
Fred Drake19479911998-02-13 06:58:54 +0000145\end{verbatim}
Skip Montanarodb8d1c22004-01-26 19:30:21 +0000146
147\begin{seealso}
Fred Drakeee3c6072004-01-26 19:40:18 +0000148 \seemodule{optparse}{More object-oriented command line option parsing.}
Skip Montanarodb8d1c22004-01-26 19:30:21 +0000149\end{seealso}