[PATCH] fancyopts: Parse options that occur after arguments
Augie Fackler
durin42 at gmail.com
Tue Feb 10 13:30:36 CST 2009
# HG changeset patch
# User Augie Fackler <durin42 at gmail.com>
# Date 1234293960 21600
# Node ID 6db88544fd8f0a6bde344e7e93bdcb382155612d
# Parent b034161948959e36879bf96f29553b5a274e58a2
fancyopts: Parse options that occur after arguments.
This changes the behavior of qguard in the case of setting negative guards, as -- will now always be required.
Fixes issue1402.
diff --git a/mercurial/dispatch.py b/mercurial/dispatch.py
--- a/mercurial/dispatch.py
+++ b/mercurial/dispatch.py
@@ -184,7 +184,7 @@
c.append((o[0], o[1], options[o[1]], o[3]))
try:
- args = fancyopts.fancyopts(args, c, cmdoptions)
+ args = fancyopts.fancyopts(args, c, cmdoptions, True)
except fancyopts.getopt.GetoptError, inst:
raise error.ParseError(cmd, inst)
diff --git a/mercurial/fancyopts.py b/mercurial/fancyopts.py
--- a/mercurial/fancyopts.py
+++ b/mercurial/fancyopts.py
@@ -1,6 +1,28 @@
import getopt
-def fancyopts(args, options, state):
+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.
+ """
+ opts, parseargs = getopt.getopt(args, options, longoptions)
+ args = []
+ while parseargs:
+ arg = parseargs.pop(0)
+ if arg == '--':
+ args.extend(parseargs)
+ parseargs = []
+ elif arg[0] == '-' and len(arg) > 1:
+ parseargs.insert(0, arg)
+ topts, parseargs = getopt.getopt(parseargs, options, longoptions)
+ opts = opts + topts
+ else:
+ args.append(arg)
+ return opts, args
+
+
+def fancyopts(args, options, state, gnu=False):
"""
read args, parse options, and store options in state
@@ -52,7 +74,11 @@
namelist.append(oname)
# parse arguments
- opts, args = getopt.getopt(args, shortlist, namelist)
+ if gnu:
+ parse = gnugetopt
+ else:
+ parse = getopt.getopt
+ opts, args = parse(args, shortlist, namelist)
# transfer result to state
for opt, val in opts:
diff --git a/tests/test-mq-guards b/tests/test-mq-guards
--- a/tests/test-mq-guards
+++ b/tests/test-mq-guards
@@ -53,7 +53,7 @@
echo % should push a.patch
hg qpush
-hg qguard c.patch -a
+hg qguard -- c.patch -a
echo % should print -a
hg qguard c.patch
@@ -95,7 +95,7 @@
hg qpush
hg qpop -a
-hg qguard a.patch +1 +2 -3
+hg qguard -- a.patch +1 +2 -3
hg qselect 1 2 3
echo % list patches and guards
hg qguard -l
More information about the Mercurial-devel
mailing list