blob: 563460b30b55ec1f4a790e27fb72b17964ebc1be [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
28\var{long_options}, if specified, must be a list of strings with the
29names of the long options which should be supported. The leading
Fred Drakea44d7401998-03-10 03:36:00 +000030\code{'-}\code{-'} characters should not be included in the option
Fred Drakea8e484c2000-08-11 19:55:06 +000031name. Long options which require an argument should be followed by an
Fred Drake45b1d6a2001-01-08 16:05:51 +000032equal sign (\character{=}). To accept only long options,
33\var{options} should be an empty string. Long options on the command
34line can be recognized so long as they provide a prefix of the option
35name that matches exactly one of the accepted options. For example,
36it \var{long_options} is \code{['foo', 'frob']}, the option
37\longprogramopt{fo} will match as \longprogramopt{foo}, but
38\longprogramopt{f} will not match uniquely, so \exception{GetoptError}
39will be raised.
Fred Drakea44d7401998-03-10 03:36:00 +000040
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000041The return value consists of two elements: the first is a list of
Fred Drakea44d7401998-03-10 03:36:00 +000042\code{(\var{option}, \var{value})} pairs; the second is the list of
43program arguments left after the option list was stripped (this is a
Fred Drakea8e484c2000-08-11 19:55:06 +000044trailing slice of \var{args}). Each option-and-value pair returned
45has the option as its first element, prefixed with a hyphen for short
46options (e.g., \code{'-x'}) or two hyphens for long options (e.g.,
47\code{'-}\code{-long-option'}), and the option argument as its second
48element, or an empty string if the option has no argument. The
49options occur in the list in the same order in which they were found,
50thus allowing multiple occurrences. Long and short options may be
51mixed.
Fred Drakea44d7401998-03-10 03:36:00 +000052\end{funcdesc}
53
Fred Drake76e6da31999-12-21 22:50:05 +000054\begin{excdesc}{GetoptError}
Fred Drakea44d7401998-03-10 03:36:00 +000055This is raised when an unrecognized option is found in the argument
56list or when an option requiring an argument is given none.
57The argument to the exception is a string indicating the cause of the
58error. For long options, an argument given to an option which does
Fred Drake76e6da31999-12-21 22:50:05 +000059not require one will also cause this exception to be raised. The
60attributes \member{msg} and \member{opt} give the error message and
61related option; if there is no specific option to which the exception
62relates, \member{opt} is an empty string.
63\end{excdesc}
64
65\begin{excdesc}{error}
66Alias for \exception{GetoptError}; for backward compatibility.
Fred Drakea44d7401998-03-10 03:36:00 +000067\end{excdesc}
68
Guido van Rossum2f666631996-09-11 21:26:29 +000069
70An example using only \UNIX{} style options:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000071
Fred Drake19479911998-02-13 06:58:54 +000072\begin{verbatim}
Fred Drakea8e484c2000-08-11 19:55:06 +000073>>> import getopt
74>>> args = '-a -b -cfoo -d bar a1 a2'.split()
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000075>>> args
76['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2']
77>>> optlist, args = getopt.getopt(args, 'abc:d:')
78>>> optlist
79[('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')]
80>>> args
81['a1', 'a2']
Fred Drake19479911998-02-13 06:58:54 +000082\end{verbatim}
Fred Drakea44d7401998-03-10 03:36:00 +000083
Guido van Rossum2f666631996-09-11 21:26:29 +000084Using long option names is equally easy:
85
Fred Drake19479911998-02-13 06:58:54 +000086\begin{verbatim}
Guido van Rossum2f666631996-09-11 21:26:29 +000087>>> s = '--condition=foo --testing --output-file abc.def -x a1 a2'
Fred Drakea8e484c2000-08-11 19:55:06 +000088>>> args = s.split()
Guido van Rossum2f666631996-09-11 21:26:29 +000089>>> args
90['--condition=foo', '--testing', '--output-file', 'abc.def', '-x', 'a1', 'a2']
91>>> optlist, args = getopt.getopt(args, 'x', [
92... 'condition=', 'output-file=', 'testing'])
93>>> optlist
Fred Drakea44d7401998-03-10 03:36:00 +000094[('--condition', 'foo'), ('--testing', ''), ('--output-file', 'abc.def'), ('-x',
95 '')]
Guido van Rossum2f666631996-09-11 21:26:29 +000096>>> args
97['a1', 'a2']
Fred Drakea8e484c2000-08-11 19:55:06 +000098\end{verbatim}
99
100In a script, typical usage is something like this:
101
102\begin{verbatim}
103import getopt, sys
104
105def main():
106 try:
107 opts, args = getopt.getopt(sys.argv[1:], "ho:", ["help", "output="])
108 except getopt.GetoptError:
109 # print help information and exit:
110 usage()
111 sys.exit(2)
112 output = None
113 for o, a in opts:
114 if o in ("-h", "--help"):
115 usage()
116 sys.exit()
117 if o in ("-o", "--output"):
118 output = a
119 # ...
120
121if __name__ == "__main__":
122 main()
Fred Drake19479911998-02-13 06:58:54 +0000123\end{verbatim}