News from this site

 Rental advertising space, please contact the webmaster if you need cooperation


+focus
focused

classification  

no classification

tag  

no tag

date  

no datas

Python argparse AssertionError when using mutually exclusive group

posted on 2023-11-12 14:30     read(358)     comment(0)     like(15)     collect(4)


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?


solution


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


Category of website: technical article > Q&A

Author:qs

link:http://www.pythonblackhole.com/blog/article/245163/f7c669890bdf43e13358/

source:python black hole net

Please indicate the source for any form of reprinting. If any infringement is discovered, it will be held legally responsible.

15 0
collect article
collected

Comment content: (supports up to 255 characters)