diff options
-rw-r--r-- | commands/commands.go | 30 |
1 files changed, 24 insertions, 6 deletions
diff --git a/commands/commands.go b/commands/commands.go index 44a08da0..e48cd542 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -2,16 +2,37 @@ package commands import ( "fmt" + "sort" "github.com/spf13/cobra" ) -var commandsDesc bool +var ( + commandsDesc bool +) + +type commandSorterByName []*cobra.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].CommandPath() < c[j].CommandPath() } func runCommands(cmd *cobra.Command, args []string) error { first := true - allCmds := cmd.Root().Commands() + var allCmds []*cobra.Command + queue := []*cobra.Command{RootCmd} + + for len(queue) > 0 { + cmd := queue[0] + queue = queue[1:] + allCmds = append(allCmds, cmd) + for _, c := range cmd.Commands() { + queue = append(queue, c) + } + } + + sort.Sort(commandSorterByName(allCmds)) for _, cmd := range allCmds { if !first { @@ -24,10 +45,7 @@ func runCommands(cmd *cobra.Command, args []string) error { fmt.Printf("# %s\n", cmd.Short) } - fmt.Printf("%s %s", - rootCommandName, - cmd.Use, - ) + fmt.Print(cmd.UseLine()) if commandsDesc { fmt.Println() |