8000 gh-80448: argparse: Fix IndexError on store_true action (#15656) · python/cpython@e02cc6d · GitHub
[go: up one dir, main page]

Skip to content

Commit e02cc6d

Browse files
shihai1991Rémi LapeyreJelleZijlstrahauntsaninja
authored
gh-80448: argparse: Fix IndexError on store_true action (#15656)
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>
1 parent 7f3a4b9 commit e02cc6d

File tree

3 files changed

+7
-2
lines changed

3 files changed

+7
-2
lines changed

Lib/argparse.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1997,7 +1997,11 @@ def consume_optional(start_index):
19971997
# arguments, try to parse more single-dash options out
19981998
# of the tail of the option string
19991999
chars = self.prefix_chars
2000-
if arg_count == 0 and option_string[1] not in chars:
2000+
if (
2001+
arg_count == 0
2002+
and option_string[1] not in chars
2003+
and explicit_arg != ''
2004+
):
20012005
action_tuples.append((action, [], option_string))
20022006
char = option_string[0]
20032007
option_string = char + explicit_arg[0]

Lib/test/test_argparse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ class TestOptionalsSingleDashCombined(ParserTestCase):
296296
Sig('-z'),
297297
]
298298
failures = ['a', '--foo', '-xa', '-x --foo', '-x -z', '-z -x',
299-
'-yx', '-yz a', '-yyyx', '-yyyza', '-xyza']
299+
'-yx', '-yz a', '-yyyx', '-yyyza', '-xyza', '-x=']
300300
successes = [
301301
('', NS(x=False, yyy=None, z=None)),
302302
('-x', NS(x=True, yyy=None, z=None)),
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix IndexError in :class:`argparse.ArgumentParser` when a ``store_true`` action is given an explicit argument.

0 commit comments

Comments
 (0)
0