From 45b377d6b1944611871668beff0806c02e556d70 Mon Sep 17 00:00:00 2001 From: Viktor Benei Date: Mon, 4 Jul 2016 21:24:58 +0200 Subject: [PATCH 1/2] use root if no other command matches and root is runnable --- command.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/command.go b/command.go index a8b0fa56..5c8855c6 100644 --- a/command.go +++ b/command.go @@ -436,7 +436,7 @@ func (c *Command) Find(args []string) (*Command, []string, error) { } // root command with subcommands, do subcommand checking - if commandFound == c && len(argsWOflags) > 0 { + if commandFound == c && !c.Runnable() && len(argsWOflags) > 0 { suggestionsString := "" if !c.DisableSuggestions { if c.SuggestionsMinimumDistance <= 0 { @@ -734,7 +734,7 @@ func (c commandSorterByName) Less(i, j int) bool { return c[i].Name() < c[j].Nam // Commands returns a sorted slice of child commands. func (c *Command) Commands() []*Command { // do not sort commands if it already sorted or sorting was disabled - if EnableCommandSorting && !c.commandsAreSorted{ + if EnableCommandSorting && !c.commandsAreSorted { sort.Sort(commandSorterByName(c.commands)) c.commandsAreSorted = true } From 5513220bc3d99d258948b1094b864892f256c249 Mon Sep 17 00:00:00 2001 From: Viktor Benei Date: Mon, 4 Jul 2016 21:49:06 +0200 Subject: [PATCH 2/2] test changes to make it pass --- cobra_test.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/cobra_test.go b/cobra_test.go index 97090b10..a7d1744e 100644 --- a/cobra_test.go +++ b/cobra_test.go @@ -934,6 +934,11 @@ func TestRootSuggestions(t *testing.T) { cmd := initializeWithRootCmd() cmd.AddCommand(cmdTimes) + origCmdRun := cmd.Run + defer func() { + cmd.Run = origCmdRun + }() + cmd.Run = nil tests := map[string]string{ "time": "times", @@ -1024,6 +1029,12 @@ func TestFlagsBeforeCommand(t *testing.T) { func TestRemoveCommand(t *testing.T) { versionUsed = 0 c := initializeWithRootCmd() + origCmdRun := c.Run + defer func() { + c.Run = origCmdRun + }() + c.Run = nil + c.AddCommand(cmdVersion1) c.RemoveCommand(cmdVersion1) x := fullTester(c, "version")