Rearrange libbe.ui.command_line.CmdOptionParser._add_option() for Python 2.5
authorW. Trevor King <wking@drexel.edu>
Sun, 13 Dec 2009 11:33:50 +0000 (06:33 -0500)
committerW. Trevor King <wking@drexel.edu>
Sun, 13 Dec 2009 11:33:50 +0000 (06:33 -0500)
Python 2.6 doesn't mind, but 2.5 doesn't like kwargs after a *
expansion:

  $ ./be list
  Traceback (most recent call last):
    File "./be", line 5, in <module>
      import libbe.ui.command_line
    File "/home/wking/src/fun/be/be.restructure/libbe/ui/command_line.py", line 63
      *opt_strings, action='callback', dest=dest,
                         ^
  SyntaxError: invalid syntax

libbe/ui/command_line.py

index b0b52affcaf464b57baf33e677526f17cfe960da..60741f542b82e86b5748b58fe1b135eb414665c3 100755 (executable)
@@ -52,27 +52,28 @@ class CmdOptionParser(optparse.OptionParser):
     def _add_option(self, option):
         option.validate()
         self._option_by_name[option.name] = option
-        opt_strings = ['--'+option.name]
-        dest = option.name.replace('-', '_')
+        long_opt = '--%s' % option.name
+        if option.short_name != None:
+            short_opt = '-%s' % option.short_name
         assert '_' not in option.name, \
             'Non-reconstructable option name %s' % option.name
-        if option.short_name != None:
-            opt_strings.append('-'+option.short_name)
+        kwargs = {'dest':option.name.replace('-', '_'),
+                  'help':option.help}
         if option.arg == None: # a callback option
-            opt = optparse.Option(
-                *opt_strings, action='callback', dest=dest,
-                callback=self.callback, help=option.help)
+            kwargs['action'] = 'callback'
+            kwargs['callback'] = self.callback
         else:
-            kwargs = {}
             if option.arg.type == 'bool':
-                action = 'store_true'
+                kwargs['action'] = 'store_true'
             else:
                 kwargs['type'] = option.arg.type
-                action = 'store'
-            opt = optparse.Option(
-                *opt_strings, metavar=option.arg.metavar,
-                 default=option.arg.default, action=action,
-                 dest=dest, help=option.help, **kwargs)
+                kwargs['action'] = 'store'
+            kwargs['metavar'] = option.arg.metavar
+            kwargs['default'] = option.arg.default
+        if option.short_name != None:
+            opt = optparse.Option(short_opt, long_opt, **kwargs)
+        else:
+            opt = optparse.Option(long_opt, **kwargs)
         opt._option = option
         self.add_option(opt)