Does the argparse generate a wrong help message?

Random832 random832 at fastmail.com
Wed Jan 1 16:04:00 EST 2020


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.


More information about the Python-list mailing list