Remove CategorizedHelp from App and allow subcommands to have categories

Just place all subcommands in categories, the default category will be
"" which will properly format the output (and group commands that have
no category).

Also allow subcommands to have categories.

Lastly, augment the test to check the output.
This commit is contained in:
Jesse Szwedko 2016-03-20 12:17:13 -07:00
parent d0997e8f99
commit 042842b819
4 changed files with 31 additions and 21 deletions

16
app.go
View File

@ -35,8 +35,6 @@ type App struct {
HideHelp bool HideHelp bool
// Boolean to hide built-in version flag // Boolean to hide built-in version flag
HideVersion bool HideVersion bool
// Display commands by category
CategorizedHelp bool
// Populate on app startup, only gettable throught method Categories() // Populate on app startup, only gettable throught method Categories()
categories CommandCategories categories CommandCategories
// An action to execute when the bash-completion flag is set // 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}) 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{} newCmds := []Command{}
for _, c := range a.Commands { for _, c := range a.Commands {
if c.HelpName == "" { if c.HelpName == "" {
@ -117,6 +107,12 @@ func (a *App) Run(arguments []string) (err error) {
} }
a.Commands = newCmds 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 // append help to commands
if a.Command(helpCommand.Name) == nil && !a.HideHelp { if a.Command(helpCommand.Name) == nil && !a.HideHelp {
a.Commands = append(a.Commands, helpCommand) a.Commands = append(a.Commands, helpCommand)

View File

@ -943,7 +943,6 @@ func TestApp_Run_Version(t *testing.T) {
func TestApp_Run_Categories(t *testing.T) { func TestApp_Run_Categories(t *testing.T) {
app := NewApp() app := NewApp()
app.Name = "categories" app.Name = "categories"
app.CategorizedHelp = true
app.Commands = []Command{ app.Commands = []Command{
Command{ Command{
Name: "command1", Name: "command1",
@ -981,6 +980,13 @@ func TestApp_Run_Categories(t *testing.T) {
if !reflect.DeepEqual(app.Categories(), expect) { if !reflect.DeepEqual(app.Categories(), expect) {
t.Fatalf("expected categories %#v, to equal %#v", 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) { func TestApp_Run_DoesNotOverwriteErrorFromBefore(t *testing.T) {

View File

@ -3,6 +3,7 @@ package cli
import ( import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"sort"
"strings" "strings"
) )
@ -231,6 +232,13 @@ func (c Command) startApp(ctx *Context) error {
app.Email = ctx.App.Email app.Email = ctx.App.Email
app.Writer = ctx.App.Writer 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 // bash completion
app.EnableBashCompletion = ctx.App.EnableBashCompletion app.EnableBashCompletion = ctx.App.EnableBashCompletion
if c.BashComplete != nil { if c.BashComplete != nil {

18
help.go
View File

@ -23,11 +23,9 @@ VERSION:
AUTHOR(S): AUTHOR(S):
{{range .Authors}}{{ . }}{{end}} {{range .Authors}}{{ . }}{{end}}
{{end}}{{if .Commands}} {{end}}{{if .Commands}}
COMMANDS:{{if .CategorizedHelp}}{{range .Categories}}{{if .Name}} COMMANDS:{{range .Categories}}{{if .Name}}
{{.Name}}{{ ":" }}{{end}}{{range .Commands}} {{.Name}}{{ ":" }}{{end}}{{range .Commands}}
{{.Name}}{{with .ShortName}}, {{.}}{{end}}{{ "\t" }}{{.Usage}}{{end}} {{.Name}}{{with .ShortName}}, {{.}}{{end}}{{ "\t" }}{{.Usage}}{{end}}
{{end}}{{else}}{{range .Commands}}
{{.Name}}{{with .ShortName}}, {{.}}{{end}}{{ "\t" }}{{.Usage}}{{end}}
{{end}}{{end}}{{if .Flags}} {{end}}{{end}}{{if .Flags}}
GLOBAL OPTIONS: GLOBAL OPTIONS:
{{range .Flags}}{{.}} {{range .Flags}}{{.}}
@ -44,11 +42,12 @@ var CommandHelpTemplate = `NAME:
{{.HelpName}} - {{.Usage}} {{.HelpName}} - {{.Usage}}
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:
{{.Category}} {{.Category}}{{end}}{{if .Description}}
{{end}}DESCRIPTION:
DESCRIPTION:
{{.Description}}{{end}}{{if .Flags}} {{.Description}}{{end}}{{if .Flags}}
OPTIONS: OPTIONS:
@ -65,8 +64,9 @@ var SubcommandHelpTemplate = `NAME:
USAGE: USAGE:
{{.HelpName}} command{{if .Flags}} [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}} {{.HelpName}} command{{if .Flags}} [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}
COMMANDS: COMMANDS:{{range .Categories}}{{if .Name}}
{{range .Commands}}{{join .Names ", "}}{{ "\t" }}{{.Usage}} {{.Name}}{{ ":" }}{{end}}{{range .Commands}}
{{.Name}}{{with .ShortName}}, {{.}}{{end}}{{ "\t" }}{{.Usage}}{{end}}
{{end}}{{if .Flags}} {{end}}{{if .Flags}}
OPTIONS: OPTIONS:
{{range .Flags}}{{.}} {{range .Flags}}{{.}}