forked from mirror/cobra
Better error message
Calling `cobra-test echo times one two turkey` where `one` and `two` are valid arguments but `turkey` is not now results in. Error: invalid argument "turkey" for "cobra-test echo times" Run 'cobra-test echo times --help' for usage.
This commit is contained in:
parent
0a7a850026
commit
9a9d01c9ec
|
@ -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\" for \"cobra-test\"\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)
|
||||||
|
|
|
@ -422,7 +422,7 @@ func (c *Command) Find(args []string) (*Command, []string, error) {
|
||||||
}
|
}
|
||||||
// root command with subcommands, do subcommand checking
|
// root command with subcommands, do subcommand checking
|
||||||
if commandFound == c && len(argsWOflags) > 0 {
|
if commandFound == c && len(argsWOflags) > 0 {
|
||||||
return nil, a, fmt.Errorf("unknown command %q", argsWOflags[0])
|
return commandFound, a, fmt.Errorf("unknown command %q for %q", argsWOflags[0], commandFound.CommandPath())
|
||||||
}
|
}
|
||||||
|
|
||||||
return commandFound, a, nil
|
return commandFound, a, nil
|
||||||
|
@ -539,6 +539,10 @@ func (c *Command) Execute() (err error) {
|
||||||
|
|
||||||
cmd, flags, err := c.Find(args)
|
cmd, flags, err := c.Find(args)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
// If found parse to a subcommand and then failed, talk about the subcommand
|
||||||
|
if cmd != nil {
|
||||||
|
c = cmd
|
||||||
|
}
|
||||||
c.Println("Error:", err.Error())
|
c.Println("Error:", err.Error())
|
||||||
c.Printf("Run '%v --help' for usage.\n", c.CommandPath())
|
c.Printf("Run '%v --help' for usage.\n", c.CommandPath())
|
||||||
return err
|
return err
|
||||||
|
|
Loading…
Reference in New Issue