Skip to content

Commit 5971096

Browse files
committed
Merged revisions 81490 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r81490 | steven.bethard | 2010-05-23 19:38:00 -0700 (Sun, 23 May 2010) | 1 line argparse documentation updates (including updates to optparse and getopt documentation that were promised in the PEP) ........
1 parent dc787d2 commit 5971096

3 files changed

Lines changed: 31 additions & 4 deletions

File tree

Doc/library/argparse.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -672,8 +672,8 @@ command-line args should be handled. The supported actions are:
672672

673673
>>> import argparse
674674
>>> parser = argparse.ArgumentParser(prog='PROG')
675-
>>> parser.add_argument('-v', '--version', action='version', version='%(prog)s 2.0')
676-
>>> parser.parse_args(['-v'])
675+
>>> parser.add_argument('--version', action='version', version='%(prog)s 2.0')
676+
>>> parser.parse_args(['--version'])
677677
PROG 2.0
678678

679679
You can also specify an arbitrary action by passing an object that implements
@@ -1725,3 +1725,6 @@ A partial upgrade path from optparse to argparse:
17251725
* Replace strings with implicit arguments such as ``%default`` or ``%prog`` with
17261726
the standard python syntax to use dictionaries to format strings, that is,
17271727
``%(default)s`` and ``%(prog)s``.
1728+
1729+
* Replace the OptionParser constructor ``version`` argument with a call to
1730+
``parser.add_argument('--version', action='version', version='<the version>')``

Doc/library/getopt.rst

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
:synopsis: Portable parser for command line options; support both short and
66
long option names.
77

8+
.. note::
9+
The :mod:`getopt` module is a parser for command line options whose API is
10+
designed to be familiar to users of the C :cfunc:`getopt` function. Users who
11+
are unfamiliar with the C :cfunc:`getopt` function or who would like to write
12+
less code and get better help and error messages should consider using the
13+
:mod:`argparse` module instead.
814

915
This module helps scripts to parse the command line arguments in ``sys.argv``.
1016
It supports the same conventions as the Unix :cfunc:`getopt` function (including
@@ -136,9 +142,21 @@ In a script, typical usage is something like this::
136142
if __name__ == "__main__":
137143
main()
138144

145+
Note that an equivalent command line interface could be produced with less code
146+
and more informative help and error messages by using the :mod:`argparse` module::
147+
148+
import argparse
149+
150+
if __name__ == '__main__':
151+
parser = argparse.ArgumentParser()
152+
parser.add_argument('-o', '--output')
153+
parser.add_argument('-v', dest='verbose', action='store_true')
154+
args = parser.parse_args()
155+
# ... do something with args.output ...
156+
# ... do something with args.verbose ..
139157

140158
.. seealso::
141159

142-
Module :mod:`optparse`
143-
More object-oriented command line option parsing.
160+
Module :mod:`argparse`
161+
Alternative command line option and argument parsing library.
144162

Doc/library/optparse.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33

44
.. module:: optparse
55
:synopsis: Command-line option parsing library.
6+
:deprecated:
7+
8+
.. deprecated:: 2.7
9+
The :mod:`optparse` module is deprecated and will not be developed further;
10+
development will continue with the :mod:`argparse` module.
11+
612
.. moduleauthor:: Greg Ward <gward@python.net>
713
.. sectionauthor:: Greg Ward <gward@python.net>
814

0 commit comments

Comments
 (0)