From 5c7afce5b80250e8f0ebdca4c5eeb5b3556216f8 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Wed, 22 Jun 2016 09:34:24 -0400 Subject: [PATCH 1/3] Add help command before category scan/sort --- app.go | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/app.go b/app.go index a17300d..a046c01 100644 --- a/app.go +++ b/app.go @@ -140,13 +140,6 @@ func (a *App) Setup() { } a.Commands = newCmds - a.categories = CommandCategories{} - for _, command := range a.Commands { - a.categories = a.categories.AddCommand(command.Category, command) - } - sort.Sort(a.categories) - - // append help to commands if a.Command(helpCommand.Name) == nil && !a.HideHelp { a.Commands = append(a.Commands, helpCommand) if (HelpFlag != BoolFlag{}) { @@ -154,7 +147,6 @@ func (a *App) Setup() { } } - //append version/help flags if a.EnableBashCompletion { a.appendFlag(BashCompletionFlag) } @@ -162,6 +154,12 @@ func (a *App) Setup() { if !a.HideVersion { a.appendFlag(VersionFlag) } + + a.categories = CommandCategories{} + for _, command := range a.Commands { + a.categories = a.categories.AddCommand(command.Category, command) + } + sort.Sort(a.categories) } // Run is the entry point to the cli app. Parses the arguments slice and routes From be875c395c0b00ecc198c002a78c5d0bc79f297e Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Wed, 22 Jun 2016 09:41:42 -0400 Subject: [PATCH 2/3] Add test to verify help command usage --- help_test.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/help_test.go b/help_test.go index a664865..7c15400 100644 --- a/help_test.go +++ b/help_test.go @@ -141,6 +141,23 @@ func Test_helpCommand_Action_ErrorIfNoTopic(t *testing.T) { } } +func Test_helpCommand_InHelpOutput(t *testing.T) { + app := NewApp() + output := &bytes.Buffer{} + app.Writer = output + app.Run([]string{"test", "--help"}) + + s := output.String() + + if strings.Contains(s, "\nCOMMANDS:\nGLOBAL OPTIONS:\n") { + t.Fatalf("empty COMMANDS section detected: %q", s) + } + + if !strings.Contains(s, "help, h") { + t.Fatalf("missing \"help, h\": %q", s) + } +} + func Test_helpSubcommand_Action_ErrorIfNoTopic(t *testing.T) { app := NewApp() From 3c5afd4757061c696af6d82b3376e45a69e3cff8 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Wed, 22 Jun 2016 09:48:39 -0400 Subject: [PATCH 3/3] Hide help command for category tests --- app_test.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app_test.go b/app_test.go index ee8cc35..9c6b960 100644 --- a/app_test.go +++ b/app_test.go @@ -1125,6 +1125,7 @@ func TestApp_Run_Version(t *testing.T) { func TestApp_Run_Categories(t *testing.T) { app := NewApp() app.Name = "categories" + app.HideHelp = true app.Commands = []Command{ { Name: "command1", @@ -1174,6 +1175,7 @@ func TestApp_Run_Categories(t *testing.T) { func TestApp_VisibleCategories(t *testing.T) { app := NewApp() app.Name = "visible-categories" + app.HideHelp = true app.Commands = []Command{ { Name: "command1", @@ -1213,6 +1215,7 @@ func TestApp_VisibleCategories(t *testing.T) { app = NewApp() app.Name = "visible-categories" + app.HideHelp = true app.Commands = []Command{ { Name: "command1", @@ -1247,6 +1250,7 @@ func TestApp_VisibleCategories(t *testing.T) { app = NewApp() app.Name = "visible-categories" + app.HideHelp = true app.Commands = []Command{ { Name: "command1",