[geneagrapher] 207/226: Replace optparse with argparse.

Doug Torrance dtorrance-guest at moszumanska.debian.org
Sat Jul 11 17:11:12 UTC 2015


This is an automated email from the git hooks/post-receive script.

dtorrance-guest pushed a commit to branch master
in repository geneagrapher.

commit f4fd40e17f5bc398ec669e49cf720f6e2e978d87
Author: David Alber <alber.david at gmail.com>
Date:   Fri Dec 30 09:14:06 2011 -0800

    Replace optparse with argparse.
    
    The optparse module is deprecated and has been replaced with argparse.
    This change replaces the optparse code with argparse.
    
    The argparse module is available starting in Python 2.7, so this
    decision may need to be reconsidered if it is blocking users from
    using the Geneagrapher.
---
 src/geneagrapher/geneagrapher.py                | 61 +++++++++++++------------
 tests/geneagrapher/test_geneagrapher_methods.py |  6 +--
 2 files changed, 34 insertions(+), 33 deletions(-)

diff --git a/src/geneagrapher/geneagrapher.py b/src/geneagrapher/geneagrapher.py
index 28cdb07..04d2a26 100644
--- a/src/geneagrapher/geneagrapher.py
+++ b/src/geneagrapher/geneagrapher.py
@@ -1,4 +1,4 @@
-from optparse import OptionParser
+from argparse import ArgumentParser
 from collections import deque
 import pkg_resources
 from graph import Graph
@@ -23,39 +23,40 @@ class Geneagrapher:
         Parse command-line information.
         """
         pkg_env = pkg_resources.Environment()
-        self.parser = OptionParser(version="{}".format(
-            pkg_env[self.__module__.split('.')[0]][0].version))
+        description = 'Create a Graphviz "dot" file for a mathematics \
+genealogy, where ID is a record identifier from the Mathematics Genealogy \
+Project. Multiple IDs may be passed.'
+        self.parser = ArgumentParser(description=description)
 
-        self.parser.set_usage("%prog [options] ID [ID...]")
-        self.parser.set_description('Create a Graphviz "dot" file for a \
-mathematics genealogy, where ID is a record identifier from the Mathematics \
-Genealogy Project. Multiple IDs may be passed.')
-
-        self.parser.add_option("-f", "--file", dest="filename",
-                               help="write output to FILE [default: stdout]",
-                               metavar="FILE", default=None)
-        self.parser.add_option("-a", "--with-ancestors", action="store_true",
-                               dest="get_ancestors", default=False,
-                               help="retrieve ancestors of IDs and include in \
-graph")
-        self.parser.add_option("-d", "--with-descendants", action="store_true",
-                               dest="get_descendants", default=False,
-                               help="retrieve descendants of IDs and include \
+        version = "{}".format(pkg_env[self.__module__.split('.')[0]][0].
+                              version)
+        self.parser.add_argument('--version', action='version',
+                                 version='%(prog)s {0}'.format(version))
+        self.parser.add_argument("-f", "--file", dest="filename",
+                                 help="write output to FILE [default: stdout]",
+                                 metavar="FILE", default=None)
+        self.parser.add_argument("-a", "--with-ancestors", action="store_true",
+                                 dest="get_ancestors", default=False,
+                                 help="retrieve ancestors of IDs and include \
 in graph")
-        self.parser.add_option("--verbose", "-v", action="store_true",
-                               dest="verbose", default=False,
-                               help="list nodes being retrieved")
-
-        (options, args) = self.parser.parse_args()
+        self.parser.add_argument("-d", "--with-descendants",
+                                 action="store_true", dest="get_descendants",
+                                 default=False,
+                                 help="retrieve descendants of IDs and \
+include in graph")
+        self.parser.add_argument("-v", "--verbose", action="store_true",
+                                 dest="verbose", default=False,
+                                 help="list nodes being retrieved")
+        self.parser.add_argument('ids', metavar='ID', type=int, nargs='+',
+                            help='mathematician record ID')
 
-        if len(args) == 0:
-            self.parser.error("no record IDs given")
+        args = self.parser.parse_args()
 
-        self.get_ancestors = options.get_ancestors
-        self.get_descendants = options.get_descendants
-        self.verbose = options.verbose
-        self.write_filename = options.filename
-        self.seed_ids = [int(arg) for arg in args]
+        self.get_ancestors = args.get_ancestors
+        self.get_descendants = args.get_descendants
+        self.verbose = args.verbose
+        self.write_filename = args.filename
+        self.seed_ids = [int(arg) for arg in args.ids]
 
     def build_graph_portion(self, grab_queue, is_seed, grabber, **kwargs):
         """Handle grabbing and storing nodes in the graph. Depending on the
diff --git a/tests/geneagrapher/test_geneagrapher_methods.py b/tests/geneagrapher/test_geneagrapher_methods.py
index 8c2c7fc..b13dd7c 100644
--- a/tests/geneagrapher/test_geneagrapher_methods.py
+++ b/tests/geneagrapher/test_geneagrapher_methods.py
@@ -30,9 +30,9 @@ class TestGeneagrapherMethods(unittest.TestCase):
         stderr_intercept = StringIO.StringIO()
         sys.stderr = stderr_intercept
 
-        expected = """Usage: geneagrapher [options] ID [ID...]
-
-geneagrapher: error: no record IDs given
+        expected = """usage: geneagrapher [-h] [--version] [-f FILE] [-a] \
+[-d] [-v] ID [ID ...]
+geneagrapher: error: too few arguments
 """
         try:
             self.ggrapher.parse_input()

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/geneagrapher.git



More information about the debian-science-commits mailing list