forked from tomma-a/ponzu
/
usage.go
99 lines (79 loc) · 2.62 KB
/
usage.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"fmt"
"sort"
"strings"
"text/template"
"unicode"
"github.com/spf13/cobra"
)
var templateFuncs = template.FuncMap{
"rpad": rpad,
"trimTrailingWhitespaces": trimRightSpace,
}
var tmpl = `{{with (or .Cmd.Long .Cmd.Short)}}{{. | trimTrailingWhitespaces}}
{{end}}{{if or .Cmd.Runnable .Cmd.HasSubCommands}}Usage:{{if .Cmd.Runnable}}
{{.Cmd.UseLine}}{{end}}{{if .Cmd.HasAvailableSubCommands}}
{{.Cmd.CommandPath}} [command]{{end}}{{if gt (len .Cmd.Aliases) 0}}
Aliases:
{{.Cmd.NameAndAliases}}{{end}}{{if .Cmd.HasExample}}
Examples:
{{.Cmd.Example}}{{end}}{{if .Cmd.HasAvailableSubCommands}}
Available Commands:{{range .Cmd.Commands}}{{if (or .IsAvailableCommand false)}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .Cmd.HasAvailableLocalFlags}}
Flags for all commands:
{{.Cmd.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{range .Subs}}{{if (and .IsAvailableCommand .HasAvailableLocalFlags)}}
Flags for '{{.Name}}' command:
{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{end}}{{if .Cmd.HasHelpSubCommands}}
Additional help topics:{{range .Cmd.Commands}}{{if .Cmd.IsAdditionalHelpTopicCommand}}
{{rpad .Cmd.CommandPath .Cmd.CommandPathPadding}} {{.Cmd.Short}}{{end}}{{end}}{{end}}{{if .Cmd.HasAvailableSubCommands}}
Use "{{.Cmd.CommandPath}} [command] --help" for more information about a command.{{end}}
{{end}}`
var helpCmd = &cobra.Command{
Use: "help",
Short: "help about any command",
Run: func(cmd *cobra.Command, args []string) {
cmd, _, e := rootCmd.Find(args)
if cmd == nil || e != nil {
rootCmd.Printf("Unknown help topic %#q\n", args)
rootCmd.Usage()
return
}
t := template.New("help")
t.Funcs(templateFuncs)
template.Must(t.Parse(tmpl))
if len(args) > 0 {
rootCmd.HelpFunc()(cmd, args)
return
}
sortByName := func(i, j int) bool { return cmds[i].Name() < cmds[j].Name() }
sort.Slice(cmds, sortByName)
err := t.Execute(cmd.OutOrStdout(), struct {
Cmd *cobra.Command
Subs []*cobra.Command
}{
Cmd: rootCmd,
Subs: cmds})
if err != nil {
cmd.Println(err)
}
},
}
var cmds []*cobra.Command
// RegisterCmdlineCommand adds a cobra command to the root command and makes it
// known to the main package
func RegisterCmdlineCommand(cmd *cobra.Command) {
rootCmd.AddCommand(cmd)
cmds = append(cmds, cmd)
}
func init() {
rootCmd.SetHelpCommand(helpCmd)
}
// rpad adds padding to the right of a string.
func rpad(s string, padding int) string {
template := fmt.Sprintf("%%-%ds", padding)
return fmt.Sprintf(template, s)
}
func trimRightSpace(s string) string {
return strings.TrimRightFunc(s, unicode.IsSpace)
}