diff --git a/app.go b/app.go index 4bc333b..dcab379 100644 --- a/app.go +++ b/app.go @@ -35,8 +35,6 @@ type App struct { HideHelp bool // Boolean to hide built-in version flag HideVersion bool - // Display commands by category - CategorizedHelp bool // Populate on app startup, only gettable throught method Categories() categories CommandCategories // An action to execute when the bash-completion flag is set @@ -100,14 +98,6 @@ func (a *App) Run(arguments []string) (err error) { a.Authors = append(a.Authors, Author{Name: a.Author, Email: a.Email}) } - if a.CategorizedHelp { - a.categories = CommandCategories{} - for _, command := range a.Commands { - a.categories = a.categories.AddCommand(command.Category, command) - } - } - sort.Sort(a.categories) - newCmds := []Command{} for _, c := range a.Commands { if c.HelpName == "" { @@ -117,6 +107,12 @@ func (a *App) Run(arguments []string) (err error) { } 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) diff --git a/app_test.go b/app_test.go index 95bdd41..ebf26c7 100644 --- a/app_test.go +++ b/app_test.go @@ -943,7 +943,6 @@ 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", @@ -981,6 +980,13 @@ func TestApp_Run_Categories(t *testing.T) { if !reflect.DeepEqual(app.Categories(), expect) { t.Fatalf("expected categories %#v, to equal %#v", app.Categories(), expect) } + + output := buf.String() + t.Logf("output: %q\n", buf.Bytes()) + + if !strings.Contains(output, "1:\n command1") { + t.Errorf("want buffer to include category %q, did not: \n%q", "1:\n command1", output) + } } func TestApp_Run_DoesNotOverwriteErrorFromBefore(t *testing.T) { diff --git a/command.go b/command.go index 024ddbc..1a05b54 100644 --- a/command.go +++ b/command.go @@ -3,6 +3,7 @@ package cli import ( "fmt" "io/ioutil" + "sort" "strings" ) @@ -231,6 +232,13 @@ func (c Command) startApp(ctx *Context) error { app.Email = ctx.App.Email app.Writer = ctx.App.Writer + app.categories = CommandCategories{} + for _, command := range c.Subcommands { + app.categories = app.categories.AddCommand(command.Category, command) + } + + sort.Sort(app.categories) + // bash completion app.EnableBashCompletion = ctx.App.EnableBashCompletion if c.BashComplete != nil { diff --git a/help.go b/help.go index 6708192..6efb011 100644 --- a/help.go +++ b/help.go @@ -23,11 +23,9 @@ VERSION: AUTHOR(S): {{range .Authors}}{{ . }}{{end}} {{end}}{{if .Commands}} -COMMANDS:{{if .CategorizedHelp}}{{range .Categories}}{{if .Name}} +COMMANDS:{{range .Categories}}{{if .Name}} {{.Name}}{{ ":" }}{{end}}{{range .Commands}} {{.Name}}{{with .ShortName}}, {{.}}{{end}}{{ "\t" }}{{.Usage}}{{end}} -{{end}}{{else}}{{range .Commands}} - {{.Name}}{{with .ShortName}}, {{.}}{{end}}{{ "\t" }}{{.Usage}}{{end}} {{end}}{{end}}{{if .Flags}} GLOBAL OPTIONS: {{range .Flags}}{{.}} @@ -44,11 +42,12 @@ var CommandHelpTemplate = `NAME: {{.HelpName}} - {{.Usage}} USAGE: - {{.HelpName}}{{if .Flags}} [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}{{if .Description}} + {{.HelpName}}{{if .Flags}} [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}{{if .Category}} -{{if .Category}}CATEGORY: - {{.Category}} -{{end}}DESCRIPTION: +CATEGORY: + {{.Category}}{{end}}{{if .Description}} + +DESCRIPTION: {{.Description}}{{end}}{{if .Flags}} OPTIONS: @@ -65,9 +64,10 @@ var SubcommandHelpTemplate = `NAME: USAGE: {{.HelpName}} command{{if .Flags}} [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}} -COMMANDS: - {{range .Commands}}{{join .Names ", "}}{{ "\t" }}{{.Usage}} - {{end}}{{if .Flags}} +COMMANDS:{{range .Categories}}{{if .Name}} + {{.Name}}{{ ":" }}{{end}}{{range .Commands}} + {{.Name}}{{with .ShortName}}, {{.}}{{end}}{{ "\t" }}{{.Usage}}{{end}} +{{end}}{{if .Flags}} OPTIONS: {{range .Flags}}{{.}} {{end}}{{end}}