changeset 14265:e4ab5ae193f2

add positional arguments to non-shell aliases
author Alexander Solovyov <alexander@solovyov.net>
date Sun, 01 May 2011 12:29:32 +0200
parents 8e00906066c9
children 6332c02b3d68
files mercurial/dispatch.py tests/test-alias.t
diffstat 2 files changed, 22 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/dispatch.py	Sat May 07 21:01:15 2011 +0300
+++ b/mercurial/dispatch.py	Sun May 01 12:29:32 2011 +0200
@@ -182,10 +182,21 @@
 
     return -1
 
-def aliasargs(fn):
-    if hasattr(fn, 'args'):
-        return fn.args
-    return []
+def aliasargs(fn, givenargs):
+    args = getattr(fn, 'args', [])
+    if args and givenargs:
+        cmd = ' '.join(map(util.shellquote, args))
+
+        nums = []
+        def replacer(m):
+            num = int(m.group(1)) - 1
+            nums.append(num)
+            return givenargs[num]
+        cmd = re.sub(r'\$(\d+|\$)', replacer, cmd)
+        givenargs = [x for i, x in enumerate(givenargs)
+                     if i not in nums]
+        args = shlex.split(cmd)
+    return args + givenargs
 
 class cmdalias(object):
     def __init__(self, name, definition, cmdtable):
@@ -263,7 +274,7 @@
             else:
                 self.fn, self.opts = tableentry
 
-            self.args = aliasargs(self.fn) + args
+            self.args = aliasargs(self.fn, args)
             if cmd not in commands.norepo.split(' '):
                 self.norepo = False
             if self.help.startswith("hg " + cmd):
@@ -330,7 +341,7 @@
         aliases, entry = cmdutil.findcmd(cmd, commands.table,
                                      ui.config("ui", "strict"))
         cmd = aliases[0]
-        args = aliasargs(entry[0]) + args
+        args = aliasargs(entry[0], args)
         defaults = ui.config("defaults", cmd)
         if defaults:
             args = map(util.expandpath, shlex.split(defaults)) + args
--- a/tests/test-alias.t	Sat May 07 21:01:15 2011 +0300
+++ b/tests/test-alias.t	Sun May 01 12:29:32 2011 +0200
@@ -17,6 +17,7 @@
   > mylog = log
   > lognull = log -r null
   > shortlog = log --template '{rev} {node|short} | {date|isodate}\n'
+  > positional = log --template '{\$2} {\$1} | {date|isodate}\n'
   > dln = lognull --debug
   > nousage = rollback
   > put = export -r 0 -o "\$FOO/%R.diff"
@@ -127,6 +128,10 @@
   $ hg shortlog
   0 e63c23eaa88a | 1970-01-01 00:00 +0000
 
+positional arguments
+
+  $ hg positional 'node|short' rev
+  0 e63c23eaa88a | 1970-01-01 00:00 +0000
 
 interaction with defaults