Patchwork [5,of,5] fancyopts: fix handling of "--" value in earlygetopt()

login
register
mail settings
Submitter Yuya Nishihara
Date Dec. 2, 2017, 7 a.m.
Message ID <a0242100667f22448b33.1512198043@mimosa>
Download mbox | patch
Permalink /patch/25895/
State Accepted
Headers show

Comments

Yuya Nishihara - Dec. 2, 2017, 7 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1511598650 -32400
#      Sat Nov 25 17:30:50 2017 +0900
# Node ID a0242100667f22448b33009299c468ce3431297b
# Parent  8dd510eb51942ddae4dc69423627a349656d25f0
fancyopts: fix handling of "--" value in earlygetopt()
Augie Fackler - Dec. 4, 2017, 10:51 p.m.
On Sat, Dec 02, 2017 at 04:00:43PM +0900, Yuya Nishihara wrote:
> # HG changeset patch
> # User Yuya Nishihara <yuya@tcha.org>
> # Date 1511598650 -32400
> #      Sat Nov 25 17:30:50 2017 +0900
> # Node ID a0242100667f22448b33009299c468ce3431297b
> # Parent  8dd510eb51942ddae4dc69423627a349656d25f0
> fancyopts: fix handling of "--" value in earlygetopt()

queued, thanks

Patch

diff --git a/mercurial/fancyopts.py b/mercurial/fancyopts.py
--- a/mercurial/fancyopts.py
+++ b/mercurial/fancyopts.py
@@ -119,7 +119,7 @@  def earlygetopt(args, shortlist, namelis
     >>> 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 @@  def earlygetopt(args, shortlist, namelis
     >>> 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 @@  def earlygetopt(args, shortlist, namelis
     >>> 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 @@  def earlygetopt(args, shortlist, namelis
             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):
diff --git a/tests/test-dispatch.t b/tests/test-dispatch.t
--- a/tests/test-dispatch.t
+++ b/tests/test-dispatch.t
@@ -40,10 +40,10 @@  Missing parameter for early option:
 "--" 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)