blob: c96c3cfff4a0ef1cf1985575ae44a472eebe5c4a [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
Moshe Zadka2bd0d882001-04-11 07:33:08 +000028\strong{Note:} Unlike GNU \cfunction{getopt()}, after a non-option
29argument, all further arguments are considered also non-options.
30This is similar to the way non-GNU \UNIX{} systems work.
31
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
Fred Drake76e6da31999-12-21 22:50:05 +000058\begin{excdesc}{GetoptError}
Fred Drakea44d7401998-03-10 03:36:00 +000059This is raised when an unrecognized option is found in the argument
60list or when an option requiring an argument is given none.
61The argument to the exception is a string indicating the cause of the
62error. For long options, an argument given to an option which does
Fred Drake76e6da31999-12-21 22:50:05 +000063not require one will also cause this exception to be raised. The
64attributes \member{msg} and \member{opt} give the error message and
65related option; if there is no specific option to which the exception
66relates, \member{opt} is an empty string.
67\end{excdesc}
68
69\begin{excdesc}{error}
70Alias for \exception{GetoptError}; for backward compatibility.
Fred Drakea44d7401998-03-10 03:36:00 +000071\end{excdesc}
72
Guido van Rossum2f666631996-09-11 21:26:29 +000073
74An example using only \UNIX{} style options:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000075
Fred Drake19479911998-02-13 06:58:54 +000076\begin{verbatim}
Fred Drakea8e484c2000-08-11 19:55:06 +000077>>> import getopt
78>>> args = '-a -b -cfoo -d bar a1 a2'.split()
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000079>>> args
80['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2']
81>>> optlist, args = getopt.getopt(args, 'abc:d:')
82>>> optlist
83[('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')]
84>>> args
85['a1', 'a2']
Fred Drake19479911998-02-13 06:58:54 +000086\end{verbatim}
Fred Drakea44d7401998-03-10 03:36:00 +000087
Guido van Rossum2f666631996-09-11 21:26:29 +000088Using long option names is equally easy:
89
Fred Drake19479911998-02-13 06:58:54 +000090\begin{verbatim}
Guido van Rossum2f666631996-09-11 21:26:29 +000091>>> s = '--condition=foo --testing --output-file abc.def -x a1 a2'
Fred Drakea8e484c2000-08-11 19:55:06 +000092>>> args = s.split()
Guido van Rossum2f666631996-09-11 21:26:29 +000093>>> args
94['--condition=foo', '--testing', '--output-file', 'abc.def', '-x', 'a1', 'a2']
95>>> optlist, args = getopt.getopt(args, 'x', [
96... 'condition=', 'output-file=', 'testing'])
97>>> optlist
Fred Drakea44d7401998-03-10 03:36:00 +000098[('--condition', 'foo'), ('--testing', ''), ('--output-file', 'abc.def'), ('-x',
99 '')]
Guido van Rossum2f666631996-09-11 21:26:29 +0000100>>> args
101['a1', 'a2']
Fred Drakea8e484c2000-08-11 19:55:06 +0000102\end{verbatim}
103
104In a script, typical usage is something like this:
105
106\begin{verbatim}
107import getopt, sys
108
109def main():
110 try:
111 opts, args = getopt.getopt(sys.argv[1:], "ho:", ["help", "output="])
112 except getopt.GetoptError:
113 # print help information and exit:
114 usage()
115 sys.exit(2)
116 output = None
117 for o, a in opts:
118 if o in ("-h", "--help"):
119 usage()
120 sys.exit()
121 if o in ("-o", "--output"):
122 output = a
123 # ...
124
125if __name__ == "__main__":
126 main()
Fred Drake19479911998-02-13 06:58:54 +0000127\end{verbatim}