Issue 33415: When add_mutually_exclusive_group is built without argument, the help breaks with "IndexError: list index out of range" (original) (raw)
Hello,
When add_mutually_exclusive_group is built without argument, the help breaks with "IndexError: list index out of range". Indeed this snippet:
actions_ = parser.add_argument_group('Actions') actions = actions_.add_mutually_exclusive_group()
Breaks the help of argparse:
--help
Traceback (most recent call last): File "./ovpauto", line 135, in args = parser.parse_args() File "/usr/lib/python2.7/argparse.py", line 1701, in parse_args args, argv = self.parse_known_args(args, namespace) File "/usr/lib/python2.7/argparse.py", line 1733, in parse_known_args namespace, args = self._parse_known_args(args, namespace) File "/usr/lib/python2.7/argparse.py", line 1939, in _parse_known_args start_index = consume_optional(start_index) File "/usr/lib/python2.7/argparse.py", line 1879, in consume_optional take_action(action, args, option_string) File "/usr/lib/python2.7/argparse.py", line 1807, in take_action action(self, namespace, argument_values, option_string) File "/usr/lib/python2.7/argparse.py", line 996, in call parser.print_help() File "/usr/lib/python2.7/argparse.py", line 2340, in print_help self._print_message(self.format_help(), file) File "/usr/lib/python2.7/argparse.py", line 2314, in format_help return formatter.format_help() File "/usr/lib/python2.7/argparse.py", line 281, in format_help help = self._root_section.format_help() File "/usr/lib/python2.7/argparse.py", line 211, in format_help func(*args) File "/usr/lib/python2.7/argparse.py", line 319, in _format_usage action_usage = format(optionals + positionals, groups) File "/usr/lib/python2.7/argparse.py", line 390, in _format_actions_usage start = actions.index(group._group_actions[0]) IndexError: list index out of range
Moreover, the trace does not say whence the error came from.
uname -a
Linux quasar 4.8.0-2-amd64 #1 SMP Debian 4.8.15-2 (2017-01-04) x86_64 GNU/Linux
lsb_release -a
No LSB modules are available. Distributor ID: Debian Description: Debian GNU/Linux testing (buster) Release: testing Codename: buster
gcc --version
gcc (Debian 7.3.0-17) 7.3.0 Copyright (C) 2017 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
python --version
Python 2.7.15rc1
dpkg -l | grep -P '\s+python\s+.*2.7.15'
ii python 2.7.15~rc1-1 amd64 interactive high-level object-oriented language (default version)
Regards, Ariel
The usage formatter is brittle, especially the part that adds mutually exclusive markings to the normal usage string. I don't think I've seen this error before, but I'm not surprised.
A real fix requires a rewrite of the usage formatter, which I've suggested in an other bug/issue. It would be a good idea to include this as a test case for such a patch.
The simplest immediate fix is to just not use a mutually exclusive group when it isn't needed. I don't think it's urgent enough to require a special patch.
start = actions.index(group._group_actions[0])
group is the mutually exclusive group that's being formatted. _group_actions is is list of Actions. The developer expected that the group would be populated (normally by 2 or more Actions), and didn't think it necessary to first check if it was empty. That's a reasonable assumption.