[meld] Remove optparse Option subclass in favour of callback processing



commit dd7e9d640c5db1119c6dde9e4d9fd7eb0626c5e2
Author: Kai Willadsen <kai willadsen gmail com>
Date:   Wed May 20 09:39:44 2009 +1000

    Remove optparse Option subclass in favour of callback processing
    
    The optparse.Option subclass allows for more flexibility than we need,
    so this patch moves to using the simpler callback processing mechanism,
    and move this code into MeldApp with the rest of the optparse code.

 meld/meldapp.py |   32 +++++++++++++++++++++++++++++---
 meld/misc.py    |   52 ----------------------------------------------------
 2 files changed, 29 insertions(+), 55 deletions(-)
---
diff --git a/meld/meldapp.py b/meld/meldapp.py
index 35b23f2..925f225 100644
--- a/meld/meldapp.py
+++ b/meld/meldapp.py
@@ -847,9 +847,35 @@ class MeldApp(gnomeglade.Component):
                   (usage_3dirs, _("Start with 2 or 3 way directory comparison"))]
         return "\n" + "\n".join( ["%prog " + pad_args_fmt % u for u in usages] )
 
+    def diff_files_callback(self, option, opt_str, value, parser):
+        """Gather arguments after option in a list and append to option.dest."""
+        assert value is None
+        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]
+
+        if len(diff_files_args) not in (1, 2, 3):
+            raise optparse.OptionValueError(
+                "wrong number of arguments supplied to --diff")
+
+        value = getattr(parser.values, option.dest) or []
+        value.append(diff_files_args)
+        setattr(parser.values, option.dest, value)
+
     def parse_args(self, rawargs):
         parser = optparse.OptionParser(
-            option_class=misc.MeldOption,
             usage=self.usage_msg(),
             description=_("Meld is a file and directory comparison tool."),
             version="%prog " + version)
@@ -861,8 +887,8 @@ class MeldApp(gnomeglade.Component):
         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=[],
+        parser.add_option("", "--diff", action="callback", callback=self.diff_files_callback,
+                          dest="diff", default=[],
                           help=_("Creates a diff tab for up to 3 supplied files or directories."))
         options, args = parser.parse_args(rawargs)
         for files in options.diff:
diff --git a/meld/misc.py b/meld/misc.py
index 6f92d42..a2cf71d 100644
--- a/meld/misc.py
+++ b/meld/misc.py
@@ -359,55 +359,3 @@ class ListItem(object):
     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
-    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]