-
Notifications
You must be signed in to change notification settings - Fork 0
/
help.go
114 lines (91 loc) · 2.67 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
package klash
import (
"errors"
"fmt"
"reflect"
"strings"
)
var HelpError = errors.New("Help error")
func GenerateUsage(params *Params) string {
usages := make([]string, 0, len(params.Listing)+2)
usages = append(usages, "[-h]")
for _, parameter := range params.Listing {
var paramUsage string
shortName := DecomposeName(parameter.Alias, true)
if shortName == "" {
shortName = DecomposeName(parameter.Name, true)
}
shortName = Dashed(shortName)
if parameter.Value.Type().Kind() != reflect.Bool {
paramUsage = fmt.Sprintf(
"[%s %s]",
shortName,
DecomposeName(parameter.Name, false),
)
} else {
paramUsage = fmt.Sprintf("[%s]", shortName)
}
usages = append(usages, paramUsage)
}
usages = append(usages, "ARGS...")
return strings.Join(usages, " ")
}
func GenerateDetails(params *Params) string {
detail := ""
length := len(params.Listing)
if length > 0 {
helpArgs := "-h, --help=false"
maxLength := len(helpArgs)
details := make([][2]string, 0, length+2)
detail = fmt.Sprintf("argument details:\n")
details = append(details, [2]string{helpArgs, "Show this help"})
for _, parameter := range params.Listing {
var paramName string
name := fmt.Sprintf("%s", Dashed(DecomposeName(parameter.Name, true)))
if parameter.Alias != "" {
name = fmt.Sprintf("%s, %s", Dashed(DecomposeName(parameter.Alias, true)), name)
}
switch parameter.Value.Type().Kind() {
case reflect.Bool:
paramName = fmt.Sprintf("%s=%t", name, parameter.Value.Interface())
case reflect.Slice:
paramName = fmt.Sprintf("%s=[]", name)
case reflect.String:
paramName = fmt.Sprintf("%s=\"%s\"", name, parameter.Value.Interface())
case reflect.Int:
paramName = fmt.Sprintf("%s=%d", name, parameter.Value.Interface())
default:
paramName = fmt.Sprintf("%s=%s", name, parameter.Value.Interface())
}
paramLength := len(paramName)
if paramLength > maxLength {
maxLength = paramLength
}
details = append(details, [2]string{paramName, parameter.Help})
}
separator := "\t"
for _, detailArg := range details {
spaces := ""
for i := 0; i < maxLength-len(detailArg[0])+8; i++ {
spaces = spaces + " "
}
detail = fmt.Sprintf("%s%s%s%s%s", detail, separator, detailArg[0], spaces, detailArg[1])
separator = "\n\t"
}
}
return detail
}
func GenerateHelp(name, desc string, params *Params) string {
usageLine := fmt.Sprintf("Usage: %s %s", name, GenerateUsage(params))
usage := fmt.Sprintf("%s\n\n",
usageLine,
)
if desc != "" {
usage = fmt.Sprintf("%s%s\n\n", usage, desc)
}
details := GenerateDetails(params)
if details != "" {
usage = fmt.Sprintf("%s%s\n", usage, details)
}
return usage
}