forked from mirror/cobra
Better handling of parsing args to commands, especially the root
This commit is contained in:
parent
2d02c310f3
commit
fb5077acbe
16
commander.go
16
commander.go
|
@ -79,10 +79,26 @@ func (c *Commander) Execute() (err error) {
|
|||
// overriding
|
||||
c.initHelp()
|
||||
if len(c.args) == 0 {
|
||||
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
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue