# HG changeset patch # User Yuya Nishihara # Date 2017-11-25 08:30:50 # Node ID 98a5aa5575e73c7b13507ed8210c5ce654b688ec # Parent 5b569d512fbd7422de3f53f3a8a4acfd70ab2a30 fancyopts: fix handling of "--" value in earlygetopt() 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)