-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelp.go
44 lines (38 loc) · 990 Bytes
/
help.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package main
import (
"bytes"
"fmt"
"github.com/mitchellh/cli"
"sort"
"strings"
)
func helpFunc(commands map[string]cli.CommandFactory) string {
// Find the maximum command length for formatting
maxCmdLen := 0
for key, _ := range commands {
if len(key) > maxCmdLen {
maxCmdLen = len(key)
}
}
helpText := fmt.Sprintf(`
Usage: marsho [--version] [--help] <command> [args]
%s
`, listCommands(commands, maxCmdLen))
return strings.TrimSpace(helpText)
}
func listCommands(commands map[string]cli.CommandFactory, maxCmdLen int) string {
var buf bytes.Buffer
keys := make([]string, 0, len(commands))
for key, _ := range commands {
keys = append(keys, key)
}
sort.Strings(keys)
//TODO: error checking
for _, key := range keys {
commandFunc, _ := commands[key]
command, _ := commandFunc()
key = fmt.Sprintf("%s%s", key, strings.Repeat(" ", maxCmdLen-len(key)))
buf.WriteString(fmt.Sprintf(" %s %s\n", key, command.Synopsis()))
}
return buf.String()
}