diff --git a/Lib/getopt.py b/Lib/getopt.py --- a/Lib/getopt.py +++ b/Lib/getopt.py @@ -82,9 +82,9 @@ opts = [] if type(longopts) == type(""): - longopts = [longopts] + longopts = [longopts.strip()] else: - longopts = list(longopts) + longopts = [opt.strip() for opt in longopts] while args and args[0].startswith('-') and args[0] != '-': if args[0] == '--': args = args[1:] @@ -114,9 +114,9 @@ opts = [] prog_args = [] if isinstance(longopts, str): - longopts = [longopts] + longopts = [longopts.strip()] else: - longopts = list(longopts) + longopts = [opt.strip() for opt in longopts] # Allow options after non-option arguments? if shortopts.startswith('+'): diff --git a/Lib/test/test_getopt.py b/Lib/test/test_getopt.py --- a/Lib/test/test_getopt.py +++ b/Lib/test/test_getopt.py @@ -180,5 +180,17 @@ self.assertEqual(longopts, [('--help', 'x')]) self.assertRaises(getopt.GetoptError, getopt.getopt, ['--help='], '', ['help']) + def test_ignore_spaces(self): + longopts = [' spam', 'eggs ', ' cheese '] + args = ['--spam', '--eggs', '--cheese'] + expected = [('--cheese', ''), ('--eggs', ''), ('--spam', '')] + for func in (getopt.getopt, getopt.gnu_getopt): + with self.subTest(func=func.__name__): + options, arguments = func(args, '', longopts) + options.sort() + self.assertEqual(options, expected) + self.assertEqual(arguments, []) + + if __name__ == "__main__": unittest.main()