blob: 1c17ab5f8477bce129ac44957a889a231eeedf03 [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
Fred Drake76e6da31999-12-21 22:50:05 +000044\begin{excdesc}{GetoptError}
Fred Drakea44d7401998-03-10 03:36:00 +000045This 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
Fred Drake76e6da31999-12-21 22:50:05 +000049not require one will also cause this exception to be raised. The
50attributes \member{msg} and \member{opt} give the error message and
51related option; if there is no specific option to which the exception
52relates, \member{opt} is an empty string.
53\end{excdesc}
54
55\begin{excdesc}{error}
56Alias for \exception{GetoptError}; for backward compatibility.
Fred Drakea44d7401998-03-10 03:36:00 +000057\end{excdesc}
58
Guido van Rossum2f666631996-09-11 21:26:29 +000059
60An example using only \UNIX{} style options:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000061
Fred Drake19479911998-02-13 06:58:54 +000062\begin{verbatim}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000063>>> import getopt, string
64>>> args = string.split('-a -b -cfoo -d bar a1 a2')
65>>> args
66['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2']
67>>> optlist, args = getopt.getopt(args, 'abc:d:')
68>>> optlist
69[('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')]
70>>> args
71['a1', 'a2']
72>>>
Fred Drake19479911998-02-13 06:58:54 +000073\end{verbatim}
Fred Drakea44d7401998-03-10 03:36:00 +000074
Guido van Rossum2f666631996-09-11 21:26:29 +000075Using long option names is equally easy:
76
Fred Drake19479911998-02-13 06:58:54 +000077\begin{verbatim}
Guido van Rossum2f666631996-09-11 21:26:29 +000078>>> s = '--condition=foo --testing --output-file abc.def -x a1 a2'
79>>> args = string.split(s)
80>>> args
81['--condition=foo', '--testing', '--output-file', 'abc.def', '-x', 'a1', 'a2']
82>>> optlist, args = getopt.getopt(args, 'x', [
83... 'condition=', 'output-file=', 'testing'])
84>>> optlist
Fred Drakea44d7401998-03-10 03:36:00 +000085[('--condition', 'foo'), ('--testing', ''), ('--output-file', 'abc.def'), ('-x',
86 '')]
Guido van Rossum2f666631996-09-11 21:26:29 +000087>>> args
88['a1', 'a2']
89>>>
Fred Drake19479911998-02-13 06:58:54 +000090\end{verbatim}