diff mercurial/fancyopts.py @ 7772:88887054d277

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. Doc fixes for mq by mpm.
author Augie Fackler <durin42@gmail.com>
date Tue, 10 Feb 2009 13:26:00 -0600
parents d39af2eabb8c
children ec98f35e3e16
line wrap: on
line diff
--- a/mercurial/fancyopts.py	Tue Feb 10 16:31:52 2009 -0600
+++ b/mercurial/fancyopts.py	Tue Feb 10 13:26:00 2009 -0600
@@ -1,6 +1,32 @@
 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.
+    """
+    extraargs = []
+    if '--' in args:
+        stopindex = args.index('--')
+        extraargs = args[stopindex+1:]
+        args = args[:stopindex]
+    opts, parseargs = getopt.getopt(args, options, longoptions)
+    args = []
+    while parseargs:
+        arg = parseargs.pop(0)
+        if arg and arg[0] == '-' and len(arg) > 1:
+            parseargs.insert(0, arg)
+            topts, newparseargs = getopt.getopt(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):
     """
     read args, parse options, and store options in state
 
@@ -52,7 +78,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: