changeset 4733:0ecfc3b3f072

Simplified earlygetopt and made it remove parsed options and values. The order of aliases is no longer important, too.
author Thomas Arendsen Hein <thomas@intevation.de>
date Thu, 28 Jun 2007 15:56:25 +0200
parents b0520e3903fe
children 9ac493f12901
files mercurial/cmdutil.py
diffstat 1 files changed, 16 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/cmdutil.py	Wed Jun 27 20:37:33 2007 +0200
+++ b/mercurial/cmdutil.py	Thu Jun 28 15:56:25 2007 +0200
@@ -248,42 +248,32 @@
     return parsed
 
 def earlygetopt(aliases, args):
-    """Return list of values for a option (with aliases) in given order
+    """Return list of values for an option (or aliases).
 
-    Short option aliases have to occur before long aliases, e.g.:
-    earlygetopt(["-R", "--repository", "--repo"], args)
-    (this is not checked!)
+    The values are listed in the order they appear in args.
+    The options and values are removed from args.
     """
     try:
         argcount = args.index("--")
     except ValueError:
         argcount = len(args)
+    shortopts = [opt for opt in aliases if len(opt) == 2]
     values = []
     pos = 0
     while pos < argcount:
-        valuepos = argcount
-        for opt in aliases:
-            # short option can have no following space, e.g. hg log -Rfoo:
-            if len(opt) == 2:
-                i = argcount
-                while i > 0:
-                    i -= 1
-                    arg = args[i]
-                    if len(arg) > 2 and arg.startswith(opt):
-                        # split -Rfoo -> -R foo
-                        args[i:i+1] = [opt, arg[2:]]
-                        argcount += 1
-            # find next occurance of current alias
-            try:
-                candidate = args.index(opt, pos, argcount) + 1
+        if args[pos] in aliases:
+            if pos + 1 >= argcount:
                 # ignore and let getopt report an error if there is no value
-                if candidate < valuepos:
-                    valuepos = candidate
-            except ValueError:
-                pass
-        if valuepos < argcount:
-            values.append(args[valuepos])
-        pos = valuepos
+                break
+            del args[pos]
+            values.append(args.pop(pos))
+            argcount -= 2
+        elif args[pos][:2] in shortopts:
+            # short option can have no following space, e.g. hg log -Rfoo
+            values.append(args.pop(pos)[2:])
+            argcount -= 1
+        else:
+            pos += 1
     return values
 
 def dispatch(ui, args, argv0=None):