https://github.com/python/cpython/commit/c578271366176a1d1b0941897efefb6e4d6508b4
commit: c578271366176a1d1b0941897efefb6e4d6508b4
branch: main
author: Serhiy Storchaka <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2024-09-24T07:35:28Z
summary:

gh-53780: Ignore the first "--" (double dash) between an option and command in 
argparse (GH-124275)

files:
A Misc/NEWS.d/next/Library/2024-09-20-12-23-11.gh-issue-53780.mrV1zi.rst
M Lib/argparse.py
M Lib/test/test_argparse.py

diff --git a/Lib/argparse.py b/Lib/argparse.py
index 7f6b31bf028cfb..89496cbe454e09 100644
--- a/Lib/argparse.py
+++ b/Lib/argparse.py
@@ -2068,11 +2068,15 @@ def consume_positionals(start_index):
             # 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
                 if args and action.deprecated and action.dest not in warned:
                     self._warning(_("argument '%(argument_name)s' is 
deprecated") %
diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py
index 2d8905e6599954..3d43fc82d20614 100644
--- a/Lib/test/test_argparse.py
+++ b/Lib/test/test_argparse.py
@@ -5984,6 +5984,20 @@ def test_subparser(self):
             "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
index 00000000000000..fb700c722c8a8b
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-09-20-12-23-11.gh-issue-53780.mrV1zi.rst
@@ -0,0 +1 @@
+:mod:`argparse` now ignores the first ``"--"`` (double dash) between an option 
and command.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to