meld r1062 - trunk



Author: stevek
Date: Thu Oct  2 20:09:32 2008
New Revision: 1062
URL: http://svn.gnome.org/viewvc/meld?rev=1062&view=rev

Log:
Bug 522521 â Supporting list of file pairs to compare on command line (Brian Holmes)

Modified:
   trunk/meldapp.py
   trunk/misc.py

Modified: trunk/meldapp.py
==============================================================================
--- trunk/meldapp.py	(original)
+++ trunk/meldapp.py	Thu Oct  2 20:09:32 2008
@@ -903,6 +903,7 @@
     sys.stdout = Unbuffered(sys.stdout)
 
     parser = optparse.OptionParser(
+    option_class=misc.MeldOption,
     usage="""
     %prog                       Start with no windows open
     %prog <dir>                 Start with VC browser in 'dir'
@@ -917,6 +918,9 @@
     parser.add_option("-c", "--context", action="store_true", help=_("Ignored for compatibility"))
     parser.add_option("-e", "--ed", action="store_true", help=_("Ignored for compatibility"))
     parser.add_option("-r", "--recursive", action="store_true", help=_("Ignored for compatibility"))
+    parser.add_option("", "--diff", action="diff_files", dest='diff',
+                      default=[],
+                      help=_("Creates a diff tab for up to 3 supplied files."))
     options, args = parser.parse_args()
 
     icon_theme = gtk.icon_theme_get_default()
@@ -924,6 +928,11 @@
     app = MeldApp()
     tab = None
 
+    for files in options.diff:
+        if len(files) not in (1, 2, 3):
+            app.usage(_("Invalid number of arguments supplied for --diff."))
+        app.append_diff(files)
+
     if len(args) == 0:
         pass
 

Modified: trunk/misc.py
==============================================================================
--- trunk/misc.py	(original)
+++ trunk/misc.py	Thu Oct  2 20:09:32 2008
@@ -321,3 +321,57 @@
         self.value = " ".join(a)
     def __str__(self):
         return "<%s %s %i %s>" % ( self.__class__, self.name, self.active, self.value )
+
+
+################################################################################
+#
+# optparse Options subclass
+#
+################################################################################
+import optparse
+
+def check_diff_files(option, opt, value):
+    if len(value) not in (1, 2, 3):
+        raise optparse.OptionValueError(
+            "option %s: invalid value: %r" % (opt, value))
+
+def diff_files_callback(option, opt_str, value, parser):
+    """Gather arguments after option in a list and append to option.dest."""
+    assert value is None
+    done = 0
+    diff_files_args = []
+    rargs = parser.rargs
+    while rargs:
+        arg = rargs[0]
+
+        # Stop if we hit an arg like "--foo", "-a", "-fx", "--file=f",
+        # etc.  Note that this also stops on "-3" or "-3.0", so if
+        # your option takes numeric values, you will need to handle
+        # this.
+        if ((arg[:2] == "--" and len(arg) > 2) or
+            (arg[:1] == "-" and len(arg) > 1 and arg[1] != "-")):
+            break
+        else:
+            diff_files_args.append(arg)
+            del rargs[0]
+
+    value = getattr(parser.values, option.dest) or []
+    value.append(diff_files_args)
+    setattr(parser.values, option.dest, value)
+
+
+class MeldOption(optparse.Option):
+    """Custom Option which adds the 'diff_files' action."""
+    TYPES = optparse.Option.TYPES + ("diff_files",)
+    TYPE_CHECKER = copy.copy(optparse.Option.TYPE_CHECKER)
+    TYPE_CHECKER["diff_files"] = check_diff_files
+
+    ACTIONS = optparse.Option.ACTIONS + ("diff_files",)
+    TYPED_ACTIONS = optparse.Option.TYPED_ACTIONS + ("diff_files",)
+
+    def take_action(self, action, dest, opt, value, values, parser):
+        if action == "diff_files":
+            diff_files_callback(self, opt, value, parser)
+        else:
+            optparse.Option.take_action(
+                self, action, dest, opt, value, values, parser)



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]