improve treatment of multi-line replies, ignore empty lines
[python/dscho.git] / Doc / libgetopt.tex
blob250d31f4756496b95d7f853fc2c33adc195422bf
1 \section{Standard Module \sectcode{getopt}}
3 \stmodindex{getopt}
4 This module helps scripts to parse the command line arguments in
5 \code{sys.argv}.
6 It uses the same conventions as the \UNIX{}
7 \code{getopt()}
8 function.
9 It defines the function
10 \code{getopt.getopt(args, options)}
11 and the exception
12 \code{getopt.error}.
14 The first argument to
15 \code{getopt()}
16 is the argument list passed to the script with its first element
17 chopped off (i.e.,
18 \code{sys.argv[1:]}).
19 The second argument is the string of option letters that the
20 script wants to recognize, with options that require an argument
21 followed by a colon (i.e., the same format that \UNIX{}
22 \code{getopt()}
23 uses).
24 The return value consists of two elements: the first is a list of
25 option-and-value pairs; the second is the list of program arguments
26 left after the option list was stripped (this is a trailing slice of the
27 first argument).
28 Each option-and-value pair returned has the option as its first element,
29 prefixed with a hyphen (e.g.,
30 \code{'-x'}),
31 and the option argument as its second element, or an empty string if the
32 option has no argument.
33 The options occur in the list in the same order in which they were
34 found, thus allowing multiple occurrences.
35 Example:
37 \bcode\begin{verbatim}
38 >>> import getopt, string
39 >>> args = string.split('-a -b -cfoo -d bar a1 a2')
40 >>> args
41 ['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2']
42 >>> optlist, args = getopt.getopt(args, 'abc:d:')
43 >>> optlist
44 [('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')]
45 >>> args
46 ['a1', 'a2']
47 >>>
48 \end{verbatim}\ecode
50 The exception
51 \code{getopt.error = 'getopt error'}
52 is raised when an unrecognized option is found in the argument list or
53 when an option requiring an argument is given none.
54 The argument to the exception is a string indicating the cause of the
55 error.