Python argparse AssertionError when using mutually exclusive group

Amanjit :

Here's my simple test.py script:

import argparse

parser = argparse.ArgumentParser('A long string that goes on and on and on'
                                 'and on and on and on and on and on and on '
                                 'and on and on and on and on and on and on '
                                 'and on and on and on and on and on and on ')
me_group = parser.add_mutually_exclusive_group()
me_group.add_argument('-f', help=argparse.SUPPRESS)
me_group.add_argument('-o', help=argparse.SUPPRESS)
parser.add_argument('-t', help='c')
parser.parse_args()

When I run the following:

python test.py --help

I get this AssertionError:

...
  File "/usr/lib/python2.7/argparse.py", line 332, in _format_usage
    assert ' '.join(opt_parts) == opt_usage
AssertionError

This seems to only happen when I suppress all of the arguments in a mutually-exclusive group. If one or more is not suppressed, then everything works fine. If I print out the two sides of the comparison:

print ' '.join(opt_parts)
print opt_usage

I get the following:

[-h] [-t T]
[-h]  [-t T]

Looks like there's an extra space there. Any idea why this would be? Is there anything I am doing incorrectly?

Alex :

This is a known issue when suppressing args. It is only reached when the usage line is long enough that it needs to be wrapped. See 22363 and 17890

You can avoid this by moving the mutually exclusive group to the end of the arguments:

import argparse

parser = argparse.ArgumentParser('A long string that goes on and on and on'
                                 'and on and on and on and on and on and on '
                                 'and on and on and on and on and on and on '
                                 'and on and on and on and on and on and on ')
parser.add_argument('-t', help='c')
me_group = parser.add_mutually_exclusive_group()
me_group.add_argument('-f', help=argparse.SUPPRESS)
me_group.add_argument('-o', help=argparse.SUPPRESS)
parser.parse_args()

results in:

python test.py -h
usage: A long string that goes on and on and onand on and on and on and on and on and on and on and on and on and on and on and on and on and on and on and on and on and on
       [-h] [-t T]

optional arguments:
  -h, --help  show this help message and exit
  -t T        c

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=27725&siteId=1