]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.14] Improve `getopt.getopt` and `getopt.gnu_getopt` test coverage (GH-153933)...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sat, 18 Jul 2026 22:07:18 +0000 (00:07 +0200)
committerGitHub <noreply@github.com>
Sat, 18 Jul 2026 22:07:18 +0000 (00:07 +0200)
(cherry picked from commit 0fa144bb78042fb91c703678bb378b4dbce3cb95)

Co-authored-by: Florian Freitag <flohacksfriday@gmail.com>
Lib/test/test_getopt.py

index 961efc86fc26670fd8a0f44673560e054fe2a8a8..fac2ca3db81d703d459ed2d36db54fa5876b2fe8 100644 (file)
@@ -149,6 +149,18 @@ class GetoptTests(unittest.TestCase):
                                 ('-a', ''), ('--alpha', '')])
         self.assertEqual(args, ['arg1', 'arg2'])
 
+        # Allow string for single long argument
+        opts, args = getopt.getopt(cmdline, 'a::', 'alpha=?')
+        self.assertEqual(opts, [('-a', '1'), ('--alpha', '2'), ('--alpha', ''),
+                                ('-a', ''), ('--alpha', '')])
+        self.assertEqual(args, ['arg1', 'arg2'])
+
+        # Pass everything after -- as args
+        cmdline = ['-a1', '--alpha=2', '--', '-b', '--beta=5']
+        opts, args = getopt.getopt(cmdline, 'a:b', ['alpha=', 'beta'])
+        self.assertEqual(opts, [('-a', '1'), ('--alpha', '2')])
+        self.assertEqual(args, ['-b', '--beta=5'])
+
         self.assertError(getopt.getopt, cmdline, 'a:b', ['alpha', 'beta'])
 
     def test_gnu_getopt(self):
@@ -191,6 +203,25 @@ class GetoptTests(unittest.TestCase):
         self.assertEqual(args, ['arg1', '-b', '1', '--alpha', '--beta=2',
                                 '--beta', '3', 'arg2'])
 
+        # Allow string for single long argument
+        opts, args = getopt.gnu_getopt(cmdline, 'ab:', 'alpha')
+        self.assertEqual(opts, [('-a', '')])
+        self.assertEqual(args, ['arg1', '-b', '1', '--alpha', '--beta=2',
+                                '--beta', '3', 'arg2'])
+
+        # Pass everything after -- as args
+        cmdline = ['-a1', '--alpha=2', '--', '-b', '--beta=5']
+        opts, args = getopt.gnu_getopt(cmdline, 'a:b', ['alpha=', 'beta'])
+        self.assertEqual(opts, [('-a', '1'), ('--alpha', '2')])
+        self.assertEqual(args, ['-b', '--beta=5'])
+
+        # In order arguments
+        cmdline = ["gamma", "--alpha=3"]
+        opts, args = getopt.gnu_getopt(cmdline, '-', ["alpha="])
+        self.assertEqual(opts, [(None, ['gamma']), ('--alpha', '3')])
+        self.assertEqual(args, [])
+
+
     def test_issue4629(self):
         longopts, shortopts = getopt.getopt(['--help='], '', ['help='])
         self.assertEqual(longopts, [('--help', '')])