From d0997e8f99b5a3006872e0294ef7434a0e01c93a Mon Sep 17 00:00:00 2001 From: Soulou Date: Fri, 21 Aug 2015 13:25:37 +0200 Subject: [PATCH] Set Categories as a read-only method and fix tests --- app.go | 15 ++++++++++----- app_test.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ help.go | 3 +-- 3 files changed, 55 insertions(+), 7 deletions(-) diff --git a/app.go b/app.go index 5ebf201..4bc333b 100644 --- a/app.go +++ b/app.go @@ -37,8 +37,8 @@ type App struct { HideVersion bool // Display commands by category CategorizedHelp bool - // Populate when displaying AppHelp - Categories CommandCategories + // Populate on app startup, only gettable throught method Categories() + categories CommandCategories // An action to execute when the bash-completion flag is set BashComplete func(context *Context) // An action to execute before any subcommands are run, but after the context is ready @@ -101,12 +101,12 @@ func (a *App) Run(arguments []string) (err error) { } if a.CategorizedHelp { - a.Categories = CommandCategories{} + a.categories = CommandCategories{} for _, command := range a.Commands { - a.Categories = a.Categories.AddCommand(command.Category, command) + a.categories = a.categories.AddCommand(command.Category, command) } } - sort.Sort(a.Categories) + sort.Sort(a.categories) newCmds := []Command{} for _, c := range a.Commands { @@ -329,6 +329,11 @@ func (a *App) Command(name string) *Command { return nil } +// Returnes the array containing all the categories with the commands they contain +func (a *App) Categories() CommandCategories { + return a.categories +} + func (a *App) hasFlag(flag Flag) bool { for _, f := range a.Flags { if flag == f { diff --git a/app_test.go b/app_test.go index 7feaf1f..95bdd41 100644 --- a/app_test.go +++ b/app_test.go @@ -8,6 +8,7 @@ import ( "io" "io/ioutil" "os" + "reflect" "strings" "testing" ) @@ -939,6 +940,49 @@ func TestApp_Run_Version(t *testing.T) { } } +func TestApp_Run_Categories(t *testing.T) { + app := NewApp() + app.Name = "categories" + app.CategorizedHelp = true + app.Commands = []Command{ + Command{ + Name: "command1", + Category: "1", + }, + Command{ + Name: "command2", + Category: "1", + }, + Command{ + Name: "command3", + Category: "2", + }, + } + buf := new(bytes.Buffer) + app.Writer = buf + + app.Run([]string{"categories"}) + + expect := CommandCategories{ + &CommandCategory{ + Name: "1", + Commands: []Command{ + app.Commands[0], + app.Commands[1], + }, + }, + &CommandCategory{ + Name: "2", + Commands: []Command{ + app.Commands[2], + }, + }, + } + if !reflect.DeepEqual(app.Categories(), expect) { + t.Fatalf("expected categories %#v, to equal %#v", app.Categories(), expect) + } +} + func TestApp_Run_DoesNotOverwriteErrorFromBefore(t *testing.T) { app := NewApp() app.Action = func(c *Context) {} diff --git a/help.go b/help.go index 7838c89..6708192 100644 --- a/help.go +++ b/help.go @@ -48,8 +48,7 @@ USAGE: {{if .Category}}CATEGORY: {{.Category}} -{{end}} -DESCRIPTION: +{{end}}DESCRIPTION: {{.Description}}{{end}}{{if .Flags}} OPTIONS: