Title: When add_mutually_exclusive_group is built without argument, the help breaks with "IndexError: list index out of range"
Type: behavior Stage: resolved
Components: Versions: Python 2.7
Status: closed Resolution: duplicate
Dependencies: Superseder:
Assigned To: Nosy List: ariel-anieli, bethard, paul.j3
Priority: normal Keywords:

Created on 2018-05-03 08:26 by ariel-anieli, last changed 2018-09-23 03:20 by paul.j3. This issue is now closed.

Messages (3)
msg316113 - (view) Author: Ariel Otilibili Anieli (ariel-anieli) Date: 2018-05-03 08:26

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 <module>
    args = parser.parse_args()
  File "/usr/lib/python2.7/", line 1701, in parse_args
    args, argv = self.parse_known_args(args, namespace)
  File "/usr/lib/python2.7/", line 1733, in parse_known_args
    namespace, args = self._parse_known_args(args, namespace)
  File "/usr/lib/python2.7/", line 1939, in _parse_known_args
    start_index = consume_optional(start_index)
  File "/usr/lib/python2.7/", line 1879, in consume_optional
    take_action(action, args, option_string)
  File "/usr/lib/python2.7/", line 1807, in take_action
    action(self, namespace, argument_values, option_string)
  File "/usr/lib/python2.7/", line 996, in __call__
  File "/usr/lib/python2.7/", line 2340, in print_help
    self._print_message(self.format_help(), file)
  File "/usr/lib/python2.7/", line 2314, in format_help
    return formatter.format_help()
  File "/usr/lib/python2.7/", line 281, in format_help
    help = self._root_section.format_help()
  File "/usr/lib/python2.7/", line 211, in format_help
  File "/usr/lib/python2.7/", line 319, in _format_usage
    action_usage = format(optionals + positionals, groups)
  File "/usr/lib/python2.7/", 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

# 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)

msg316189 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2018-05-05 02:22
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.
msg326128 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2018-09-23 03:20
This is duplicate of - argparse help formatter crashes

The same issue - a mutually exclusive group without arguments.

It also deals with issue of nesting groups.
Date User Action Args
2018-09-23 03:20:08paul.j3setstatus: open -> closed
resolution: duplicate
messages: + msg326128

stage: resolved
2018-05-05 02:22:30paul.j3setnosy: + paul.j3
messages: + msg316189
2018-05-03 08:26:26ariel-anielicreate