99
1010.. note ::
1111
12- The :mod: ` getopt ` module is a parser for command line options whose API is
13- designed to be familiar to users of the C :c:func: ` !getopt ` function. Users who
14- are unfamiliar with the C :c:func: ` !getopt ` function or who would like to write
15- less code and get better help and error messages should consider using the
16- :mod: `argparse ` module instead .
12+ This module is considered feature complete. A more object-oriented and
13+ extensible alternative to this API is provided in the :mod: ` optparse `
14+ module. Further functional enhancements for command line parameter
15+ processing are provided either as third party modules on PyPI,
16+ or else as features in the :mod: `argparse ` module.
1717
1818--------------
1919
@@ -23,6 +23,12 @@ the special meanings of arguments of the form '``-``' and '``--``'). Long
2323options similar to those supported by GNU software may be used as well via an
2424optional third argument.
2525
26+ Users who are unfamiliar with the Unix :c:func: `!getopt ` function should consider
27+ using the :mod: `argparse ` module instead. Users who are familiar with the Unix
28+ :c:func: `!getopt ` function, but would like to get equivalent behavior while
29+ writing less code and getting better help and error messages should consider
30+ using the :mod: `optparse ` module.
31+
2632This module provides two functions and an
2733exception:
2834
@@ -150,29 +156,49 @@ and more informative help and error messages by using the :mod:`optparse` module
150156 import optparse
151157
152158 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::
161-
162- import argparse
163-
164- if __name__ == '__main__':
165- parser = argparse.ArgumentParser()
166- parser.add_argument('-o', '--output')
167- parser.add_argument('-v', dest='verbose', action='store_true')
168- args = parser.parse_args()
169- # ... do something with args.output ...
170- # ... do something with args.verbose ..
171-
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 ``- ``.
159+ parser = optparse.OptionParser()
160+ parser.add_option('-o', '--output')
161+ parser.add_option('-v', dest='verbose', action='store_true')
162+ opts, args = parser.parse_args()
163+ process(args, output=opts.output, verbose=opts.verbose)
164+
165+ A roughly equivalent command line interface for this case can also be
166+ produced by using the :mod: `argparse ` module::
167+
168+ import argparse
169+
170+ if __name__ == '__main__':
171+ parser = argparse.ArgumentParser()
172+ parser.add_argument('-o', '--output')
173+ parser.add_argument('-v', dest='verbose', action='store_true')
174+ parser.add_argument('rest', nargs='*')
175+ args = parser.parse_args()
176+ process(args.rest, output=args.output, verbose=args.verbose)
177+
178+ However, unlike the ``optparse `` example, this ``argparse `` example will
179+ handle some parameter combinations differently from the way the ``getopt ``
180+ version would handle them. For example (amongst other differences):
181+
182+ * supplying ``-o -v `` gives ``output="-v" `` and ``verbose=False ``
183+ for both ``getopt `` and ``optparse ``,
184+ but a usage error with ``argparse ``
185+ (complaining that no value has been supplied for ``-o/--output ``,
186+ since ``-v `` is interpreted as meaning the verbosity flag)
187+ * similarly, supplying ``-o -- `` gives ``output="--" `` and ``args=() ``
188+ for both ``getopt `` and ``optparse ``,
189+ but a usage error with ``argparse ``
190+ (also complaining that no value has been supplied for ``-o/--output ``,
191+ since ``-- `` is interpreted as terminating the option processing
192+ and treating all remaining values as positional arguments)
193+ * supplying ``-o=foo `` gives ``output="=foo" ``
194+ for both ``getopt `` and ``optparse ``,
195+ but gives ``output="foo" `` with ``argparse ``
196+ (since ``= `` is special cased as an alternative separator for
197+ option parameter values)
198+
199+ Whether these differing behaviors in the ``argparse `` version are
200+ considered desirable or a problem will depend on the specific command line
201+ application use case.
176202
177203.. seealso ::
178204
0 commit comments