-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimplecli.go
257 lines (210 loc) · 6.57 KB
/
simplecli.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package simplecli
import (
"fmt"
"os"
"reflect"
"strconv"
"strings"
)
// THE program name
var programName = os.Args[0]
var programArgs = os.Args[1:]
// OptionPrefix constant
const OptionPrefix = `--`
// Handle takes a point to a struct and construct a CommandGroup,
// based on the declared fields & methods of the specified struct.
func Handle(command interface{}) {
cmdGroup := newCommandGroup(programName, command)
defer cmdGroup.gracefulExit()
cmdGroup.handle(programArgs)
}
// CommandGroup group commands
type CommandGroup struct {
command interface{}
commandName string
subCommandByName map[string]reflect.Value
subCommandGroups map[string]*CommandGroup
}
// newCommandGroup constructs a new command group from a struct pointer
func newCommandGroup(name string, ptr interface{}) *CommandGroup {
ptrVal := reflect.ValueOf(ptr)
if !isPtrToStruct(ptrVal) {
panic("The passed interface is not a pointer to a struct.")
}
structVal := reflect.ValueOf(ptr).Elem()
structTyp := reflect.TypeOf(ptr).Elem()
cmdGroup := CommandGroup{
command: ptr,
commandName: name,
}
// Initialize subcommands with declared methods of cmd.Command struct
cmdGroup.subCommandByName = make(map[string]reflect.Value)
for i := 0; i < structVal.NumMethod(); i++ {
methodName := structTyp.Method(i).Name
lowerName := strings.ToLower(methodName)
cmdGroup.subCommandByName[lowerName] = structVal.MethodByName(methodName)
}
// Scan structs fields for possible SubCommands
cmdGroup.subCommandGroups = make(map[string]*CommandGroup)
for i := 0; i < structVal.NumField(); i++ {
fieldTyp := structTyp.Field(i)
fieldVal := structVal.Field(i)
if isPtrToStruct(fieldVal) {
lowerName := strings.ToLower(fieldTyp.Name)
cmdGroup.subCommandGroups[lowerName] = newCommandGroup(
lowerName, fieldVal.Interface(),
)
}
}
return &cmdGroup
}
func (grp *CommandGroup) gracefulExit() {
if message := recover(); message != nil {
fmt.Fprint(os.Stderr, message)
fmt.Fprintln(os.Stdout, grp.getHelp())
os.Exit(-1)
}
}
func (grp *CommandGroup) parseOption(option string) {
var field reflect.Value
var reflectedValue = reflect.ValueOf(grp.command).Elem()
// Parse option=value from string
option, value := func() (string, string) {
items := strings.Split(option, "=")
if len(items) == 1 {
return items[0], ""
}
return items[0], items[1]
}()
// Make sure there is a struct field with same name as `option`
if field = reflectedValue.FieldByName(strings.Title(option)); !field.IsValid() {
message := fmt.Sprintf("The option --%s is not a recongized option.\n", option)
panic(message)
}
// If there is a value passed, parse and set struct field
if value != "" {
parsedValue := grp.parseAs(value, field.Kind())
field.Set(parsedValue)
return
}
// If no value passed, but for bool fields store true
if field.Kind() == reflect.Bool {
field.SetBool(true)
return
}
// Raise error for non-Bool types if no value passed
message := fmt.Sprintf("No value passed for option --%s.\n", option)
panic(message)
}
// invoke either relevant method in CommandGroup or chain to SubCommandGroup
func (grp *CommandGroup) handle(optargs []string) {
defer grp.gracefulExit()
var args []string
for i, arg := range optargs {
// parse options as long as it starts with `--`
if strings.HasPrefix(arg, OptionPrefix) {
option := arg[len(OptionPrefix):]
grp.parseOption(option)
} else {
args = optargs[i:]
break
}
}
// if no arguments passed
if len(args) <= 0 {
panic("")
}
subcmd := args[0]
subargs := args[1:]
if subgrp, ok := grp.subCommandGroups[subcmd]; ok {
subgrp.handle(subargs)
return
}
// Check whether there is corresponding receiver method for subCommand
method, ok := grp.subCommandByName[subcmd]
if !ok {
message := fmt.Sprintf(" %s is not a valid command for %s.\n", subcmd, grp.commandName)
panic(message)
}
// The remaining arg types / len align with receiver method params on struct.
methodType := method.Type()
methodArgs := make([]reflect.Value, methodType.NumIn())
if len(subargs) != len(methodArgs) {
message := fmt.Sprintf("%s requires %d argument(s).\n",
subcmd,
methodType.NumIn())
panic(message)
}
for i := 0; i < methodType.NumIn(); i++ {
argIn := methodType.In(i)
methodArgs[i] = grp.parseAs(subargs[i], argIn.Kind())
}
method.Call(methodArgs)
}
// parse cli arg (string) to specific golang type
func (grp *CommandGroup) parseAs(arg string, kind reflect.Kind) reflect.Value {
switch kind {
case reflect.String:
return reflect.ValueOf(arg)
case reflect.Int:
num, err := strconv.ParseInt(arg, 10, 32)
if err != nil {
message := fmt.Sprintf("%s is not a valid number.\n", arg)
panic(message)
}
return reflect.ValueOf(int(num))
case reflect.Bool:
bool, err := strconv.ParseBool(arg)
if err != nil {
message := fmt.Sprintf("%s is not a valid bool.\n", arg)
panic(message)
}
return reflect.ValueOf(bool)
default:
message := fmt.Sprintf("Argument type %s is currently not suppotyped yet.\n", kind)
panic(message)
}
}
func (grp *CommandGroup) getHelp() string {
typ := reflect.TypeOf(grp.command).Elem()
val := reflect.ValueOf(grp.command).Elem()
numOptions := val.NumField() - len(grp.subCommandGroups)
hasOptions := numOptions > 0
var programInfo string
if hasOptions {
programInfo = fmt.Sprintf("Usage: %s [options] ", grp.commandName)
} else {
programInfo = fmt.Sprintf("Usage: %s ", grp.commandName)
}
indentation := strings.Repeat(" ", len(programInfo))
descriptions := make([]string, 0, len(grp.subCommandByName))
for name, cmd := range grp.subCommandByName {
args := cmd.Type().String()[4:] // 4 is the magic number to strip `Func`
desc := fmt.Sprintf("%s %s", name, args)
descriptions = append(descriptions, desc)
}
for name := range grp.subCommandGroups {
desc := fmt.Sprintf("%s ...", name)
descriptions = append(descriptions, desc)
}
help := programInfo + strings.Join(descriptions, "\n"+indentation)
if hasOptions {
indentation = strings.Repeat(" ", 7)
descriptions := make([]string, 0, numOptions)
for i := 0; i < typ.NumField(); i++ {
if !isPtrToStruct(val.Field(i)) {
field := typ.Field(i)
format := "%s --%s (%s) `%s`"
desc := fmt.Sprintf(format, indentation, strings.ToLower(field.Name), field.Type, field.Tag)
descriptions = append(descriptions, desc)
}
}
options := "Options:\n" + strings.Join(descriptions, "\n")
help = help + "\n" + options
}
return help
}
// return whether the value is a pointer to a struct
func isPtrToStruct(v reflect.Value) bool {
return v.Kind() == reflect.Ptr && v.Elem().Kind() == reflect.Struct
}