blob: 72824a7d9562f65635812c2729e4da305c138fdd [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
24(i.e., the same format that \UNIX{} \cfunction{getopt()} uses). If
25specified, \var{long_options} is a list of strings with the names of
26the long options which should be supported. The leading
27\code{'-}\code{-'} characters should not be included in the option
28name. Options which require an argument should be followed by an
29equal sign (\code{'='}).
30
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000031The return value consists of two elements: the first is a list of
Fred Drakea44d7401998-03-10 03:36:00 +000032\code{(\var{option}, \var{value})} pairs; the second is the list of
33program arguments left after the option list was stripped (this is a
34trailing slice of the first argument).
35Each option-and-value pair returned has the option as its first
36element, prefixed with a hyphen (e.g., \code{'-x'}), and the option
37argument as its second element, or an empty string if the option has
38no argument.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000039The options occur in the list in the same order in which they were
Guido van Rossum2f666631996-09-11 21:26:29 +000040found, thus allowing multiple occurrences. Long and short options may
41be mixed.
Fred Drakea44d7401998-03-10 03:36:00 +000042\end{funcdesc}
43
44\begin{excdesc}{error}
45This is raised when an unrecognized option is found in the argument
46list or when an option requiring an argument is given none.
47The argument to the exception is a string indicating the cause of the
48error. For long options, an argument given to an option which does
49not require one will also cause this exception to be raised.
50\end{excdesc}
51
Guido van Rossum2f666631996-09-11 21:26:29 +000052
53An example using only \UNIX{} style options:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000054
Fred Drake19479911998-02-13 06:58:54 +000055\begin{verbatim}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000056>>> import getopt, string
57>>> args = string.split('-a -b -cfoo -d bar a1 a2')
58>>> args
59['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2']
60>>> optlist, args = getopt.getopt(args, 'abc:d:')
61>>> optlist
62[('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')]
63>>> args
64['a1', 'a2']
65>>>
Fred Drake19479911998-02-13 06:58:54 +000066\end{verbatim}
Fred Drakea44d7401998-03-10 03:36:00 +000067
Guido van Rossum2f666631996-09-11 21:26:29 +000068Using long option names is equally easy:
69
Fred Drake19479911998-02-13 06:58:54 +000070\begin{verbatim}
Guido van Rossum2f666631996-09-11 21:26:29 +000071>>> s = '--condition=foo --testing --output-file abc.def -x a1 a2'
72>>> args = string.split(s)
73>>> args
74['--condition=foo', '--testing', '--output-file', 'abc.def', '-x', 'a1', 'a2']
75>>> optlist, args = getopt.getopt(args, 'x', [
76... 'condition=', 'output-file=', 'testing'])
77>>> optlist
Fred Drakea44d7401998-03-10 03:36:00 +000078[('--condition', 'foo'), ('--testing', ''), ('--output-file', 'abc.def'), ('-x',
79 '')]
Guido van Rossum2f666631996-09-11 21:26:29 +000080>>> args
81['a1', 'a2']
82>>>
Fred Drake19479911998-02-13 06:58:54 +000083\end{verbatim}