Does the argparse generate a wrong help message?

jfong at ms4.hinet.net jfong at ms4.hinet.net
Fri Dec 27 23:08:16 EST 2019


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


More information about the Python-list mailing list