fancyopts: fix handling of "--" value in earlygetopt()
authorYuya Nishihara <yuya@tcha.org>
Sat, 25 Nov 2017 17:30:50 +0900
changeset 35227 98a5aa5575e7
parent 35226 5b569d512fbd
child 35228 04a2820f2fca
fancyopts: fix handling of "--" value in earlygetopt()
mercurial/fancyopts.py
tests/test-dispatch.t
--- a/mercurial/fancyopts.py	Fri Nov 24 01:09:00 2017 +0900
+++ b/mercurial/fancyopts.py	Sat Nov 25 17:30:50 2017 +0900
@@ -119,7 +119,7 @@
     >>> get([b'--cwd=foo', b'x', b'y', b'-R', b'bar', b'--debugger'], gnu=False)
     ([('--cwd', 'foo')], ['x', 'y', '-R', 'bar', '--debugger'])
     >>> get([b'--unknown', b'--cwd=foo', b'--', '--debugger'], gnu=False)
-    ([], ['--unknown', '--cwd=foo', '--debugger'])
+    ([], ['--unknown', '--cwd=foo', '--', '--debugger'])
 
     stripping early options (without loosing '--'):
 
@@ -141,6 +141,13 @@
     >>> get([b'-q', b'--'])
     ([('-q', '')], [])
 
+    '--' may be a value:
+
+    >>> get([b'-R', b'--', b'x'])
+    ([('-R', '--')], ['x'])
+    >>> get([b'--cwd', b'--', b'x'])
+    ([('--cwd', '--')], ['x'])
+
     value passed to bool options:
 
     >>> get([b'--debugger=foo', b'x'])
@@ -163,20 +170,16 @@
     >>> get([b'-', b'y'])
     ([], ['-', 'y'])
     """
-    # ignoring everything just after '--' isn't correct as '--' may be an
-    # option value (e.g. ['-R', '--']), but we do that consistently.
-    try:
-        argcount = args.index('--')
-    except ValueError:
-        argcount = len(args)
-
     parsedopts = []
     parsedargs = []
     pos = 0
-    while pos < argcount:
+    while pos < len(args):
         arg = args[pos]
+        if arg == '--':
+            pos += not keepsep
+            break
         flag, hasval, val, takeval = _earlyoptarg(arg, shortlist, namelist)
-        if not hasval and takeval and pos + 1 >= argcount:
+        if not hasval and takeval and pos + 1 >= len(args):
             # missing last argument
             break
         if not flag or hasval and not takeval:
@@ -195,8 +198,7 @@
             parsedopts.append((flag, args[pos + 1]))
             pos += 2
 
-    parsedargs.extend(args[pos:argcount])
-    parsedargs.extend(args[argcount + (not keepsep):])
+    parsedargs.extend(args[pos:])
     return parsedopts, parsedargs
 
 def fancyopts(args, options, state, gnu=False, early=False, optaliases=None):
--- a/tests/test-dispatch.t	Fri Nov 24 01:09:00 2017 +0900
+++ b/tests/test-dispatch.t	Sat Nov 25 17:30:50 2017 +0900
@@ -40,10 +40,10 @@
 "--" may be an option value:
 
   $ hg -R -- log
-  abort: option -R has to be separated from other options (e.g. not -qR) and --repository may only be abbreviated as --repo!
+  abort: repository -- not found!
   [255]
   $ hg log -R --
-  abort: option -R has to be separated from other options (e.g. not -qR) and --repository may only be abbreviated as --repo!
+  abort: repository -- not found!
   [255]
   $ hg log -T --
   -- (no-eol)