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