alexflint / go-arg

Struct-based argument parsing in Go
https://pkg.go.dev/github.com/alexflint/go-arg
BSD 2-Clause "Simplified" License
2.04k stars 100 forks source link

Why --help and -h flags don't work while using arg.Parse(&args) #251

Closed uynilo9 closed 5 months ago

uynilo9 commented 5 months ago

As the title, arg.Parse(&args) always return ErrHelp after getting --help and -h flags. Is this what the program is supposed to do? And how to make it work (print help interface after getting --help and -h flags) by using arg.Parse(&args) rather than arg.MustParse(&args)?

Thanks for helping <3

alexflint commented 5 months ago

You should do something like

p, err := arg.NewParser(&args)
if err != nil {
  log.Fatal(err)
}

err := p.Parse(os.Args[1:])
if err == arg.ErrHelp {
    p.WriteHelp(os.Stdout)
}