[issue36267] User input to argparse raises Index_Error: "-a=" on a 'store_true' action
paul j3
report at bugs.python.org
Mon Mar 11 21:36:10 EDT 2019
New submission from paul j3 <ajipanca at gmail.com>:
Test case:
parser = argparse.ArgumentParser()
parser.add_argument("-a", action="store_true")
args = parser.parse_args("-a=".split())
raises an Index_Error, which is not caught by the argparse error mechanism.
This is an unusual case, the coincidence of several user errors - 'store_true' which shouldn't take an argument and a short optional with a bare =. So it's unlikely to occur. Still, argparse shouldn't ever respond to a command line value with an uncaught error.
The traceback shows that it occurs during the handling of the explicit_arg in consume_optional.
Traceback (most recent call last):
File "argparseRaiseIndexError.py", line 5, in <module>
args = parser.parse_args("-a=".split())
File "/usr/lib/python3.6/argparse.py", line 1743, in parse_args
args, argv = self.parse_known_args(args, namespace)
File "/usr/lib/python3.6/argparse.py", line 1775, in parse_known_args
namespace, args = self._parse_known_args(args, namespace)
File "/usr/lib/python3.6/argparse.py", line 1981, in _parse_known_args
start_index = consume_optional(start_index)
File "/usr/lib/python3.6/argparse.py", line 1881, in consume_optional
option_string = char + explicit_arg[0]
IndexError: string index out of range
The issue was raised in a Stackoverflow question, where I and the poster explore why it occurs, and possible fixes.
https://stackoverflow.com/questions/54662609/python-argparse-indexerror-for-passing-a
----------
components: Library (Lib)
messages: 337709
nosy: paul.j3
priority: normal
severity: normal
stage: needs patch
status: open
title: User input to argparse raises Index_Error: "-a=" on a 'store_true' action
type: crash
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue36267>
_______________________________________
More information about the Python-bugs-list
mailing list