gh-80448: argparse: Fix IndexError on store_true action (GH-15656)

(cherry picked from commit e02cc6d42a)

Co-authored-by: Hai Shi <shihai1992@gmail.com>
Co-authored-by: Rémi Lapeyre <remi.lapeyre@henki.fr>
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
This commit is contained in:
Miss Islington (bot) 2022-11-11 18:30:38 -08:00 committed by GitHub
parent d75b2e2fc8
commit 92b531b858
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 7 additions and 2 deletions

View File

@ -1997,7 +1997,11 @@ def consume_optional(start_index):
# arguments, try to parse more single-dash options out
# of the tail of the option string
chars = self.prefix_chars
if arg_count == 0 and option_string[1] not in chars:
if (
arg_count == 0
and option_string[1] not in chars
and explicit_arg != ''
):
action_tuples.append((action, [], option_string))
char = option_string[0]
option_string = char + explicit_arg[0]

View File

@ -296,7 +296,7 @@ class TestOptionalsSingleDashCombined(ParserTestCase):
Sig('-z'),
]
failures = ['a', '--foo', '-xa', '-x --foo', '-x -z', '-z -x',
'-yx', '-yz a', '-yyyx', '-yyyza', '-xyza']
'-yx', '-yz a', '-yyyx', '-yyyza', '-xyza', '-x=']
successes = [
('', NS(x=False, yyy=None, z=None)),
('-x', NS(x=True, yyy=None, z=None)),

View File

@ -0,0 +1 @@
Fix IndexError in :class:`argparse.ArgumentParser` when a ``store_true`` action is given an explicit argument.