Does the argparse generate a wrong help message?

jfong at ms4.hinet.net jfong at ms4.hinet.net
Wed Jan 1 19:54:40 EST 2020


Random832於 2020年1月2日星期四 UTC+8上午5時04分36秒寫道:
> On Fri, Dec 27, 2019, at 23:08, jfong at ms4.hinet.net wrote:
> > > > 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']
> >
> > So the square bracket means optional, not list? My misunderstanding:-(
> 
> To be clear, the brackets in the print output do mean a list, and it is because nargs=1 produces a list rather than the default (no nargs) which is to produce a single value. This is not normally visible to the end user (it's just a difference in how your program consumes the one mandatory argument), so it's not reflected in the help output. Square brackets in the help output mean an optional argument.

Yes, I have noticed my misunderstanding. I thought [FOO] in "--foo [FOO]" is a list until Chris had reminded me:-) Thank you for clarifying it in detail.

--Jach


More information about the Python-list mailing list