fancyopts: use getopt.gnu_getopt()
The issue described in the docstring has been fixed since Python 20ab2260dc93,
which is in 2.7.
https://hg.python.org/cpython/rev/20ab2260dc93
https://bugs.python.org/issue4458
This fixes the handling of '--' value.
--- a/mercurial/fancyopts.py Thu Nov 23 23:18:56 2017 +0900
+++ b/mercurial/fancyopts.py Fri Nov 24 01:09:00 2017 +0900
@@ -199,33 +199,6 @@
parsedargs.extend(args[argcount + (not keepsep):])
return parsedopts, parsedargs
-def gnugetopt(args, options, longoptions):
- """Parse options mostly like getopt.gnu_getopt.
-
- This is different from getopt.gnu_getopt in that an argument of - will
- become an argument of - instead of vanishing completely.
- """
- extraargs = []
- if '--' in args:
- stopindex = args.index('--')
- extraargs = args[stopindex + 1:]
- args = args[:stopindex]
- opts, parseargs = pycompat.getoptb(args, options, longoptions)
- args = []
- while parseargs:
- arg = parseargs.pop(0)
- if arg and arg[0:1] == '-' and len(arg) > 1:
- parseargs.insert(0, arg)
- topts, newparseargs = pycompat.getoptb(parseargs,\
- options, longoptions)
- opts = opts + topts
- parseargs = newparseargs
- else:
- args.append(arg)
- args.extend(extraargs)
- return opts, args
-
-
def fancyopts(args, options, state, gnu=False, early=False, optaliases=None):
"""
read args, parse options, and store options in state
@@ -312,7 +285,7 @@
if early:
parse = functools.partial(earlygetopt, gnu=gnu)
elif gnu:
- parse = gnugetopt
+ parse = pycompat.gnugetoptb
else:
parse = pycompat.getoptb
opts, args = parse(args, shortlist, namelist)
--- a/mercurial/pycompat.py Thu Nov 23 23:18:56 2017 +0900
+++ b/mercurial/pycompat.py Fri Nov 24 01:09:00 2017 +0900
@@ -214,7 +214,7 @@
def open(name, mode='r', buffering=-1):
return builtins.open(name, sysstr(mode), buffering)
- def getoptb(args, shortlist, namelist):
+ def _getoptbwrapper(orig, args, shortlist, namelist):
"""
Takes bytes arguments, converts them to unicode, pass them to
getopt.getopt(), convert the returned values back to bytes and then
@@ -224,7 +224,7 @@
args = [a.decode('latin-1') for a in args]
shortlist = shortlist.decode('latin-1')
namelist = [a.decode('latin-1') for a in namelist]
- opts, args = getopt.getopt(args, shortlist, namelist)
+ opts, args = orig(args, shortlist, namelist)
opts = [(a[0].encode('latin-1'), a[1].encode('latin-1'))
for a in opts]
args = [a.encode('latin-1') for a in args]
@@ -291,8 +291,8 @@
def getdoc(obj):
return getattr(obj, '__doc__', None)
- def getoptb(args, shortlist, namelist):
- return getopt.getopt(args, shortlist, namelist)
+ def _getoptbwrapper(orig, args, shortlist, namelist):
+ return orig(args, shortlist, namelist)
strkwargs = identity
byteskwargs = identity
@@ -320,3 +320,9 @@
isdarwin = sysplatform == 'darwin'
isposix = osname == 'posix'
iswindows = osname == 'nt'
+
+def getoptb(args, shortlist, namelist):
+ return _getoptbwrapper(getopt.getopt, args, shortlist, namelist)
+
+def gnugetoptb(args, shortlist, namelist):
+ return _getoptbwrapper(getopt.gnu_getopt, args, shortlist, namelist)
--- a/tests/test-dispatch.t Thu Nov 23 23:18:56 2017 +0900
+++ b/tests/test-dispatch.t Fri Nov 24 01:09:00 2017 +0900
@@ -37,10 +37,17 @@
hg log [OPTION]... [FILE]
(use 'hg log -h' to show more help)
- $ hg log -R -- 2>&1 | grep 'hg log'
- hg log: option -R requires argument
- hg log [OPTION]... [FILE]
- (use 'hg log -h' to show more help)
+"--" 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!
+ [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!
+ [255]
+ $ hg log -T --
+ -- (no-eol)
+ $ hg log -T -- -k nomatch
Parsing of early options should stop at "--":