Skip to content

Commit

Permalink
pythongh-80448: argparse: Fix IndexError on store_true action (python…
Browse files Browse the repository at this point in the history
…#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>
  • Loading branch information
4 people committed Nov 12, 2022
1 parent 7f3a4b9 commit e02cc6d
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 2 deletions.
6 changes: 5 additions & 1 deletion Lib/argparse.py
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_argparse.py
Expand Up @@ -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)),
Expand Down
@@ -0,0 +1 @@
Fix IndexError in :class:`argparse.ArgumentParser` when a ``store_true`` action is given an explicit argument.

0 comments on commit e02cc6d

Please sign in to comment.