-
Notifications
You must be signed in to change notification settings - Fork 0
/
subcommand.go
206 lines (173 loc) · 3.63 KB
/
subcommand.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
package main
import (
"flag"
"fmt"
"log"
"os"
"sort"
"strings"
)
type (
cmdOption func(c *baseCommand)
baseCommand struct {
isDebug bool
cmdsMap map[cmdName]cmdable
usage func()
}
)
func NewItems(cmds ...cmdable) []cmdable {
return cmds
}
func NewCommandController(cmds []cmdable, opts ...cmdOption) *baseCommand {
c := &baseCommand{
cmdsMap: make(map[cmdName]cmdable),
}
for _, opt := range cmds {
var name cmdName
switch targetOpt := opt.(type) {
case *boolCmd:
targetOpt.ptr = flag.Bool(string(targetOpt.name), targetOpt.def, targetOpt.description)
if targetOpt.alias != "" {
flag.BoolVar(targetOpt.ptr, targetOpt.alias, targetOpt.def, "")
}
name = targetOpt.name
case *stringCmd:
targetOpt.ptr = flag.String(string(targetOpt.name), targetOpt.def, targetOpt.description)
if targetOpt.alias != "" {
flag.StringVar(targetOpt.ptr, targetOpt.alias, targetOpt.def, "")
}
name = targetOpt.name
}
c.cmdsMap[name] = opt
}
for _, opt := range opts {
opt(c)
}
return c
}
func WithUsageHeader(header string) cmdOption {
return func(c *baseCommand) {
c.usage = func() {
fmt.Fprintln(os.Stderr, header)
c.PrintDefaults()
os.Exit(0)
}
}
}
func (c *baseCommand) Usage() {
c.usage()
}
func (c *baseCommand) Ready() {
flag.Parse()
for _, opt := range c.cmdsMap {
switch targetOpt := opt.(type) {
case *boolCmd:
*c.cmdsMap[targetOpt.name].(*boolCmd).ptr = *targetOpt.ptr
case *stringCmd:
*c.cmdsMap[targetOpt.name].(*stringCmd).ptr = *targetOpt.ptr
}
}
}
func (c *baseCommand) GetBool(n cmdName) bool {
v := *c.cmdsMap[n].(*boolCmd).ptr
if c.isDebug {
log.Printf("GetBool() name = '%s', value = %+v", n, v)
}
return v
}
func (c *baseCommand) GetString(n cmdName) string {
v := *c.cmdsMap[n].(*stringCmd).ptr
if c.isDebug {
log.Printf("GetString() name = '%s', value = \"%+v\"", n, v)
}
return v
}
func (c *baseCommand) PrintDefaults() {
keys := make([]string, 0, len(c.cmdsMap))
for cn := range c.cmdsMap {
keys = append(keys, string(cn))
}
sort.Strings(keys)
for _, v := range keys {
opt := c.cmdsMap[cmdName(v)]
// Read default value from option
var defStr string
defaultStringReader, ok := opt.(defaultStringReader)
if ok {
defStr = defaultStringReader.DefaultString()
}
fmt.Printf(" %s\n", opt.Usage(defStr))
}
}
type defaultStringReader interface {
DefaultString() string
}
type cmdName string
type cmdable interface {
Usage(defStr string) string
}
type Option struct {
name cmdName
alias string
description string
}
func (c *Option) Name() cmdName {
return c.name
}
func (c *Option) Usage(defStr string) string {
if defStr == "" {
defStr = "<empty>"
}
name := fmt.Sprintf("-%s", string(c.name))
alias := ""
if c.alias != "" {
alias = fmt.Sprintf("-%s", c.alias)
}
return fmt.Sprintf(
"%s (default: %s)\n %s",
strings.Join([]string{name, alias}, ", "),
defStr,
c.description,
)
}
type boolCmd struct {
Option
def bool
ptr *bool
}
func (c *boolCmd) DefaultString() string {
if !c.def {
return "false"
}
return "true"
}
func NewBoolCommand(name cmdName, alias string, def bool, desc string) cmdable {
return &boolCmd{
Option: Option{
name: name,
alias: alias,
description: desc,
},
def: def,
ptr: new(bool),
}
}
type stringCmd struct {
Option
def string
ptr *string
}
func (c *stringCmd) DefaultString() string {
return c.def
}
func NewStringCommand(name cmdName, alias string, def string, desc string) cmdable {
return &stringCmd{
Option: Option{
name: name,
alias: alias,
description: desc,
},
def: def,
ptr: new(string),
}
}