Better handling of parsing args to commands, especially the root

This commit is contained in:
spf13 2013-09-29 02:03:29 -04:00
parent 2d02c310f3
commit fb5077acbe

View File

@ -79,10 +79,26 @@ func (c *Commander) Execute() (err error) {
// overriding
c.initHelp()
if len(c.args) == 0 {
err = c.execute(os.Args[1:])
if len(os.Args) == 1 {
// If only the executable is called and the root is runnable, run it
if c.Runnable() {
argWoFlags := c.Flags().Args()
c.Run(c.cmd, argWoFlags)
} else {
c.Usage()
}
} else {
err = c.execute(os.Args[1:])
}
} else {
err = c.execute(c.args)
}
if err != nil {
c.Println("Error:", err.Error())
c.Printf("%v: invalid command %#q\n", c.Root().Name(), os.Args[1:])
c.Printf("Run '%v help' for usage\n", c.Root().Name())
}
return
}