Message337709
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 |
|
Date |
User |
Action |
Args |
2019-03-12 01:36:10 | paul.j3 | set | recipients:
+ paul.j3 |
2019-03-12 01:36:10 | paul.j3 | set | messageid: <1552354570.63.0.477016922618.issue36267@roundup.psfhosted.org> |
2019-03-12 01:36:10 | paul.j3 | link | issue36267 messages |
2019-03-12 01:36:10 | paul.j3 | create | |
|