-
Notifications
You must be signed in to change notification settings - Fork 0
/
help.go
175 lines (141 loc) · 3.33 KB
/
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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package cligobrr
import "fmt"
import "strings"
import "strconv"
func appHelp(app *App, input []string) error {
return help(
app.Name,
app.Description,
app.Cmds,
app.Args,
input,
)
}
func cmdHelp(cmd *Cmd, input []string) error {
return help(
cmd.Name,
cmd.Description,
cmd.Cmds,
cmd.Args,
input,
)
}
func help(
name string,
description string,
commands Cmds,
arguments Args,
input []string,
) error {
helpHeader(name, description)
if len(input) > 0 {
token := input[0]
arg := arguments.get(token)
if arg == nil {
return errUnexpectedArg(token)
}
helpUsage(name, []IArg{*arg})
helpSingleArg(*arg)
} else {
args := arguments.args
if len(args) > 0 {
helpUsage(name, args)
helpAllArgs(name, args)
}
helpCmds(commands.cmds)
}
return nil
}
func helpHeader(name, description string) {
tableFields := TableFields{
Cols: 2,
}
table, _ := tableNew(tableFields)
table.Add([]string{"Name:", name})
if len(description) > 0 {
table.Add([]string{"Description:", description})
}
fmt.Println(table.ToString())
fmt.Println("")
}
func helpUsage(name string, args []IArg) {
output := []string{"Usage:", ""}
cmdLine := []string{name}
for _, arg := range args {
fragment := fmt.Sprintf("%s=%s", arg.GetName(), arg.GetKind())
if !arg.GetRequired() {
fragment = fmt.Sprintf("[%s]", fragment)
}
cmdLine = append(cmdLine, fragment)
}
output = append(output, strings.Join(cmdLine, " "))
fmt.Println(strings.Join(output, "\n"))
fmt.Println("")
}
func helpSingleArg(arg IArg) {
tableFields := TableFields{
Cols: 2,
}
table, _ := tableNew(tableFields)
table.Add([]string{"Name:", arg.GetName()})
table.Add([]string{"Alias:", arg.GetAlias()})
table.Add([]string{"Description:", arg.GetDescription()})
table.Add([]string{"Kind:", arg.GetKind()})
table.Add([]string{"Multiple:", strconv.FormatBool(arg.GetMultiple())})
table.Add([]string{"Required:", strconv.FormatBool(arg.GetRequired())})
table.Add([]string{"Default:", arg.GetDefault()})
table.Add([]string{"Choices:", strings.Join(arg.GetChoices(), arg.GetSeparator())})
fmt.Println(table.ToString())
fmt.Println("")
}
func helpAllArgs(name string, args []IArg) {
output := []string{
"Arguments:",
"",
}
tableFields := TableFields{
Cols: 2,
}
table, _ := tableNew(tableFields)
table.Add([]string{"Name", "Description"})
table.Add([]string{"----", "-----------"})
for _, arg := range args {
table.Add([]string{arg.GetName(), arg.GetDescription()})
}
output = append(output, table.ToString())
output = append(output, "")
output = append(output, fmt.Sprintf("`%s help arg` for more information.", name))
fmt.Println(strings.Join(output, "\n"))
fmt.Println("")
}
func helpCmds(cmds []Cmd) {
output := []string{
"Commands:",
"",
}
tableFields := TableFields{
Cols: 3,
}
table, _ := tableNew(tableFields)
table.Add([]string{"Name", "Alias", "Description"})
table.Add([]string{"----", "-----", "-----------"})
hasDefault := false
for _, cmd := range cmds {
name := cmd.Name
if cmd.Default {
name = fmt.Sprintf("%s*", name)
hasDefault = true
}
table.Add([]string{
name,
cmd.Alias,
cmd.Description,
})
}
output = append(output, table.ToString())
if hasDefault {
output = append(output, "")
output = append(output, "* indicates default command")
}
fmt.Println(strings.Join(output, "\n"))
}