1 \section{Standard Module
\module{getopt
}}
5 This module helps scripts to parse the command line arguments in
7 It supports the same conventions as the
\UNIX{} \cfunction{getopt()
}
8 function (including the special meanings of arguments of the form
9 `
\code{-
}' and `
\code{-
}\code{-
}').
10 % That's to fool latex2html into leaving the two hyphens alone!
11 Long options similar to those supported by
12 GNU software may be used as well via an optional third argument.
13 This module provides a single function and an exception:
15 \begin{funcdesc
}{getopt
}{args, options
\optional{, long_options
}}
16 Parses command line options and parameter list.
\var{args
} is the
17 argument list to be parsed, without the leading reference to the
18 running program. Typically, this means
\samp{sys.argv
[1:
]}.
19 \var{options
} is the string of option letters that the script wants to
20 recognize, with options that require an argument followed by a colon
21 (i.e., the same format that
\UNIX{} \cfunction{getopt()
} uses). If
22 specified,
\var{long_options
} is a list of strings with the names of
23 the long options which should be supported. The leading
24 \code{'-
}\code{-'
} characters should not be included in the option
25 name. Options which require an argument should be followed by an
26 equal sign (
\code{'='
}).
28 The return value consists of two elements: the first is a list of
29 \code{(
\var{option
},
\var{value
})
} pairs; the second is the list of
30 program arguments left after the option list was stripped (this is a
31 trailing slice of the first argument).
32 Each option-and-value pair returned has the option as its first
33 element, prefixed with a hyphen (e.g.,
\code{'-x'
}), and the option
34 argument as its second element, or an empty string if the option has
36 The options occur in the list in the same order in which they were
37 found, thus allowing multiple occurrences. Long and short options may
41 \begin{excdesc
}{error
}
42 This is raised when an unrecognized option is found in the argument
43 list or when an option requiring an argument is given none.
44 The argument to the exception is a string indicating the cause of the
45 error. For long options, an argument given to an option which does
46 not require one will also cause this exception to be raised.
50 An example using only
\UNIX{} style options:
53 >>> import getopt, string
54 >>> args = string.split('-a -b -cfoo -d bar a1 a2')
56 ['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2'
]
57 >>> optlist, args = getopt.getopt(args, 'abc:d:')
59 [('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')
]
65 Using long option names is equally easy:
68 >>> s = '--condition=foo --testing --output-file abc.def -x a1 a2'
69 >>> args = string.split(s)
71 ['--condition=foo', '--testing', '--output-file', 'abc.def', '-x', 'a1', 'a2'
]
72 >>> optlist, args = getopt.getopt(args, 'x',
[
73 ... 'condition=', 'output-file=', 'testing'
])
75 [('--condition', 'foo'), ('--testing', ''), ('--output-file', 'abc.def'), ('-x',