blob: 6f839a7ab03ba257fde2829b7533d9502a72f833 [file] [log] [blame]
Fred Drake3a0351c1998-04-04 07:23:21 +00001\section{Standard Module \module{getopt}}
Fred Drakeb91e9341998-07-23 17:59:49 +00002\declaremodule{standard}{getopt}
3
4\modulesynopsis{Parser for command line options.}
5
Fred Drakea44d7401998-03-10 03:36:00 +00006
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00007This module helps scripts to parse the command line arguments in
8\code{sys.argv}.
Fred Drakea44d7401998-03-10 03:36:00 +00009It supports the same conventions as the \UNIX{} \cfunction{getopt()}
Guido van Rossum470be141995-03-17 16:07:09 +000010function (including the special meanings of arguments of the form
Guido van Rossume8d94a81997-04-02 06:05:07 +000011`\code{-}' and `\code{-}\code{-}').
12% That's to fool latex2html into leaving the two hyphens alone!
13Long options similar to those supported by
Guido van Rossum2f666631996-09-11 21:26:29 +000014GNU software may be used as well via an optional third argument.
Fred Drakea44d7401998-03-10 03:36:00 +000015This module provides a single function and an exception:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000016
Fred Drakea44d7401998-03-10 03:36:00 +000017\begin{funcdesc}{getopt}{args, options\optional{, long_options}}
18Parses command line options and parameter list. \var{args} is the
19argument list to be parsed, without the leading reference to the
20running program. Typically, this means \samp{sys.argv[1:]}.
21\var{options} is the string of option letters that the script wants to
22recognize, with options that require an argument followed by a colon
23(i.e., the same format that \UNIX{} \cfunction{getopt()} uses). If
24specified, \var{long_options} is a list of strings with the names of
25the long options which should be supported. The leading
26\code{'-}\code{-'} characters should not be included in the option
27name. Options which require an argument should be followed by an
28equal sign (\code{'='}).
29
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000030The return value consists of two elements: the first is a list of
Fred Drakea44d7401998-03-10 03:36:00 +000031\code{(\var{option}, \var{value})} pairs; the second is the list of
32program arguments left after the option list was stripped (this is a
33trailing slice of the first argument).
34Each option-and-value pair returned has the option as its first
35element, prefixed with a hyphen (e.g., \code{'-x'}), and the option
36argument as its second element, or an empty string if the option has
37no argument.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000038The options occur in the list in the same order in which they were
Guido van Rossum2f666631996-09-11 21:26:29 +000039found, thus allowing multiple occurrences. Long and short options may
40be mixed.
Fred Drakea44d7401998-03-10 03:36:00 +000041\end{funcdesc}
42
43\begin{excdesc}{error}
44This is raised when an unrecognized option is found in the argument
45list or when an option requiring an argument is given none.
46The argument to the exception is a string indicating the cause of the
47error. For long options, an argument given to an option which does
48not require one will also cause this exception to be raised.
49\end{excdesc}
50
Guido van Rossum2f666631996-09-11 21:26:29 +000051
52An example using only \UNIX{} style options:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000053
Fred Drake19479911998-02-13 06:58:54 +000054\begin{verbatim}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000055>>> import getopt, string
56>>> args = string.split('-a -b -cfoo -d bar a1 a2')
57>>> args
58['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2']
59>>> optlist, args = getopt.getopt(args, 'abc:d:')
60>>> optlist
61[('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')]
62>>> args
63['a1', 'a2']
64>>>
Fred Drake19479911998-02-13 06:58:54 +000065\end{verbatim}
Fred Drakea44d7401998-03-10 03:36:00 +000066
Guido van Rossum2f666631996-09-11 21:26:29 +000067Using long option names is equally easy:
68
Fred Drake19479911998-02-13 06:58:54 +000069\begin{verbatim}
Guido van Rossum2f666631996-09-11 21:26:29 +000070>>> s = '--condition=foo --testing --output-file abc.def -x a1 a2'
71>>> args = string.split(s)
72>>> args
73['--condition=foo', '--testing', '--output-file', 'abc.def', '-x', 'a1', 'a2']
74>>> optlist, args = getopt.getopt(args, 'x', [
75... 'condition=', 'output-file=', 'testing'])
76>>> optlist
Fred Drakea44d7401998-03-10 03:36:00 +000077[('--condition', 'foo'), ('--testing', ''), ('--output-file', 'abc.def'), ('-x',
78 '')]
Guido van Rossum2f666631996-09-11 21:26:29 +000079>>> args
80['a1', 'a2']
81>>>
Fred Drake19479911998-02-13 06:58:54 +000082\end{verbatim}