mercurial/fancyopts.py
changeset 7772 88887054d277
parent 5878 d39af2eabb8c
child 8230 ec98f35e3e16
--- 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: