Skip to content
Myr

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

3 files+62 added−1 removed

modified:cobra.go+4 added
39 unmodified lines
40// automatic prefix matching can be a dangerous thing to automatically enable in CLI tools.41// Set this to true to enable it42var EnablePrefixMatching = false4344//EnableCommandSorting controls sorting of the slice of commands, which is turned on by default.45//To disable sorting, set it to false.46var EnableCommandSorting = true4748//AddTemplateFunc adds a template function that's available to Usage and Help49//template generation.50func AddTemplateFunc(name string, tmplFunc interface{}) {51 templateFuncs[name] = tmplFunc
124 unmodified lines
modified:command_test.go+41 added
131 unmodified lines
132 if !reflect.DeepEqual(as, targs) {133 t.Errorf("expected: %v, got: %v", as, targs)134 }135}136137func TestCommandsAreSorted(t *testing.T) {138 EnableCommandSorting = true139140 originalNames := []string{"middle", "zlast", "afirst"}141 expectedNames := []string{"afirst", "middle", "zlast"}142143 var tmpCommand = &Command{Use: "tmp"}144145 for _, name := range(originalNames) {146 tmpCommand.AddCommand(&Command{Use: name})147 }148149 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 }154155 EnableCommandSorting = true156}157158func TestEnableCommandSortingIsDisabled(t *testing.T) {159 EnableCommandSorting = false160161 originalNames := []string{"middle", "zlast", "afirst"}162163 var tmpCommand = &Command{Use: "tmp"}164165 for _, name := range(originalNames) {166 tmpCommand.AddCommand(&Command{Use: name})167 }168169 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 }174175 EnableCommandSorting = true176}
modified:command.go+17 added-1 removed
19 unmodified lines
20 "fmt"21 "io"22 "os"23 "path/filepath"24 "sort"25 "strings"2627 flag "github.com/spf13/pflag"28)
74 unmodified lines
103 // max lengths of commands' string lengths for use in padding104 commandsMaxUseLen int105 commandsMaxCommandPathLen int106 commandsMaxNameLen int107 // is commands slice are sorted or not108 commandsAreSorted bool109110 flagErrorBuf *bytes.Buffer111112 args []string // actual args parsed from flags
610 unmodified lines
723 c.commands = nil724 c.helpCommand = nil725}726727// Sorts commands by their names728type commandSorterByName []*Command729730func (c commandSorterByName) Len() int { return len(c) }731func (c commandSorterByName) Swap(i, j int) { c[i], c[j] = c[j], c[i] }732func (c commandSorterByName) Less(i, j int) bool { return c[i].Name() < c[j].Name() }733724//Commands returns a slice of child commands.734// Commands returns a sorted slice of child commands.735func (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}743744// AddCommand adds one or more commands to this parent command.
20 unmodified lines
765 if c.globNormFunc != nil {766 x.SetGlobalNormalizationFunc(c.globNormFunc)767 }768 c.commands = append(c.commands, x)769 c.commandsAreSorted = false770 }771}772773// RemoveCommand removes one or more commands from a parent command.
483 unmodified lines