cmdutil.parse moves to dispatch._parse; use fancyopts bugfix
authorChristian Ebert <blacktrash@gmx.net>
Thu, 16 Aug 2007 14:19:33 +0200
changeset 227 8ae160f24b9f
parent 226 5255b00fbba5
child 228 88362a9b5afd
cmdutil.parse moves to dispatch._parse; use fancyopts bugfix Copy fancyopts from 88803a69b24a as _fancyopts to make argument parsing reliable.
hgkw/keyword.py
--- a/hgkw/keyword.py	Tue Aug 14 16:26:22 2007 +0200
+++ b/hgkw/keyword.py	Thu Aug 16 14:19:33 2007 +0200
@@ -80,10 +80,12 @@
         Or, better, use bundle/unbundle to share changes.
 '''
 
-from mercurial import commands, cmdutil, context, fancyopts
-from mercurial import filelog, localrepo, templater, util, hg
+from mercurial import commands, cmdutil, context, fancyopts, filelog
+from mercurial import localrepo, templater, util, hg
 from mercurial.i18n import gettext as _
-import re, shutil, sys, tempfile, time
+import getopt, re, shutil, sys, tempfile, time
+
+# backwards compatibility hacks
 
 # findcmd, bail_if_changed were in commands until 0c61124ad877
 try:
@@ -93,6 +95,47 @@
     findcmd = commands.findcmd
     bail_if_changed = commands.bail_if_changed
 
+# cmdutil.parse moves to dispatch._parse in 18a9fbb5cd78
+try:
+    from mercurial import dispatch
+except ImportError:
+    pass
+
+# commands.parse/cmdutil.parse returned nothing for
+# "hg diff --rev" before 88803a69b24a due to bug in fancyopts
+def _fancyopts(args, options, state):
+    '''Fixed fancyopts from 88803a69b24a.'''
+    long = []
+    short = ''
+    map = {}
+    dt = {}
+    for s, l, d, c in options:
+        pl = l.replace('-', '_')
+        map['-'+s] = map['--'+l] = pl
+        if isinstance(d, list):
+            state[pl] = d[:]
+        else:
+            state[pl] = d
+        dt[pl] = type(d)
+        if (d is not None and d is not True and d is not False and
+            not callable(d)):
+            if s: s += ':'
+            if l: l += '='
+        if s: short = short + s
+        if l: long.append(l)
+    opts, args = getopt.getopt(args, short, long)
+    for opt, arg in opts:
+        if dt[map[opt]] is type(fancyopts): state[map[opt]](state, map[opt], arg)
+        elif dt[map[opt]] is type(1): state[map[opt]] = int(arg)
+        elif dt[map[opt]] is type(''): state[map[opt]] = arg
+        elif dt[map[opt]] is type([]): state[map[opt]].append(arg)
+        elif dt[map[opt]] is type(None): state[map[opt]] = True
+        elif dt[map[opt]] is type(False): state[map[opt]] = True
+    return args
+
+fancyopts.fancyopts = _fancyopts
+
+
 commands.optionalrepo += ' kwdemo'
 
 def utcdate(date):
@@ -397,14 +440,13 @@
                     'remove', 'rename', 'rollback']
 
     def _getcmd():
-        # commands or cmdutil.parse(ui, sys.argv[1:])[0]
-        # is broken before 88803a69b24a (fancyopts)
-        # results in no output from "hg diff --rev" with older hg versions
-        args = fancyopts.fancyopts(sys.argv[1:], commands.globalopts, {})
-        if args:
-            cmd = args[0]
-            aliases, i = findcmd(ui, cmd)
-            return aliases[0]
+        try:
+            return dispatch._parse(ui, sys.argv[1:])[0]
+        except (ImportError, NameError):
+            try:
+                return cmdutil.parse(ui, sys.argv[1:])[0]
+            except AttributeError:
+                return commands.parse(ui, sys.argv[1:])[0]
 
     if not repo.local() or _getcmd() in nokwcommands:
         return