Make earlygetopt return a list of all option values, use the last value.
authorThomas Arendsen Hein <thomas@intevation.de>
Mon, 25 Jun 2007 22:08:10 +0200
changeset 4716 36d23de02da1
parent 4715 ad45209a7c7a
child 4717 97369f6a6bb6
Make earlygetopt return a list of all option values, use the last value. This fixes: "hg -R" showing a useful error instead of traceback "hg -R foo --repository bar" using bar instead of foo And provides a way for other users of earlygetopt to accept more than one value.
mercurial/cmdutil.py
--- a/mercurial/cmdutil.py	Sun Jun 24 11:17:49 2007 +0200
+++ b/mercurial/cmdutil.py	Mon Jun 25 22:08:10 2007 +0200
@@ -248,12 +248,28 @@
     return parsed
 
 def earlygetopt(aliases, args):
-    if "--" in args:
-        args = args[:args.index("--")]
-    for opt in aliases:
-        if opt in args:
-            return args[args.index(opt) + 1]
-    return None
+    """Return list of values for a option (with aliases) in given order"""
+    try:
+        argcount = args.index("--")
+    except ValueError:
+        argcount = len(args)
+    values = []
+    pos = 0
+    while pos < argcount:
+        valuepos = argcount
+        for opt in aliases:
+            # find next occurance of current alias
+            try:
+                candidate = args.index(opt, pos, argcount) + 1
+                # 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
+    return values
 
 def dispatch(ui, args, argv0=None):
     # remember how to call 'hg' before changing the working dir
@@ -262,7 +278,7 @@
     # check for cwd first
     cwd = earlygetopt(['--cwd'], args)
     if cwd:
-        os.chdir(cwd)
+        os.chdir(cwd[-1])
 
     # read the local repository .hgrc into a local ui object
     path = findrepo() or ""
@@ -278,7 +294,7 @@
     # now we can expand paths, even ones in .hg/hgrc
     rpath = earlygetopt(["-R", "--repository", "--repo"], args)
     if rpath:
-        path = lui.expandpath(rpath)
+        path = lui.expandpath(rpath[-1])
         lui = commands.ui.ui(parentui=ui)
         lui.readconfig(os.path.join(path, ".hg", "hgrc"))