mirror of
https://github.com/spf13/cobra
synced 2024-11-24 14:47:12 +00:00
Make error handling more obvious
Again, the code looks a little more like a middle-schooler's code. But that just makes it easier to understand and maintain.
This commit is contained in:
parent
6f735782e0
commit
0a7a850026
2 changed files with 17 additions and 16 deletions
|
@ -768,7 +768,7 @@ func TestRootNoCommandHelp(t *testing.T) {
|
||||||
|
|
||||||
func TestRootUnknownCommand(t *testing.T) {
|
func TestRootUnknownCommand(t *testing.T) {
|
||||||
r := noRRSetupTest("bogus")
|
r := noRRSetupTest("bogus")
|
||||||
s := "Error: unknown command \"bogus\"\nRun 'cobra-test help' for usage.\n"
|
s := "Error: unknown command \"bogus\"\nRun 'cobra-test --help' for usage.\n"
|
||||||
|
|
||||||
if r.Output != s {
|
if r.Output != s {
|
||||||
t.Errorf("Unexpected response.\nExpecting to be:\n %q\nGot:\n %q\n", s, r.Output)
|
t.Errorf("Unexpected response.\nExpecting to be:\n %q\nGot:\n %q\n", s, r.Output)
|
||||||
|
|
31
command.go
31
command.go
|
@ -273,7 +273,7 @@ Additional help topics:
|
||||||
{{if .HasHelpSubCommands}}{{range .Commands}}{{if and (not .Runnable) (not .Deprecated)}} {{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasRunnableSiblings }}{{range .Parent.Commands}}{{if and (not .Runnable) (not .Deprecated)}}{{if not (eq .Name $cmd.Name) }}
|
{{if .HasHelpSubCommands}}{{range .Commands}}{{if and (not .Runnable) (not .Deprecated)}} {{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasRunnableSiblings }}{{range .Parent.Commands}}{{if and (not .Runnable) (not .Deprecated)}}{{if not (eq .Name $cmd.Name) }}
|
||||||
{{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{end}}
|
{{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{end}}
|
||||||
{{end}}{{ if .HasSubCommands }}
|
{{end}}{{ if .HasSubCommands }}
|
||||||
Use "{{.Root.Name}} help [command]" for more information about a command.
|
Use "{{.CommandPath}} [command] --help" for more information about a command.
|
||||||
{{end}}`
|
{{end}}`
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -452,19 +452,13 @@ func (c *Command) execute(a []string) (err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
err = c.ParseFlags(a)
|
err = c.ParseFlags(a)
|
||||||
if err == flag.ErrHelp {
|
|
||||||
c.Help()
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.Usage()
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// If help is called, regardless of other flags, we print that.
|
// If help is called, regardless of other flags, return we want help
|
||||||
// Print help also if c.Run is nil.
|
// Also say we need help if c.Run is nil.
|
||||||
if c.helpFlagVal || !c.Runnable() {
|
if c.helpFlagVal || !c.Runnable() {
|
||||||
c.Help()
|
return flag.ErrHelp
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
c.preRun()
|
c.preRun()
|
||||||
|
@ -544,13 +538,20 @@ func (c *Command) Execute() (err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd, flags, err := c.Find(args)
|
cmd, flags, err := c.Find(args)
|
||||||
if err == nil {
|
|
||||||
err = cmd.execute(flags)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.Println("Error:", err.Error())
|
c.Println("Error:", err.Error())
|
||||||
c.Printf("Run '%v help' for usage.\n", c.Root().Name())
|
c.Printf("Run '%v --help' for usage.\n", c.CommandPath())
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = cmd.execute(flags)
|
||||||
|
if err != nil {
|
||||||
|
if err == flag.ErrHelp {
|
||||||
|
cmd.Help()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
c.Println(cmd.UsageString())
|
||||||
|
c.Println("Error:", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|
Loading…
Reference in a new issue