Skip to content

Commit 8d27e62

Browse files
miss-islingtonshihai1991Rémi LapeyreJelleZijlstrahauntsaninja
authored
gh-80448: argparse: Fix IndexError on store_true action (GH-15656)
(cherry picked from commit e02cc6d) Co-authored-by: Hai Shi <[email protected]> Co-authored-by: Rémi Lapeyre <[email protected]> Co-authored-by: Jelle Zijlstra <[email protected]> Co-authored-by: Shantanu <[email protected]>
1 parent ad5159b commit 8d27e62

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
@@ -1962,7 +1962,11 @@ def consume_optional(start_index):
19621962
# arguments, try to parse more single-dash options out
19631963
# of the tail of the option string
19641964
chars = self.prefix_chars
1965-
if arg_count == 0 and option_string[1] not in chars:
1965+
if (
1966+
arg_count == 0
1967+
and option_string[1] not in chars
1968+
and explicit_arg != ''
1969+
):
19661970
action_tuples.append((action, [], option_string))
19671971
char = option_string[0]
19681972
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
@@ -295,7 +295,7 @@ class TestOptionalsSingleDashCombined(ParserTestCase):
295295
Sig('-z'),
296296
]
297297
failures = ['a', '--foo', '-xa', '-x --foo', '-x -z', '-z -x',
298-
'-yx', '-yz a', '-yyyx', '-yyyza', '-xyza']
298+
'-yx', '-yz a', '-yyyx', '-yyyza', '-xyza', '-x=']
299299
successes = [
300300
('', NS(x=False, yyy=None, z=None)),
301301
('-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)