Sort commands by their names

The slice of commands are sorted now automatically while Commands are called.
To turn off this feature, EnableCommandSorting variable is added.
This commit is contained in:
Andrey Kurilin
2016-06-14 17:04:53 +03:00
parent 1238ba19d2
commit ab97c7a63a
3 changed files with 62 additions and 1 deletions

View File

@ -21,6 +21,7 @@ import (
"io"
"os"
"path/filepath"
"sort"
"strings"
flag "github.com/spf13/pflag"
@ -103,6 +104,8 @@ type Command struct {
commandsMaxUseLen int
commandsMaxCommandPathLen int
commandsMaxNameLen int
// is commands slice are sorted or not
commandsAreSorted bool
flagErrorBuf *bytes.Buffer
@ -721,8 +724,20 @@ func (c *Command) ResetCommands() {
c.helpCommand = nil
}
//Commands returns a slice of child commands.
// Sorts commands by their names
type commandSorterByName []*Command
func (c commandSorterByName) Len() int { return len(c) }
func (c commandSorterByName) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
func (c commandSorterByName) Less(i, j int) bool { return c[i].Name() < c[j].Name() }
// Commands returns a sorted slice of child commands.
func (c *Command) Commands() []*Command {
// do not sort commands if it already sorted or sorting was disabled
if EnableCommandSorting && !c.commandsAreSorted{
sort.Sort(commandSorterByName(c.commands))
c.commandsAreSorted = true
}
return c.commands
}
@ -751,6 +766,7 @@ func (c *Command) AddCommand(cmds ...*Command) {
x.SetGlobalNormalizationFunc(c.globNormFunc)
}
c.commands = append(c.commands, x)
c.commandsAreSorted = false
}
}