Skip to content

Commit 55b7215

Browse files
Incorporate improvements from Serhiy's PR
Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 0a568f1 commit 55b7215

File tree

3 files changed

+34
-10
lines changed

3 files changed

+34
-10
lines changed

Doc/library/argparse.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@
1414
.. note::
1515

1616
While :mod:`argparse` is the recommended standard library module for
17-
*implementing* command line applications, authors of third party
17+
*implementing* basic command line applications, authors of third party
1818
command line argument processing libraries may find that the
1919
lower level :mod:`optparse` module serves as a better foundation for
20-
that use case.
20+
that use case. ``optparse`` (or one of the third party libraries
21+
based on it) may also be worth considering for applications where
22+
stricter adherence to common Unix and Linux command line interface
23+
conventions around the handling of option parameter values that start
24+
with ``-`` is desired.
2125

2226
--------------
2327

Doc/library/getopt.rst

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

147147
Note 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.

Doc/library/optparse.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,11 @@
4646
--------------
4747

4848
:mod:`optparse` is a more convenient, flexible, and powerful library for parsing
49-
command-line options than the old :mod:`getopt` module. :mod:`optparse` uses a
50-
more declarative style of command-line parsing: you create an instance of
51-
:class:`OptionParser`, populate it with options, and parse the command
52-
line. :mod:`optparse` allows users to specify options in the conventional
49+
command-line options than the minimalist :mod:`getopt` module.
50+
:mod:`optparse` uses a more declarative style of command-line parsing:
51+
you create an instance of :class:`OptionParser`,
52+
populate it with options, and parse the command line.
53+
:mod:`optparse` allows users to specify options in the conventional
5354
GNU/POSIX syntax, and additionally generates usage and help messages for you.
5455

5556
Here's an example of using :mod:`optparse` in a simple script::

0 commit comments

Comments
 (0)