This repository has been archived by the owner on Mar 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
usage.go
153 lines (124 loc) · 3.32 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
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
package jhanda
import (
"fmt"
"reflect"
"sort"
"strings"
)
// Usage provides all of the details describing a Command, including a
// description, a shorter description (used when display a list of commands),
// and the flag options offered by the Command.
type Usage struct {
Description string
ShortDescription string
Flags interface{}
}
// PrintUsage will return a string representation of the options provided by a
// Command flag set.
func PrintUsage(receiver interface{}) (string, error) {
v := reflect.ValueOf(receiver)
t := v.Type()
if t.Kind() != reflect.Struct {
return "", fmt.Errorf("unexpected pointer to non-struct type %s", t.Kind())
}
fields := getFields(t)
var usage []string
var length int
for _, field := range fields {
var arguments []string
long, ok := field.Tag.Lookup("long")
if ok {
arguments = append(arguments, fmt.Sprintf("--%s", long))
}
short, ok := field.Tag.Lookup("short")
if ok {
arguments = append(arguments, fmt.Sprintf("-%s", short))
}
envs, ok := field.Tag.Lookup("env")
if ok {
for _, env := range strings.Split(envs, ",") {
arguments = append(arguments, fmt.Sprintf("%s", env))
}
}
field := strings.Join(arguments, ", ")
if len(field) > length {
length = len(field)
}
usage = append(usage, field)
}
for i, line := range usage {
usage[i] = pad(line, " ", length)
}
for i, field := range fields {
var kindParts []string
if _, ok := field.Tag.Lookup("required"); ok {
kindParts = append(kindParts, "required")
}
kind := field.Type.Kind().String()
if kind == reflect.Slice.String() {
kind = field.Type.Elem().Kind().String()
kindParts = append(kindParts, "variadic")
}
if len(kindParts) > 0 {
kind = fmt.Sprintf("%s (%s)", kind, strings.Join(kindParts, ", "))
}
line := fmt.Sprintf("%s %s", usage[i], kind)
if len(line) > length {
length = len(line)
}
usage[i] = line
}
for i, line := range usage {
usage[i] = pad(line, " ", length)
}
for i, field := range fields {
description, ok := field.Tag.Lookup("description")
if ok {
if _, ok := field.Tag.Lookup("deprecated"); ok {
description = fmt.Sprintf("**DEPRECATED** %s", description)
}
if _, ok := field.Tag.Lookup("experimental"); ok {
description = fmt.Sprintf("**EXPERIMENTAL** %s", description)
}
usage[i] += fmt.Sprintf(" %s", description)
}
}
for i, field := range fields {
defaultValue, ok := field.Tag.Lookup("default")
if ok {
usage[i] += fmt.Sprintf(" (default: %s)", defaultValue)
}
}
for i, field := range fields {
aliases, ok := field.Tag.Lookup("alias")
if ok {
var arguments []string
for _, alias := range strings.Split(aliases, ",") {
arguments = append(arguments, fmt.Sprintf("--%s", alias))
}
usage[i] += fmt.Sprintf("\n (aliases: %s)", strings.Join(arguments, ", "))
}
}
sort.Strings(usage)
return strings.Join(usage, "\n"), nil
}
func getFields(t reflect.Type) []reflect.StructField {
var fields []reflect.StructField
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
if field.Type.Kind() == reflect.Struct {
fields = append(fields, getFields(field.Type)...)
continue
}
fields = append(fields, field)
}
return fields
}
func pad(str, pad string, length int) string {
for {
str += pad
if len(str) > length {
return str[0:length]
}
}
}