Browse code

Switch to argparse instead of optparse

optparse is deprecated since 2.7/3.2

Peter Hill authored on 02/07/2016 14:36:19 • Jose Fonseca committed on 10/07/2016 17:03:51
Showing 1 changed files

... ...
@@ -15,24 +15,17 @@
15 15
 # You should have received a copy of the GNU Lesser General Public License
16 16
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 17
 #
18
-import optparse
18
+import argparse
19 19
 import sys
20 20
 
21 21
 from .ui.window import DotWindow, Gtk
22 22
 
23 23
 
24
-class OptionParser(optparse.OptionParser):
25
-
26
-    def format_epilog(self, formatter):
27
-        # Prevent stripping the newlines in epilog message
28
-        # http://stackoverflow.com/questions/1857346/python-optparse-how-to-include-additional-info-in-usage-output
29
-        return self.epilog
30
-
31
-
32 24
 def main():
33 25
 
34
-    parser = OptionParser(
35
-        usage='\n\t%prog [file]',
26
+    parser = argparse.ArgumentParser(
27
+        description="xdot.py is an interactive viewer for graphs written in Graphviz's dot language.",
28
+        formatter_class=argparse.RawDescriptionHelpFormatter,
36 29
         epilog='''
37 30
 Shortcuts:
38 31
   Up, Down, Left, Right     scroll
... ...
@@ -47,23 +40,24 @@ Shortcuts:
47 40
   Shift-drag                zooms an area
48 41
 '''
49 42
     )
50
-    parser.add_option(
51
-        '-f', '--filter',
52
-        type='choice', choices=('dot', 'neato', 'twopi', 'circo', 'fdp'),
53
-        dest='filter', default='dot',
54
-        help='graphviz filter: dot, neato, twopi, circo, or fdp [default: %default]')
55
-    parser.add_option(
43
+    parser.add_argument(
44
+        'inputfile', metavar='file', nargs='?',
45
+        help='input file to be viewed')
46
+    parser.add_argument(
47
+        '-f', '--filter', choices=['dot', 'neato', 'twopi', 'circo', 'fdp'],
48
+        dest='filter', default='dot', metavar='FILTER',
49
+        help='graphviz filter: dot, neato, twopi, circo, or fdp [default: %(default)s]')
50
+    parser.add_argument(
56 51
         '-n', '--no-filter',
57 52
         action='store_const', const=None, dest='filter',
58 53
         help='assume input is already filtered into xdot format (use e.g. dot -Txdot)')
59
-    parser.add_option(
60
-        '-g', None,
54
+    parser.add_argument(
55
+        '-g', '--geometry',
61 56
         action='store', dest='geometry',
62 57
         help='default window size in form WxH')
63 58
 
64
-    (options, args) = parser.parse_args(sys.argv[1:])
65
-    if len(args) > 1:
66
-        parser.error('incorrect number of arguments')
59
+    options = parser.parse_args()
60
+    inputfile = options.inputfile
67 61
 
68 62
     width = height = 512
69 63
     if options.geometry:
... ...
@@ -75,11 +69,11 @@ Shortcuts:
75 69
     win = DotWindow(width=width, height=height)
76 70
     win.connect('delete-event', Gtk.main_quit)
77 71
     win.set_filter(options.filter)
78
-    if len(args) >= 1:
79
-        if args[0] == '-':
72
+    if inputfile and len(inputfile) >= 1:
73
+        if inputfile == '-':
80 74
             win.set_dotcode(sys.stdin.read())
81 75
         else:
82
-            win.open_file(args[0])
76
+            win.open_file(inputfile)
83 77
     Gtk.main()
84 78
 
85 79
 if __name__ == '__main__':