Does the argparse generate a wrong help message?

Richard Damon Richard at Damon-Family.org
Fri Dec 27 23:18:28 EST 2019


On 12/27/19 11:08 PM, jfong at ms4.hinet.net wrote:
> Chris Angelico於 2019年12月28日星期六 UTC+8上午11時30分47秒寫道:
>> On Sat, Dec 28, 2019 at 2:26 PM <jfong at ms4.hinet.net> wrote:
>>> The codes in test.py are:
>>> ---------
>>> import argparse
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--foo', nargs='?', help='foo help')
>>> parser.add_argument('--goo', nargs=1, help='goo help')
>>> args = parser.parse_args()
>>> print(args.foo, args.goo)
>>> ---------
>>>
>>> But I get the following result:
>>> ---------
>>> D:\Works\Python>py test.py -h
>>> usage: test.py [-h] [--foo [FOO]] [--goo GOO]
>>>
>>> optional arguments:
>>>    -h, --help   show this help message and exit
>>>    --foo [FOO]  foo help
>>>    --goo GOO    goo help
>>>
>>> D:\Works\Python>py test.py --foo 1 --goo 2
>>> 1 ['2']
>>> ---------
>>>
>>> It seems to me that the help message should be:
>>> usage: test.py [-h] [--foo FOO] [--goo [GOO]]
>>>
>>> Do I had missed something?
>>>
>> Can you elaborate on why you expect this? You've declared one of them
>> to have a single mandatory argument, and the other a single optional
>> argument. This corresponds to what I'm seeing.
>>
>> ChrisA
> So the square bracket means optional, not list? My misunderstanding:-(
>
> --Jach

Yes, the normal notation is [x] indicates that x is optionial

-- 
Richard Damon



More information about the Python-list mailing list