mirror of
https://github.com/spf13/cobra
synced 2025-05-05 12:57:22 +00:00
Merge f411518d3f
into 3c187e9045
This commit is contained in:
commit
cc325c02b5
1 changed files with 22 additions and 1 deletions
23
command.go
23
command.go
|
@ -27,6 +27,14 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type UnknownCommandError struct {
|
||||||
|
commandStr string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *UnknownCommandError) Error() string {
|
||||||
|
return fmt.Sprintf("unknown command %q\nRun 'help' for usage.\n", u.commandStr)
|
||||||
|
}
|
||||||
|
|
||||||
// Command is just that, a command for your application.
|
// Command is just that, a command for your application.
|
||||||
// eg. 'go run' ... 'run' is the command. Cobra requires
|
// eg. 'go run' ... 'run' is the command. Cobra requires
|
||||||
// you to define the usage and description as part of your command
|
// you to define the usage and description as part of your command
|
||||||
|
@ -386,7 +394,7 @@ func (c *Command) Find(arrs []string) (*Command, []string, error) {
|
||||||
|
|
||||||
// If we matched on the root, but we asked for a subcommand, return an error
|
// If we matched on the root, but we asked for a subcommand, return an error
|
||||||
if commandFound.Name() == c.Name() && len(stripFlags(arrs, c)) > 0 && commandFound.Name() != arrs[0] {
|
if commandFound.Name() == c.Name() && len(stripFlags(arrs, c)) > 0 && commandFound.Name() != arrs[0] {
|
||||||
return nil, a, fmt.Errorf("unknown command %q", a[0])
|
return nil, a, &UnknownCommandError{a[0]}
|
||||||
}
|
}
|
||||||
|
|
||||||
return commandFound, a, nil
|
return commandFound, a, nil
|
||||||
|
@ -500,7 +508,20 @@ func (c *Command) Execute() (err error) {
|
||||||
c.Help()
|
c.Help()
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
<<<<<<< HEAD
|
||||||
cmd, flags, e := c.Find(args)
|
cmd, flags, e := c.Find(args)
|
||||||
|
=======
|
||||||
|
err = c.findAndExecute(args)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, isNotFound := err.(*UnknownCommandError)
|
||||||
|
|
||||||
|
// Now handle the case where the root is runnable and only flags are provided
|
||||||
|
if err != nil && isNotFound && c.Runnable() {
|
||||||
|
// This is pretty much a custom version of the *Command.execute method
|
||||||
|
// with a few differences because it's the final command (no fall back)
|
||||||
|
e := c.ParseFlags(args)
|
||||||
|
>>>>>>> Fix a bug where errors were masked.
|
||||||
if e != nil {
|
if e != nil {
|
||||||
err = e
|
err = e
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Add table
Reference in a new issue