New submission from paul j3 <[email protected]>:
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 <[email protected]>
<https://bugs.python.org/issue36267>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com