-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgocli.go
308 lines (255 loc) · 6.26 KB
/
gocli.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
* gocli
* Copyright (c) 2015 Yieldbot, Inc.
* For the full copyright and license information, please view the LICENSE.txt file.
*/
// Package gocli is a CLI library that provides subcommand handling, tidy usage and version printing.
package gocli
import (
"errors"
"flag"
"fmt"
"log"
"os"
"runtime"
"sort"
"strings"
)
// Cli represent command line interface
type Cli struct {
// Name is the cli name
Name string
// Version is the cli version
Version string
// Description is the cli description
Description string
// Commands contains the subcommand list of the cli
Commands map[string]string
// SubCommand contains the runtime subcommand
SubCommand string
// SubCommandArgs contains the args of the runtime subcommand
SubCommandArgs []string
// SubCommandArgsMap contains the args of the runtime subcommand as mapped
SubCommandArgsMap map[string]string
// Flags contains flags
Flags map[string]string
// LogOut is logger for stdout
LogOut *log.Logger
// LogErr is logger for stderr
LogErr *log.Logger
}
// Init initializes Cli instance
func (cl *Cli) Init() {
// Init flag
if !flag.Parsed() {
flag.Parse()
}
// Init loggers
cl.LogOut = log.New(os.Stdout, "", log.LstdFlags)
cl.LogErr = log.New(os.Stderr, "", log.LstdFlags)
// Init flags
cl.Flags = make(map[string]string)
flag.VisitAll(func(f *flag.Flag) {
cl.Flags[f.Name] = f.Value.String()
})
// Init args
if len(os.Args) > 1 {
// Iterate the args
for _, arg := range os.Args {
// If the arg is in command list then
if _, ok := cl.Commands[arg]; ok {
cl.SubCommand = arg // set as command
} else {
// Otherwise add it to subcommand args
if cl.SubCommand != "" {
cl.SubCommandArgs = append(cl.SubCommandArgs, arg)
}
}
}
// Init subcommand args map
cl.SubCommandArgsMap = make(map[string]string)
var curArg string
for _, v := range cl.SubCommandArgs {
// If it's an arg then
if strings.HasPrefix(v, "-") {
curArg = strings.TrimLeft(v, "-")
if len(curArg) > 0 {
cl.SubCommandArgsMap[curArg] = ""
}
} else {
// Otherwise add it to current arg or add it as arg
if len(curArg) > 0 {
cl.SubCommandArgsMap[curArg] = v
curArg = ""
} else {
cl.SubCommandArgsMap[v] = ""
}
}
}
}
}
// PrintVersion prints version information
func (cl Cli) PrintVersion(extra bool) {
var ver string
if extra == true {
ver += fmt.Sprintf("Bin Version : %s\n", strings.TrimPrefix(cl.Version, "v"))
ver += fmt.Sprintf("Go version : %s", runtime.Version())
} else {
ver = fmt.Sprintf("%s", strings.TrimPrefix(cl.Version, "v"))
}
fmt.Println(ver)
}
// PrintUsage prints usage info
// Usage format follows common convention for Go apps
func (cl Cli) PrintUsage() {
// Init vars
type flagInfo struct {
nameu string
name string
usage string
defValue string
}
// Find the longest command for alignment
maxlen := 0
if len(cl.Commands) > 0 {
for c := range cl.Commands {
if len(c) > maxlen {
maxlen = len(c)
}
}
}
// Iterate flags
flagList := make(map[string]*flagInfo)
flag.VisitAll(func(f *flag.Flag) {
// If the flag name starts with `test.` then
if strings.Index(f.Name, "test.") == 0 {
return
}
// Set key by the flag usage for grouping
key := fmt.Sprint(f.Usage)
// Init usage name
nameu := "-" + f.Name
if len(f.Name) > 2 {
nameu = "-" + nameu
}
// If the flag exists then
if _, ok := flagList[key]; ok {
// Merge names
flagList[key].nameu += ", " + nameu
} else {
// Otherwise add the flag
flagList[key] = &flagInfo{
nameu: nameu,
name: f.Name,
usage: f.Usage,
defValue: f.DefValue,
}
}
// Check and set maximum length for alignment
if len(flagList[key].nameu) > maxlen {
maxlen = len(flagList[key].nameu)
}
})
var maxlenF = fmt.Sprintf("%d", maxlen)
// Fixed flag list
flagListF := []string{}
for _, v := range flagList {
flagline := fmt.Sprintf("%-"+maxlenF+"s : %s", v.nameu, v.usage)
if v.defValue != "false" && v.defValue != "" {
flagline += " (default \"" + v.defValue + "\")"
}
flagListF = append(flagListF, flagline)
}
sort.Strings(flagListF)
// Fixed command list
cmdListF := []string{}
for cn, cv := range cl.Commands {
cmdListF = append(cmdListF, fmt.Sprintf("%-"+maxlenF+"s : %s", cn, cv))
}
sort.Strings(cmdListF)
// Header and description
usage := "Usage: " + cl.Name + " [OPTIONS] COMMAND [arg...]\n\n"
if cl.Description != "" {
usage += cl.Description + "\n\n"
}
// Options
if len(flagListF) > 0 {
usage += "Options:\n"
for _, f := range flagListF {
usage += fmt.Sprintf(" %s\n", f)
}
}
// Commands
if len(cmdListF) > 0 {
usage += "\nCommands:\n"
for _, c := range cmdListF {
usage += fmt.Sprintf(" %s\n", c)
}
}
fmt.Println(usage)
}
// Table represent tabular data as a table
type Table struct {
data [][]string
colSizes map[int]int
}
// Data gets data
func (t *Table) Data() [][]string {
return t.data
}
// SetData sets a data by the given row, column and value
func (t *Table) SetData(row, col int, val string) error {
// Check row and column numbers
if row < 1 || col < 1 {
return errors.New("invalid row or column index")
}
// Increase the row capacity if it's necessary
if row > len(t.data) {
nt := make([][]string, row)
copy(nt, t.data)
t.data = nt
}
// Increase the column capacity if it's necessary
if col > len(t.data[row-1]) {
nr := make([]string, col)
copy(nr, t.data[row-1])
t.data[row-1] = nr
}
// Set the value
t.data[row-1][col-1] = val
// Set the column size for alignment
if t.colSizes == nil {
t.colSizes = make(map[int]int)
}
if len(val) > t.colSizes[col-1] {
t.colSizes[col-1] = len(val)
}
return nil
}
// AddRow adds a row data by the given row number and column values
func (t *Table) AddRow(row int, cols ...string) error {
// Iterate rows and set data
for i, v := range cols {
if err := t.SetData(row, i+1, v); err != nil {
return err
}
}
return nil
}
// PrintData prints data
func (t *Table) PrintData() {
if len(t.data) == 0 {
return
}
// Print data
var rowVal string
var colSize string
for _, row := range t.data {
rowVal = ""
for i, c := range row {
colSize = fmt.Sprintf("%d", t.colSizes[i])
rowVal += fmt.Sprintf("%-"+colSize+"s\t", c)
}
fmt.Println(rowVal)
}
}