]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] gh-53780: Ignore the first "--" (double dash) between an option and command...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Tue, 24 Sep 2024 07:54:32 +0000 (09:54 +0200)
committerGitHub <noreply@github.com>
Tue, 24 Sep 2024 07:54:32 +0000 (07:54 +0000)
(cherry picked from commit c578271366176a1d1b0941897efefb6e4d6508b4)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Lib/argparse.py
Lib/test/test_argparse.py
Misc/NEWS.d/next/Library/2024-09-20-12-23-11.gh-issue-53780.mrV1zi.rst [new file with mode: 0644]

index bedcfb8ea07a6b708bc7e7e89d667105b3e945bc..1b7c54e56bab38576dea23d71e6c0285f113545e 100644 (file)
@@ -2106,11 +2106,15 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
             # and add the Positional and its args to the list
             for action, arg_count in zip(positionals, arg_counts):
                 args = arg_strings[start_index: start_index + arg_count]
-                # Strip out the first '--' if it is not in PARSER or REMAINDER arg.
-                if (action.nargs not in [PARSER, REMAINDER]
-                    and arg_strings_pattern.find('-', start_index,
+                # Strip out the first '--' if it is not in REMAINDER arg.
+                if action.nargs == PARSER:
+                    if arg_strings_pattern[start_index] == '-':
+                        assert args[0] == '--'
+                        args.remove('--')
+                elif action.nargs != REMAINDER:
+                    if (arg_strings_pattern.find('-', start_index,
                                                  start_index + arg_count) >= 0):
-                    args.remove('--')
+                        args.remove('--')
                 start_index += arg_count
                 take_action(action, args)
 
index f908a21c52c51b45bdf68acdcb36f63faf5ae613..37c2238895b08a6cbbeb490d20a19fb0aee8e73b 100644 (file)
@@ -5645,6 +5645,20 @@ class TestDoubleDash(TestCase):
             "invalid choice: '--'",
             parser.parse_args, ['--', 'x', '--', 'run', 'a', 'b'])
 
+    def test_subparser_after_multiple_argument_option(self):
+        parser = argparse.ArgumentParser(exit_on_error=False)
+        parser.add_argument('--foo', nargs='*')
+        subparsers = parser.add_subparsers()
+        parser1 = subparsers.add_parser('run')
+        parser1.add_argument('-f')
+        parser1.add_argument('bar', nargs='*')
+
+        args = parser.parse_args(['--foo', 'x', 'y', '--', 'run', 'a', 'b', '-f', 'c'])
+        self.assertEqual(NS(foo=['x', 'y'], f='c', bar=['a', 'b']), args)
+        self.assertRaisesRegex(argparse.ArgumentError,
+            "invalid choice: '--'",
+            parser.parse_args, ['--foo', 'x', '--', '--', 'run', 'a', 'b'])
+
 
 # ===========================
 # parse_intermixed_args tests
diff --git a/Misc/NEWS.d/next/Library/2024-09-20-12-23-11.gh-issue-53780.mrV1zi.rst b/Misc/NEWS.d/next/Library/2024-09-20-12-23-11.gh-issue-53780.mrV1zi.rst
new file mode 100644 (file)
index 0000000..fb700c7
--- /dev/null
@@ -0,0 +1 @@
+:mod:`argparse` now ignores the first ``"--"`` (double dash) between an option and command.