#4458: recognize "-" as an argument, not a malformed option in gnu_getopt().

This commit is contained in:
Georg Brandl 2008-12-05 09:23:14 +00:00
parent 8d6c49047f
commit a07435d3e3
3 changed files with 9 additions and 1 deletions

View File

@ -130,7 +130,7 @@ def gnu_getopt(args, shortopts, longopts = []):
if args[0][:2] == '--': if args[0][:2] == '--':
opts, args = do_longs(opts, args[0][2:], longopts, args[1:]) 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:]) opts, args = do_shorts(opts, args[0][1:], shortopts, args[1:])
else: else:
if all_options_first: if all_options_first:

View File

@ -124,6 +124,11 @@ class GetoptTests(unittest.TestCase):
self.assertEqual(opts, [('-a', ''), ('-b', '1'), self.assertEqual(opts, [('-a', ''), ('-b', '1'),
('--alpha', ''), ('--beta', '2')]) ('--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 + # Posix style via +
opts, args = getopt.gnu_getopt(cmdline, '+ab:', ['alpha', 'beta=']) opts, args = getopt.gnu_getopt(cmdline, '+ab:', ['alpha', 'beta='])
self.assertEqual(opts, [('-a', '')]) self.assertEqual(opts, [('-a', '')])

View File

@ -60,6 +60,9 @@ Core and Builtins
Library 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 - Added the subprocess.check_output() convenience function to get output
from a subprocess on success or raise an exception on error. from a subprocess on success or raise an exception on error.