Sort commands by their names
Before adoption: this commit came in with the history the project adopted, so no landing records it.
The slice of commands are sorted now automatically while Commands are called. To turn off this feature, EnableCommandSorting variable is added.
authored by Andrey Kurilin · committed by Andrey Kurilin · parent 1238ba1 · Browse files · 3 files·+62 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+62 added−1 removed
- other:+62 added−1 removed
Provenance
- Commit
ab97c7a- Tree
570eb84- Author
Andrey Kurilin <andr.kurilin@gmail.com>- Committer
Andrey Kurilin <andr.kurilin@gmail.com>- Parent
1238ba1
3 files changed+62 added−1 removed
modified:cobra.go+4 added
39 unmodified lines40 // automatic prefix matching can be a dangerous thing to automatically enable in CLI tools.41 // Set this to true to enable it42 var EnablePrefixMatching = false43 44+//EnableCommandSorting controls sorting of the slice of commands, which is turned on by default.45+//To disable sorting, set it to false.46+var EnableCommandSorting = true47+48 //AddTemplateFunc adds a template function that's available to Usage and Help49 //template generation.50 func AddTemplateFunc(name string, tmplFunc interface{}) {51 templateFuncs[name] = tmplFunc124 unmodified linesmodified:command_test.go+41 added
131 unmodified lines132 if !reflect.DeepEqual(as, targs) {133 t.Errorf("expected: %v, got: %v", as, targs)134 }135 }136+137+func TestCommandsAreSorted(t *testing.T) {138+ EnableCommandSorting = true139+140+ originalNames := []string{"middle", "zlast", "afirst"}141+ expectedNames := []string{"afirst", "middle", "zlast"}142+143+ var tmpCommand = &Command{Use: "tmp"}144+145+ for _, name := range(originalNames) {146+ tmpCommand.AddCommand(&Command{Use: name})147+ }148+149+ for i, c := range(tmpCommand.Commands()) {150+ if expectedNames[i] != c.Name() {151+ t.Errorf("expected: %s, got: %s", expectedNames[i], c.Name())152+ }153+ }154+155+ EnableCommandSorting = true156+}157+158+func TestEnableCommandSortingIsDisabled(t *testing.T) {159+ EnableCommandSorting = false160+161+ originalNames := []string{"middle", "zlast", "afirst"}162+163+ var tmpCommand = &Command{Use: "tmp"}164+165+ for _, name := range(originalNames) {166+ tmpCommand.AddCommand(&Command{Use: name})167+ }168+169+ for i, c := range(tmpCommand.Commands()) {170+ if originalNames[i] != c.Name() {171+ t.Errorf("expected: %s, got: %s", originalNames[i], c.Name())172+ }173+ }174+175+ EnableCommandSorting = true176+}modified:command.go+17 added-1 removed
19 unmodified lines20 "fmt"21 "io"22 "os"23 "path/filepath"24+ "sort"25 "strings"26 27 flag "github.com/spf13/pflag"28 )74 unmodified lines103 // max lengths of commands' string lengths for use in padding104 commandsMaxUseLen int105 commandsMaxCommandPathLen int106 commandsMaxNameLen int107+ // is commands slice are sorted or not108+ commandsAreSorted bool109 110 flagErrorBuf *bytes.Buffer111 112 args []string // actual args parsed from flags610 unmodified lines723 c.commands = nil724 c.helpCommand = nil725 }726 727+// Sorts commands by their names728+type commandSorterByName []*Command729+730+func (c commandSorterByName) Len() int { return len(c) }731+func (c commandSorterByName) Swap(i, j int) { c[i], c[j] = c[j], c[i] }732+func (c commandSorterByName) Less(i, j int) bool { return c[i].Name() < c[j].Name() }733+724-//Commands returns a slice of child commands.734+// Commands returns a sorted slice of child commands.735 func (c *Command) Commands() []*Command {736+ // do not sort commands if it already sorted or sorting was disabled737+ if EnableCommandSorting && !c.commandsAreSorted{738+ sort.Sort(commandSorterByName(c.commands))739+ c.commandsAreSorted = true740+ }741 return c.commands742 }743 744 // AddCommand adds one or more commands to this parent command.20 unmodified lines765 if c.globNormFunc != nil {766 x.SetGlobalNormalizationFunc(c.globNormFunc)767 }768 c.commands = append(c.commands, x)769+ c.commandsAreSorted = false770 }771 }772 773 // RemoveCommand removes one or more commands from a parent command.483 unmodified lines