Categories as slice, not a map anymore, order is always preserved
This commit is contained in:
parent
a0801792cc
commit
994a7028e2
8
app.go
8
app.go
@ -6,6 +6,7 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"sort"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -37,7 +38,7 @@ type App struct {
|
|||||||
// Display commands by category
|
// Display commands by category
|
||||||
CategorizedHelp bool
|
CategorizedHelp bool
|
||||||
// Populate when displaying AppHelp
|
// Populate when displaying AppHelp
|
||||||
Categories map[string]Commands
|
Categories CommandCategories
|
||||||
// An action to execute when the bash-completion flag is set
|
// An action to execute when the bash-completion flag is set
|
||||||
BashComplete func(context *Context)
|
BashComplete func(context *Context)
|
||||||
// An action to execute before any subcommands are run, but after the context is ready
|
// An action to execute before any subcommands are run, but after the context is ready
|
||||||
@ -100,11 +101,12 @@ func (a *App) Run(arguments []string) (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if a.CategorizedHelp {
|
if a.CategorizedHelp {
|
||||||
a.Categories = make(map[string]Commands)
|
a.Categories = CommandCategories{}
|
||||||
for _, command := range a.Commands {
|
for _, command := range a.Commands {
|
||||||
a.Categories[command.Category] = append(a.Categories[command.Category], command)
|
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 {
|
||||||
|
30
category.go
Normal file
30
category.go
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
package cli
|
||||||
|
|
||||||
|
type CommandCategories []*CommandCategory
|
||||||
|
|
||||||
|
type CommandCategory struct {
|
||||||
|
Name string
|
||||||
|
Commands Commands
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c CommandCategories) Less(i, j int) bool {
|
||||||
|
return c[i].Name < c[j].Name
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c CommandCategories) Len() int {
|
||||||
|
return len(c)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c CommandCategories) Swap(i, j int) {
|
||||||
|
c[i], c[j] = c[j], c[i]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c CommandCategories) AddCommand(category string, command Command) CommandCategories {
|
||||||
|
for _, commandCategory := range c {
|
||||||
|
if commandCategory.Name == category {
|
||||||
|
commandCategory.Commands = append(commandCategory.Commands, command)
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return append(c, &CommandCategory{Name: category, Commands: []Command{command}})
|
||||||
|
}
|
4
help.go
4
help.go
@ -23,8 +23,8 @@ VERSION:
|
|||||||
AUTHOR(S):
|
AUTHOR(S):
|
||||||
{{range .Authors}}{{ . }}{{end}}
|
{{range .Authors}}{{ . }}{{end}}
|
||||||
{{end}}{{if .Commands}}
|
{{end}}{{if .Commands}}
|
||||||
COMMANDS:{{if .CategorizedHelp}}{{range $category, $commands := .Categories}}{{if $category}}
|
COMMANDS:{{if .CategorizedHelp}}{{range .Categories}}{{if .Name}}
|
||||||
{{$category}}{{ ":" }}{{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}}
|
{{end}}{{else}}{{range .Commands}}
|
||||||
{{.Name}}{{with .ShortName}}, {{.}}{{end}}{{ "\t" }}{{.Usage}}{{end}}
|
{{.Name}}{{with .ShortName}}, {{.}}{{end}}{{ "\t" }}{{.Usage}}{{end}}
|
||||||
|
Loading…
Reference in New Issue
Block a user