blob: fff30430e09c48aa578458318a56e893bd010bd7 [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.
Fred Drake293f77a2001-04-18 03:18:57 +000067
68\versionchanged[Introduced \exception{GetoptError} as a synonym for
69 \exception{error}]{1.6}
Fred Drake76e6da31999-12-21 22:50:05 +000070\end{excdesc}
71
72\begin{excdesc}{error}
73Alias for \exception{GetoptError}; for backward compatibility.
Fred Drakea44d7401998-03-10 03:36:00 +000074\end{excdesc}
75
Guido van Rossum2f666631996-09-11 21:26:29 +000076
77An example using only \UNIX{} style options:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000078
Fred Drake19479911998-02-13 06:58:54 +000079\begin{verbatim}
Fred Drakea8e484c2000-08-11 19:55:06 +000080>>> import getopt
81>>> args = '-a -b -cfoo -d bar a1 a2'.split()
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000082>>> args
83['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2']
84>>> optlist, args = getopt.getopt(args, 'abc:d:')
85>>> optlist
86[('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')]
87>>> args
88['a1', 'a2']
Fred Drake19479911998-02-13 06:58:54 +000089\end{verbatim}
Fred Drakea44d7401998-03-10 03:36:00 +000090
Guido van Rossum2f666631996-09-11 21:26:29 +000091Using long option names is equally easy:
92
Fred Drake19479911998-02-13 06:58:54 +000093\begin{verbatim}
Guido van Rossum2f666631996-09-11 21:26:29 +000094>>> s = '--condition=foo --testing --output-file abc.def -x a1 a2'
Fred Drakea8e484c2000-08-11 19:55:06 +000095>>> args = s.split()
Guido van Rossum2f666631996-09-11 21:26:29 +000096>>> args
97['--condition=foo', '--testing', '--output-file', 'abc.def', '-x', 'a1', 'a2']
98>>> optlist, args = getopt.getopt(args, 'x', [
99... 'condition=', 'output-file=', 'testing'])
100>>> optlist
Fred Drakea44d7401998-03-10 03:36:00 +0000101[('--condition', 'foo'), ('--testing', ''), ('--output-file', 'abc.def'), ('-x',
102 '')]
Guido van Rossum2f666631996-09-11 21:26:29 +0000103>>> args
104['a1', 'a2']
Fred Drakea8e484c2000-08-11 19:55:06 +0000105\end{verbatim}
106
107In a script, typical usage is something like this:
108
109\begin{verbatim}
110import getopt, sys
111
112def main():
113 try:
114 opts, args = getopt.getopt(sys.argv[1:], "ho:", ["help", "output="])
115 except getopt.GetoptError:
116 # print help information and exit:
117 usage()
118 sys.exit(2)
119 output = None
120 for o, a in opts:
121 if o in ("-h", "--help"):
122 usage()
123 sys.exit()
124 if o in ("-o", "--output"):
125 output = a
126 # ...
127
128if __name__ == "__main__":
129 main()
Fred Drake19479911998-02-13 06:58:54 +0000130\end{verbatim}