akamensky / argparse

Argparse for golang. Just because `flag` sucks
MIT License
604 stars 62 forks source link

--help flag is being ignored when required arguments are not provided with sub-command #69

Closed akamensky closed 4 years ago

akamensky commented 4 years ago

i.e. progname command --help will yield error message instead of printing entire help message.

Help message should be printed whenever -h|--help is provided regardless whether on top level or on sub-command.

jhughes1153 commented 4 years ago

I'd be interested in helping with this one if possible.

akamensky commented 4 years ago

Feel free to pick this up! I think can start with adding relevant test case for tests.

jhughes1153 commented 4 years ago

Im not getting an error message like I would expect. Is this just referring to the '[--arg] is required' and then the help message being dumped after that or is it something else? Im have trouble reproducing an error with subcommands.

akamensky commented 4 years ago

@jhughes1153 I've put together following example:

package main

import (
    "fmt"
    "github.com/akamensky/argparse"
    "os"
)

func main() {
    p := argparse.NewParser("example", "simple sub-command example")

    subOne := p.NewCommand("sub-one", "Sub-command one")
    subTwo := p.NewCommand("sub-two", "Sub-command two")

    intArgOne := subOne.Int("i", "int", &argparse.Options{
        Required: true,
        Help: "simple integer",
    })

    intArgTwo := subTwo.Int("i", "int", &argparse.Options{
        Required: true,
        Help: "simple integer",
    })

    err := p.Parse(os.Args)
    if err != nil {
        panic(err)
    }

    if subOne.Happened() {
        fmt.Printf("sub-command one arg is %d\n", *intArgOne)
    } else if subTwo.Happened() {
        fmt.Printf("sub-command two arg is %d\n", *intArgTwo)
    }
}

the output running this is as follows:

$ go run main.go --help
usage: example <Command> [-h|--help]

               simple sub-command example

Commands:

  sub-one  Sub-command one
  sub-two  Sub-command two

Arguments:

  -h  --help  Print help information

$ go run main.go sub-one --help
panic: [-i|--int] is required

goroutine 1 [running]:
main.main()
    /tmp/main.go:28 +0x603
exit status 2

As you can see on second run it did not print help message as requested, but instead it returned error that [-i|--int] is required, while i believe it should have printed help message for sub-command. If I change Required: false and add Default: 0 to sub-commands then it works as expected:

$ go run main.go sub-one --help
usage: example sub-one [-i|--int <integer>] [-h|--help]

               Sub-command one

Arguments:

  -i  --int   simple integer. Default: 0
  -h  --help  Print help information
akamensky commented 4 years ago

I believe -h|--help should have higher priority than required:true arguments.

jhughes1153 commented 4 years ago

Thanks for the explanation, I was trying to use the zooprog example and I guess I was just doing something wrong with trying to produce the error.

jhughes1153 commented 4 years ago

So it looks like help isnt caught in the sub commands because the sub commands dont look for the --help so they just panic on the required, so I guess we could either add help as a special flag to all subcommands or change that when it parses the flags and sees help itll call the most recent help function. Ill look into what python argparse does in this case for reference as well.

jhughes1153 commented 4 years ago

It looks like in argparse for python they just add help to each subparser so I think ill head in that direction first

jhughes1153 commented 4 years ago

@akamensky when you disable help on the parser should it disable the help for all subcommands as well?

jhughes1153 commented 4 years ago

Sorry for taking so long with this, I still want to work on this bug but my job has been crazy lately and taking up a lot of time on my weekends, but these next few weekends I should be free to work on it so i'll be able to spend some time on this

akamensky commented 4 years ago

Closed by #72