argparse

Ian Kelly ian.g.kelly at gmail.com
Fri Mar 11 18:40:56 EST 2016


On Fri, Mar 11, 2016 at 4:18 PM, Fillmore <fillmore_remove at hotmail.com> wrote:
>
> Playing with ArgumentParser. I can't find a way to override the -h and
> --help options so that it provides my custom help message.
>
>   -h, --help         show this help message and exit
>
> Here is what I am trying:
>
> parser = argparse.ArgumentParser("csresolver.py",add_help=False)
> parser.add_argument("-h","--help",
>                     help="USAGE: <STDIN> | myscript.py [-exf Exception
> File]")
> parser.add_argument("-e","--ext", type=str,
>                     help="Exception file")
> args = parser.parse_args()
>
>
> The result:
>
> $ ./myscript.py -h
> usage: myscript.py [-h HELP] [-e EXT]
> csresolver.py: error: argument -h/--help: expected one argument
>
> am I missing something obvious?

Larry gave you the right way to do this. However I wanted to also
point out that the reason you're getting an error is because you
didn't specify an action or an nargs for the argument. The default
action is store, and the default nargs for that action is 1, so
argparse is expecting to find one argument after your -h.



More information about the Python-list mailing list