blob: 4f32225f3fc9b810eeed58dfaddfb88015a3faf2 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{getopt} ---
2 Parser for command line options.}
Fred Drakeb91e9341998-07-23 17:59:49 +00003\declaremodule{standard}{getopt}
4
5\modulesynopsis{Parser for command line options.}
6
Fred Drakea44d7401998-03-10 03:36:00 +00007
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00008This module helps scripts to parse the command line arguments in
9\code{sys.argv}.
Fred Drakea44d7401998-03-10 03:36:00 +000010It supports the same conventions as the \UNIX{} \cfunction{getopt()}
Guido van Rossum470be141995-03-17 16:07:09 +000011function (including the special meanings of arguments of the form
Guido van Rossume8d94a81997-04-02 06:05:07 +000012`\code{-}' and `\code{-}\code{-}').
13% That's to fool latex2html into leaving the two hyphens alone!
14Long options similar to those supported by
Guido van Rossum2f666631996-09-11 21:26:29 +000015GNU software may be used as well via an optional third argument.
Fred Drakea44d7401998-03-10 03:36:00 +000016This module provides a single function and an exception:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000017
Fred Drakea44d7401998-03-10 03:36:00 +000018\begin{funcdesc}{getopt}{args, options\optional{, long_options}}
19Parses command line options and parameter list. \var{args} is the
20argument list to be parsed, without the leading reference to the
21running program. Typically, this means \samp{sys.argv[1:]}.
22\var{options} is the string of option letters that the script wants to
23recognize, with options that require an argument followed by a colon
Fred Drakea8e484c2000-08-11 19:55:06 +000024(\character{:}; i.e., the same format that \UNIX{}
25\cfunction{getopt()} uses).
26
27\var{long_options}, if specified, must be a list of strings with the
28names of the long options which should be supported. The leading
Fred Drakea44d7401998-03-10 03:36:00 +000029\code{'-}\code{-'} characters should not be included in the option
Fred Drakea8e484c2000-08-11 19:55:06 +000030name. Long options which require an argument should be followed by an
31equal sign (\character{=}).
Fred Drakea44d7401998-03-10 03:36:00 +000032
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000033The return value consists of two elements: the first is a list of
Fred Drakea44d7401998-03-10 03:36:00 +000034\code{(\var{option}, \var{value})} pairs; the second is the list of
35program arguments left after the option list was stripped (this is a
Fred Drakea8e484c2000-08-11 19:55:06 +000036trailing slice of \var{args}). Each option-and-value pair returned
37has the option as its first element, prefixed with a hyphen for short
38options (e.g., \code{'-x'}) or two hyphens for long options (e.g.,
39\code{'-}\code{-long-option'}), and the option argument as its second
40element, or an empty string if the option has no argument. The
41options occur in the list in the same order in which they were found,
42thus allowing multiple occurrences. Long and short options may be
43mixed.
Fred Drakea44d7401998-03-10 03:36:00 +000044\end{funcdesc}
45
Fred Drake76e6da31999-12-21 22:50:05 +000046\begin{excdesc}{GetoptError}
Fred Drakea44d7401998-03-10 03:36:00 +000047This is raised when an unrecognized option is found in the argument
48list or when an option requiring an argument is given none.
49The argument to the exception is a string indicating the cause of the
50error. For long options, an argument given to an option which does
Fred Drake76e6da31999-12-21 22:50:05 +000051not require one will also cause this exception to be raised. The
52attributes \member{msg} and \member{opt} give the error message and
53related option; if there is no specific option to which the exception
54relates, \member{opt} is an empty string.
55\end{excdesc}
56
57\begin{excdesc}{error}
58Alias for \exception{GetoptError}; for backward compatibility.
Fred Drakea44d7401998-03-10 03:36:00 +000059\end{excdesc}
60
Guido van Rossum2f666631996-09-11 21:26:29 +000061
62An example using only \UNIX{} style options:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000063
Fred Drake19479911998-02-13 06:58:54 +000064\begin{verbatim}
Fred Drakea8e484c2000-08-11 19:55:06 +000065>>> import getopt
66>>> args = '-a -b -cfoo -d bar a1 a2'.split()
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000067>>> args
68['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2']
69>>> optlist, args = getopt.getopt(args, 'abc:d:')
70>>> optlist
71[('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')]
72>>> args
73['a1', 'a2']
Fred Drake19479911998-02-13 06:58:54 +000074\end{verbatim}
Fred Drakea44d7401998-03-10 03:36:00 +000075
Guido van Rossum2f666631996-09-11 21:26:29 +000076Using long option names is equally easy:
77
Fred Drake19479911998-02-13 06:58:54 +000078\begin{verbatim}
Guido van Rossum2f666631996-09-11 21:26:29 +000079>>> s = '--condition=foo --testing --output-file abc.def -x a1 a2'
Fred Drakea8e484c2000-08-11 19:55:06 +000080>>> args = s.split()
Guido van Rossum2f666631996-09-11 21:26:29 +000081>>> args
82['--condition=foo', '--testing', '--output-file', 'abc.def', '-x', 'a1', 'a2']
83>>> optlist, args = getopt.getopt(args, 'x', [
84... 'condition=', 'output-file=', 'testing'])
85>>> optlist
Fred Drakea44d7401998-03-10 03:36:00 +000086[('--condition', 'foo'), ('--testing', ''), ('--output-file', 'abc.def'), ('-x',
87 '')]
Guido van Rossum2f666631996-09-11 21:26:29 +000088>>> args
89['a1', 'a2']
Fred Drakea8e484c2000-08-11 19:55:06 +000090\end{verbatim}
91
92In a script, typical usage is something like this:
93
94\begin{verbatim}
95import getopt, sys
96
97def main():
98 try:
99 opts, args = getopt.getopt(sys.argv[1:], "ho:", ["help", "output="])
100 except getopt.GetoptError:
101 # print help information and exit:
102 usage()
103 sys.exit(2)
104 output = None
105 for o, a in opts:
106 if o in ("-h", "--help"):
107 usage()
108 sys.exit()
109 if o in ("-o", "--output"):
110 output = a
111 # ...
112
113if __name__ == "__main__":
114 main()
Fred Drake19479911998-02-13 06:58:54 +0000115\end{verbatim}