no classification
no tag
no datas
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?
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
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.
name:
Comment content: (supports up to 255 characters)
Copyright © 2018-2021 python black hole network All Rights Reserved All rights reserved, and all rights reserved.京ICP备18063182号-7
For complaints and reports, and advertising cooperation, please contact vgs_info@163.com or QQ3083709327
Disclaimer: All articles on the website are uploaded by users and are only for readers' learning and communication use, and commercial use is prohibited. If the article involves pornography, reactionary, infringement and other illegal information, please report it to us and we will delete it immediately after verification!