akamensky / argparse

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

[BUG] v1.4.0 args does not happened when subcommand exists #115

Open naughtyGitCat opened 2 years ago

naughtyGitCat commented 2 years ago
var cmdRootParser = argparse.NewParser("crank", "cli")
var debugFlag = cmdRootParser.Flag("", "debug", &argparse.Options{Default: false})
var versionFlag = cmdRootParser.Flag("", "version", &argparse.Options{})

var updateCmd = cmdRootParser.NewCommand("update", "update self")
var showCmd = cmdRootParser.NewCommand("show", "show some thing")
var listCmd = cmdRootParser.NewCommand("list", "list some thing")
var eventCmd = cmdRootParser.NewCommand("event", "do with event")

func main() {
    var err = cmdRootParser.Parse(os.Args)
    if err != nil {
        fmt.Println(cmdRootParser.Usage(err))
        return
    }

    if *debugFlag {
        log.SetLevel(log.DebugLevel)
    } else {
        log.SetLevel(log.InfoLevel)
    }

    if *versionFlag {
        log.Infof("current version: %s\n", Version)
        log.Infof("git tag: %s\n", GitTag)
        log.Infof("build time: %s\n", BuildTime)
        log.Infof("go version: %s\n", GoVersion)
        log.Infof("commit hash: %s\n", CommitHash)
        return
    }
.....

raises

 ./crank --version
[sub]Command required
usage: crank <Command> [-h|--help] [--debug] [--version]

             sensor cli

Commands:

  update  update self
  show    show some thing
  list    list some thing
  event   do with event

Arguments:

  -h  --help     Print help information
      --debug
      --version

v1.3.1 will not occur this circumstance v1.4.0 will occur this circumstance

when fmt.Println(os.Args) it turnout [./crank --version]