argparse icon indicating copy to clipboard operation
argparse copied to clipboard

support "h" custom argument

Open jossef opened this issue 4 years ago • 3 comments

If I define

host := parser.String("h", "host", &argparse.Options{Required: false, Help: "listening host", Default: "127.0.0.1"})
// ...

I'll get a panic ->

panic: unable to add String: short name h occurs more than once

This is because there's a built-in -h/--help already occupied it.

For now I can workaround it by using a different letter, but It would be a nice to have support for h 😁

jossef avatar Oct 15 '20 11:10 jossef

That would be great. I realized that this was not a very good design choice. However this was already implemented this way.

I think this can be achieved without breaking compatibility for existing code by adding a method to re-define help argument (or perhaps entirely unset it depending on method arguments). Then this method would need to be called anywhere before setting new -h argument.

PR for this change is welcome.

akamensky avatar Oct 15 '20 13:10 akamensky

@jossef FWIW, there is DisableHelp() method that removes use of -h|--help as automagic help arguments. This was added previously for a similar use, but I have not used it myself, so not sure if that works for your use case.

akamensky avatar Oct 17 '20 16:10 akamensky

With SetHelp from #50, we can remove the short name for help with:

parser.SetHelp("", "help")
host := parser.String("h", "host", &argparse.Options{Required: true, Help: "Host"})

sayap avatar Dec 14 '22 09:12 sayap