blob: 6731c3220a26f45297bbf01a121daf0aa572c4ec [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
32equal sign (\character{=}).
Fred Drakea44d7401998-03-10 03:36:00 +000033
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000034The return value consists of two elements: the first is a list of
Fred Drakea44d7401998-03-10 03:36:00 +000035\code{(\var{option}, \var{value})} pairs; the second is the list of
36program arguments left after the option list was stripped (this is a
Fred Drakea8e484c2000-08-11 19:55:06 +000037trailing slice of \var{args}). Each option-and-value pair returned
38has the option as its first element, prefixed with a hyphen for short
39options (e.g., \code{'-x'}) or two hyphens for long options (e.g.,
40\code{'-}\code{-long-option'}), and the option argument as its second
41element, or an empty string if the option has no argument. The
42options occur in the list in the same order in which they were found,
43thus allowing multiple occurrences. Long and short options may be
44mixed.
Fred Drakea44d7401998-03-10 03:36:00 +000045\end{funcdesc}
46
Fred Drake76e6da31999-12-21 22:50:05 +000047\begin{excdesc}{GetoptError}
Fred Drakea44d7401998-03-10 03:36:00 +000048This is raised when an unrecognized option is found in the argument
49list or when an option requiring an argument is given none.
50The argument to the exception is a string indicating the cause of the
51error. For long options, an argument given to an option which does
Fred Drake76e6da31999-12-21 22:50:05 +000052not require one will also cause this exception to be raised. The
53attributes \member{msg} and \member{opt} give the error message and
54related option; if there is no specific option to which the exception
55relates, \member{opt} is an empty string.
56\end{excdesc}
57
58\begin{excdesc}{error}
59Alias for \exception{GetoptError}; for backward compatibility.
Fred Drakea44d7401998-03-10 03:36:00 +000060\end{excdesc}
61
Guido van Rossum2f666631996-09-11 21:26:29 +000062
63An example using only \UNIX{} style options:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000064
Fred Drake19479911998-02-13 06:58:54 +000065\begin{verbatim}
Fred Drakea8e484c2000-08-11 19:55:06 +000066>>> import getopt
67>>> args = '-a -b -cfoo -d bar a1 a2'.split()
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000068>>> args
69['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2']
70>>> optlist, args = getopt.getopt(args, 'abc:d:')
71>>> optlist
72[('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')]
73>>> args
74['a1', 'a2']
Fred Drake19479911998-02-13 06:58:54 +000075\end{verbatim}
Fred Drakea44d7401998-03-10 03:36:00 +000076
Guido van Rossum2f666631996-09-11 21:26:29 +000077Using long option names is equally easy:
78
Fred Drake19479911998-02-13 06:58:54 +000079\begin{verbatim}
Guido van Rossum2f666631996-09-11 21:26:29 +000080>>> s = '--condition=foo --testing --output-file abc.def -x a1 a2'
Fred Drakea8e484c2000-08-11 19:55:06 +000081>>> args = s.split()
Guido van Rossum2f666631996-09-11 21:26:29 +000082>>> args
83['--condition=foo', '--testing', '--output-file', 'abc.def', '-x', 'a1', 'a2']
84>>> optlist, args = getopt.getopt(args, 'x', [
85... 'condition=', 'output-file=', 'testing'])
86>>> optlist
Fred Drakea44d7401998-03-10 03:36:00 +000087[('--condition', 'foo'), ('--testing', ''), ('--output-file', 'abc.def'), ('-x',
88 '')]
Guido van Rossum2f666631996-09-11 21:26:29 +000089>>> args
90['a1', 'a2']
Fred Drakea8e484c2000-08-11 19:55:06 +000091\end{verbatim}
92
93In a script, typical usage is something like this:
94
95\begin{verbatim}
96import getopt, sys
97
98def main():
99 try:
100 opts, args = getopt.getopt(sys.argv[1:], "ho:", ["help", "output="])
101 except getopt.GetoptError:
102 # print help information and exit:
103 usage()
104 sys.exit(2)
105 output = None
106 for o, a in opts:
107 if o in ("-h", "--help"):
108 usage()
109 sys.exit()
110 if o in ("-o", "--output"):
111 output = a
112 # ...
113
114if __name__ == "__main__":
115 main()
Fred Drake19479911998-02-13 06:58:54 +0000116\end{verbatim}