]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
argparse documentation updates (including updates to optparse and getopt documentatio...
authorSteven Bethard <steven.bethard@gmail.com>
Mon, 24 May 2010 02:38:00 +0000 (02:38 +0000)
committerSteven Bethard <steven.bethard@gmail.com>
Mon, 24 May 2010 02:38:00 +0000 (02:38 +0000)
Doc/library/argparse.rst
Doc/library/getopt.rst
Doc/library/optparse.rst

index 4f154c41d3f88461ab0c2ee8c4365ebd872ade61..7a5655437f456917eb3709d03ac7bcdb9a29ecb4 100644 (file)
@@ -672,8 +672,8 @@ command-line args should be handled. The supported actions are:
 
     >>> import argparse
     >>> parser = argparse.ArgumentParser(prog='PROG')
-    >>> parser.add_argument('-v', '--version', action='version', version='%(prog)s 2.0')
-    >>> parser.parse_args(['-v'])
+    >>> parser.add_argument('--version', action='version', version='%(prog)s 2.0')
+    >>> parser.parse_args(['--version'])
     PROG 2.0
 
 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:
 * Replace strings with implicit arguments such as ``%default`` or ``%prog`` with
   the standard python syntax to use dictionaries to format strings, that is,
   ``%(default)s`` and ``%(prog)s``.
+
+* Replace the OptionParser constructor ``version`` argument with a call to
+  ``parser.add_argument('--version', action='version', version='<the version>')``
index 91bf34342e20511ec6dfd59d54713af609bcb906..3114a82877a91d90f0c969ce1de9b04d3db29418 100644 (file)
@@ -6,6 +6,12 @@
    :synopsis: Portable parser for command line options; support both short and long option
               names.
 
+.. note::
+   The :mod:`getopt` module is a parser for command line options whose API is
+   designed to be familiar to users of the C :cfunc:`getopt` function. Users who
+   are unfamiliar with the C :cfunc:`getopt` function or who would like to write
+   less code and get better help and error messages should consider using the
+   :mod:`argparse` module instead.
 
 This module helps scripts to parse the command line arguments in ``sys.argv``.
 It supports the same conventions as the Unix :cfunc:`getopt` function (including
@@ -142,9 +148,21 @@ In a script, typical usage is something like this::
    if __name__ == "__main__":
        main()
 
+Note that an equivalent command line interface could be produced with less code
+and more informative help and error messages by using the :mod:`argparse` module::
+
+   import argparse
+
+   if __name__ == '__main__':
+       parser = argparse.ArgumentParser()
+       parser.add_argument('-o', '--output')
+       parser.add_argument('-v', dest='verbose', action='store_true')
+       args = parser.parse_args()
+       # ... do something with args.output ...
+       # ... do something with args.verbose ..
 
 .. seealso::
 
-   Module :mod:`optparse`
-      More object-oriented command line option parsing.
+   Module :mod:`argparse`
+      Alternative command line option and argument parsing library.
 
index 50c62aad4c42ea09fd1f1bf8563c56fff45d042b..9d5b9ff907091ca5e138e13a202bdee7a28cb912 100644 (file)
@@ -3,8 +3,13 @@
 
 .. module:: optparse
    :synopsis: Command-line option parsing library.
-.. moduleauthor:: Greg Ward <gward@python.net>
+   :deprecated:
+
+.. deprecated:: 2.7
+   The :mod:`optparse` module is deprecated and will not be developed further;
+   development will continue with the :mod:`argparse` module.
 
+.. moduleauthor:: Greg Ward <gward@python.net>
 
 .. versionadded:: 2.3