Sort commands in help output

Closes #5
This commit is contained in:
Ryan Cavicchioni 2021-02-02 18:34:37 -06:00
parent 0cbd256b86
commit 4e25579718
Signed by: ryanc
GPG Key ID: 877EEDAF9245103D

View File

@ -3,6 +3,7 @@ package lumecmd
import ( import (
"flag" "flag"
"fmt" "fmt"
"sort"
) )
func init() { func init() {
@ -32,7 +33,10 @@ func HelpCmd(args CmdArgs) (int, error) {
func printHelp(commands map[string]Command) { func printHelp(commands map[string]Command) {
var maxLen, cmdLen int var maxLen, cmdLen int
var keys []string
for _, c := range commands { for _, c := range commands {
keys = append(keys, c.Name)
cmdLen = len(c.Name) cmdLen = len(c.Name)
if cmdLen > maxLen { if cmdLen > maxLen {
maxLen = cmdLen maxLen = cmdLen
@ -41,9 +45,11 @@ func printHelp(commands map[string]Command) {
fmt.Printf("usage:\n lume <command> [<args...>]") fmt.Printf("usage:\n lume <command> [<args...>]")
fmt.Println() fmt.Println()
fmt.Println("\ncommands:") fmt.Println("\ncommands:")
for _, c := range commands {
sort.Strings(keys)
for _, k := range keys {
c := commands[k]
fmt.Printf(" %-*s %s\n", maxLen, c.Name, c.Short) fmt.Printf(" %-*s %s\n", maxLen, c.Name, c.Short)
} }
} }