]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
#4458: recognize "-" as an argument, not a malformed option in gnu_getopt().
authorGeorg Brandl <georg@python.org>
Fri, 5 Dec 2008 09:23:14 +0000 (09:23 +0000)
committerGeorg Brandl <georg@python.org>
Fri, 5 Dec 2008 09:23:14 +0000 (09:23 +0000)
Lib/getopt.py
Lib/test/test_getopt.py
Misc/NEWS

index 04e881ec73d08bfa6b29056bd75155486273c274..7248f08a3206a8899d4a1b8280c0415068c77c10 100644 (file)
@@ -130,7 +130,7 @@ def gnu_getopt(args, shortopts, longopts = []):
 
         if args[0][:2] == '--':
             opts, args = do_longs(opts, args[0][2:], longopts, args[1:])
-        elif args[0][:1] == '-':
+        elif args[0][:1] == '-' and args[0] != '-':
             opts, args = do_shorts(opts, args[0][1:], shortopts, args[1:])
         else:
             if all_options_first:
index 36d1688b03f6425ddb52e69c477d5c3b2cd0479a..aac720d5a63eb343e20118b17f6e51f4e4bedbaa 100644 (file)
@@ -124,6 +124,11 @@ class GetoptTests(unittest.TestCase):
         self.assertEqual(opts, [('-a', ''), ('-b', '1'),
                                 ('--alpha', ''), ('--beta', '2')])
 
+        # recognize "-" as an argument
+        opts, args = getopt.gnu_getopt(['-a', '-', '-b', '-'], 'ab:', [])
+        self.assertEqual(args, ['-'])
+        self.assertEqual(opts, [('-a', ''), ('-b', '-')])
+
         # Posix style via +
         opts, args = getopt.gnu_getopt(cmdline, '+ab:', ['alpha', 'beta='])
         self.assertEqual(opts, [('-a', '')])
index 55e123baa65b9e8eba5153c7885704b8022ab1b1..b1fe2e48e5f5ba7e908acd721c0c0408aed4f9d4 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -60,6 +60,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #4458: getopt.gnu_getopt() now recognizes a single "-" as an argument,
+  not a malformed option.
+
 - Added the subprocess.check_output() convenience function to get output
   from a subprocess on success or raise an exception on error.