@@ -139,13 +139,25 @@ In a script, typical usage is something like this::
139139 output = a
140140 else:
141141 assert False, "unhandled option"
142- # ...
142+ process(args, output=output, verbose=verbose)
143143
144144 if __name__ == "__main__":
145145 main()
146146
147147Note that an equivalent command line interface could be produced with less code
148- and more informative help and error messages by using the :mod: `argparse ` module::
148+ and more informative help and error messages by using the :mod: `optparse ` module::
149+
150+ import optparse
151+
152+ if __name__ == '__main__':
153+ parser = optparse.OptionParser()
154+ parser.add_option('-o', '--output')
155+ parser.add_option('-v', dest='verbose', action='store_true')
156+ opts, args = parser.parse_args()
157+ process(args, output=opts.output, verbose=opts.verbose)
158+
159+ An equivalent command line interface for this simple case can also be produced
160+ by using the :mod: `argparse ` module::
149161
150162 import argparse
151163
@@ -157,8 +169,15 @@ and more informative help and error messages by using the :mod:`argparse` module
157169 # ... do something with args.output ...
158170 # ... do something with args.verbose ..
159171
172+ In more complex cases (such as options which accept values), the behaviour
173+ of the ``argparse `` version may diverge from that of the ``getopt `` and
174+ ``optparse `` versions due to the way ``argparse `` handles parameter
175+ values that start with ``- ``.
176+
160177.. seealso ::
161178
162- Module :mod: `argparse `
163- Alternative command line option and argument parsing library .
179+ Module :mod: `optparse `
180+ More object-oriented command line option parsing.
164181
182+ Module :mod: `argparse `
183+ More opinionated command line option and argument parsing library.
0 commit comments