-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.go
192 lines (177 loc) · 4.38 KB
/
cmd.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
package broccli
import (
"fmt"
"log"
"os"
"path"
"reflect"
"sort"
"text/tabwriter"
)
// Cmd represent a command which has a name (used in args when calling app), description, a handler that is called.
// Such command can have flags and arguments. In addition to that, required environment variables can be set.
type Cmd struct {
name string
desc string
flags map[string]*param
args map[string]*param
argsOrder []string
argsIdx int
envVars map[string]*param
handler func(*CLI) int
options cmdOptions
}
// AddFlag adds a flag to a command and returns a pointer to Param instance.
// Method requires name (eg. 'data' for '--data', alias (eg. 'd' for '-d'), placeholder for the value displayed on the
// 'help' screen, description, type of the value and additional validation that is set up with bit flags, eg. IsRequired
// or AllowMultipleValues. If no additional flags are required, 0 should be used.
func (c *Cmd) AddFlag(n string, a string, hv string, d string, t int64, f int64, opts ...paramOption) {
if c.flags == nil {
c.flags = map[string]*param{}
}
c.flags[n] = ¶m{
name: n,
alias: a,
desc: d,
helpValue: hv,
valueType: t,
flags: f,
options: paramOptions{},
}
for _, o := range opts {
o(&(c.flags[n].options))
}
}
// AddArg adds an argument to a command and returns a pointer to Param instance. It is the same as adding flag except
// it does not have an alias.
func (c *Cmd) AddArg(n string, hv string, d string, t int64, f int64, opts ...paramOption) {
if c.argsIdx > 9 {
log.Fatal("Only 10 arguments are allowed")
}
if c.args == nil {
c.args = map[string]*param{}
}
c.args[n] = ¶m{
name: n,
desc: d,
helpValue: hv,
valueType: t,
flags: f,
options: paramOptions{},
}
if c.argsOrder == nil {
c.argsOrder = make([]string, 10)
}
c.argsOrder[c.argsIdx] = n
c.argsIdx++
for _, o := range opts {
o(&(c.args[n].options))
}
}
// AddEnvVar adds a required environment variable to a command and returns a pointer to Param. It's arguments are very
// similar to ones in previous AddArg and AddFlag methods.
func (c *Cmd) AddEnvVar(n string, d string, t int64, f int64, opts ...paramOption) {
if c.envVars == nil {
c.envVars = map[string]*param{}
}
c.envVars[n] = ¶m{
name: n,
desc: d,
valueType: t,
flags: f,
options: paramOptions{},
}
}
func (c *Cmd) sortedArgs() []string {
args := make([]string, c.argsIdx)
idx := 0
for i := 0; i < c.argsIdx; i++ {
n := c.argsOrder[i]
arg := c.args[n]
if arg.flags&IsRequired > 0 {
args[idx] = n
idx++
}
}
for i := 0; i < c.argsIdx; i++ {
n := c.argsOrder[i]
arg := c.args[n]
if arg.flags&IsRequired == 0 {
args[idx] = n
idx++
}
}
return args
}
func (c *Cmd) sortedFlags() []string {
fs := reflect.ValueOf(c.flags).MapKeys()
sfs := make([]string, len(fs))
for i, f := range fs {
sfs[i] = f.String()
}
sort.Strings(sfs)
return sfs
}
func (c *Cmd) sortedEnvVars() []string {
evs := reflect.ValueOf(c.envVars).MapKeys()
sevs := make([]string, len(evs))
for i, ev := range evs {
sevs[i] = ev.String()
}
sort.Strings(sevs)
return sevs
}
// PrintHelp prints command usage information to stdout file.
func (c *Cmd) printHelp() {
fmt.Fprintf(os.Stdout, fmt.Sprintf("\nUsage: %s %s [FLAGS]%s\n\n", path.Base(os.Args[0]), c.name,
c.argsHelpLine()))
fmt.Fprintf(os.Stdout, fmt.Sprintf("%s\n", c.desc))
if len(c.envVars) > 0 {
fmt.Fprintf(os.Stdout, "\nRequired environment variables:\n")
w := new(tabwriter.Writer)
w.Init(os.Stdout, 8, 8, 0, '\t', 0)
for _, n := range c.sortedEnvVars() {
fmt.Fprintf(w, "%s\t%s\n", n, c.envVars[n].desc)
}
w.Flush()
}
w := new(tabwriter.Writer)
w.Init(os.Stdout, 8, 8, 0, '\t', 0)
var s [2]string
i := 1
for _, n := range c.sortedFlags() {
flag := c.flags[n]
if flag.flags&IsRequired > 0 {
i = 0
} else {
i = 1
}
s[i] += flag.helpLine()
}
if s[0] != "" {
fmt.Fprintf(w, "\nRequired flags: \n")
fmt.Fprintf(w, s[0])
w.Flush()
}
if s[1] != "" {
fmt.Fprintf(w, "\nOptional flags: \n")
fmt.Fprintf(w, s[1])
w.Flush()
}
}
func (c *Cmd) argsHelpLine() string {
sr := ""
so := ""
if c.argsIdx > 0 {
for i := 0; i < c.argsIdx; i++ {
n := c.argsOrder[i]
arg := c.args[n]
if arg.flags&IsRequired > 0 {
sr += " " + arg.helpValue
} else {
so += " [" + arg.helpValue + "]"
}
}
}
return sr + so
}