blob: 286cf58555766df795c3b7a7061608f30ca306aa [file] [log] [blame]
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00001\section{Standard Module \sectcode{getopt}}
Guido van Rossume47da0a1997-07-17 16:34:52 +00002\label{module-getopt}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00003
4\stmodindex{getopt}
5This module helps scripts to parse the command line arguments in
6\code{sys.argv}.
Guido van Rossum2f666631996-09-11 21:26:29 +00007It supports the same conventions as the \UNIX{}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00008\code{getopt()}
Guido van Rossum470be141995-03-17 16:07:09 +00009function (including the special meanings of arguments of the form
Guido van Rossume8d94a81997-04-02 06:05:07 +000010`\code{-}' and `\code{-}\code{-}').
11% That's to fool latex2html into leaving the two hyphens alone!
12Long options similar to those supported by
Guido van Rossum2f666631996-09-11 21:26:29 +000013GNU software may be used as well via an optional third argument.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000014It defines the function
Guido van Rossum2f666631996-09-11 21:26:29 +000015\code{getopt.getopt(args, options [, long_options])}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000016and the exception
17\code{getopt.error}.
18
19The first argument to
20\code{getopt()}
21is the argument list passed to the script with its first element
22chopped off (i.e.,
23\code{sys.argv[1:]}).
24The second argument is the string of option letters that the
25script wants to recognize, with options that require an argument
26followed by a colon (i.e., the same format that \UNIX{}
27\code{getopt()}
28uses).
Guido van Rossum2f666631996-09-11 21:26:29 +000029The third option, if specified, is a list of strings with the names of
Guido van Rossume8d94a81997-04-02 06:05:07 +000030the long options which should be supported. The leading \code{'-}\code{-'}
Guido van Rossum2f666631996-09-11 21:26:29 +000031characters should not be included in the option name. Options which
32require an argument should be followed by an equal sign (\code{'='}).
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000033The return value consists of two elements: the first is a list of
34option-and-value pairs; the second is the list of program arguments
35left after the option list was stripped (this is a trailing slice of the
36first argument).
37Each option-and-value pair returned has the option as its first element,
38prefixed with a hyphen (e.g.,
39\code{'-x'}),
40and the option argument as its second element, or an empty string if the
41option has no argument.
42The options occur in the list in the same order in which they were
Guido van Rossum2f666631996-09-11 21:26:29 +000043found, thus allowing multiple occurrences. Long and short options may
44be mixed.
45
46An example using only \UNIX{} style options:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000047
Fred Drake19479911998-02-13 06:58:54 +000048\begin{verbatim}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000049>>> import getopt, string
50>>> args = string.split('-a -b -cfoo -d bar a1 a2')
51>>> args
52['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2']
53>>> optlist, args = getopt.getopt(args, 'abc:d:')
54>>> optlist
55[('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')]
56>>> args
57['a1', 'a2']
58>>>
Fred Drake19479911998-02-13 06:58:54 +000059\end{verbatim}
Guido van Rossume47da0a1997-07-17 16:34:52 +000060%
Guido van Rossum2f666631996-09-11 21:26:29 +000061Using long option names is equally easy:
62
Fred Drake19479911998-02-13 06:58:54 +000063\begin{verbatim}
Guido van Rossum2f666631996-09-11 21:26:29 +000064>>> s = '--condition=foo --testing --output-file abc.def -x a1 a2'
65>>> args = string.split(s)
66>>> args
67['--condition=foo', '--testing', '--output-file', 'abc.def', '-x', 'a1', 'a2']
68>>> optlist, args = getopt.getopt(args, 'x', [
69... 'condition=', 'output-file=', 'testing'])
70>>> optlist
71[('--condition', 'foo'), ('--testing', ''), ('--output-file', 'abc.def'), ('-x', '')]
72>>> args
73['a1', 'a2']
74>>>
Fred Drake19479911998-02-13 06:58:54 +000075\end{verbatim}
Guido van Rossume47da0a1997-07-17 16:34:52 +000076%
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000077The exception
Guido van Rossumeb0f0661997-12-30 20:38:16 +000078\code{getopt.error}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000079is raised when an unrecognized option is found in the argument list or
80when an option requiring an argument is given none.
81The argument to the exception is a string indicating the cause of the
Guido van Rossum2f666631996-09-11 21:26:29 +000082error. For long options, an argument given to an option which does
83not require one will also cause this exception to be raised.