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

(cherry picked from commit c578271366)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
Miss Islington (bot) 2024-10-08 00:51:07 +02:00 committed by GitHub
parent a380dc6836
commit db3ccd8b62
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 23 additions and 4 deletions

View file

@ -6106,6 +6106,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