-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.go
358 lines (303 loc) · 10.2 KB
/
cli.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package flagconfig
import (
"bytes"
"fmt"
"github.com/mediocregopher/flagconfig/cla"
"github.com/mediocregopher/flagconfig/params"
"os"
"strings"
"github.com/mediocregopher/tablewriter"
)
type FlagConfig struct {
projname string
projdescr string
projpostdescr string
fullConfig []params.Param
pos []string
delim string
// if true, don't use configuration file at all
forceNoConfig bool
// if true and a config file is set but doesn't exist, continue and don't
// error
ignoreMissingConfig bool
// the file to use as a config file by default
defaultConfigFile string
// if true we don't print usage on Help()
dontPrintUsage bool
cliOnly map[string]bool
}
// New returns a FlagConfig struct. Usage of this struct is:
//* Tell it what params to look for with StrParam, IntParam, etc...
//* Call Parse()
//* Get found parameters using GetStr, GetInt, etc...
func New(projname string) *FlagConfig {
return &FlagConfig{
projname: projname,
fullConfig: make([]params.Param,0,8),
delim: "--",
cliOnly: map[string]bool{
"example": true,
"config": true,
"help": true,
},
}
}
// SetDescription sets the description which will be shown above the flag usage
// when --help is called on the command-line. This is optional.
func (f *FlagConfig) SetDescription(descr string) {
f.projdescr = descr
}
// SetExtraHelp sets a string which will show up at the tail end of the --help
// message, after all flags are defined. This is optional.
func (f *FlagConfig) SetExtraHelp(help string) {
f.projpostdescr = help
}
// Don't print a Usage line when printing help. Use this if you want to define
// your own usage line in SetDescription or SetExtraHelp.
func (f *FlagConfig) DontPrintUsage() {
f.dontPrintUsage = true
}
// Set the argument delimiter which identifies and argument as being a flag.
// Default is -- (--username, for example).
func (f *FlagConfig) SetDelimiter(delim string) {
f.delim = delim
}
// Sets the given field names to be cli-only. They will not be shown in the
// -example output. Can be called multiple times
func (f *FlagConfig) CLIOnly(fields ...string) {
for i := range fields {
f.cliOnly[fields[i]] = true
}
}
func (f *FlagConfig) get(name string) params.Param {
for _, param := range f.fullConfig {
if name == param.Name() {
return param
}
}
return nil
}
func (f *FlagConfig) add(p params.Param) {
f.fullConfig = append(f.fullConfig, p)
}
// IntParam tells flagconfig to look for a param called name of type int in
// either the config file or on the command line, or use the given default
// instead
func (f *FlagConfig) IntParam(name, descr string, def int) {
f.add(params.NewInt(name, descr, def, false))
}
// RequiredIntParam tells flagconfig to look for a param called name of type int
// in either the config file or on the command line, or return an error from
// Parse if it's not specified anywhere
func (f *FlagConfig) RequiredIntParam(name, descr string) {
f.add(params.NewInt(name, descr, 0, true))
}
// GetInt looks for a configuration parameter of the given name and
// returns its value (assuming the parameter is an integer)
func (f *FlagConfig) GetInt(name string) int {
return f.get(name).Value().(int)
}
// IntParams tells flagconfig to look for zero or more parameters of type int in
// either the config file or on the command line, or use the given default
// instead. If any are defined in one location they overwrite all from the
// other. For example, if there are three defined in the config file and one
// defined on the command-line, that one will be the only one in the returned
// value.
func (f *FlagConfig) IntParams(name, descr string, defaults ...int) {
f.add(params.NewInts(name, descr, defaults))
}
// GetInts looks for a configuration parameter of the given name and returns its
// value (assuming the parameter is a list of strings)
func (f *FlagConfig) GetInts(name string) []int {
return f.get(name).Value().([]int)
}
// StrParam tells flagconfig to look for a param called name of type string in
// either the config file or on the command line, or use the given default
// instead
func (f *FlagConfig) StrParam(name, descr, def string) {
f.add(params.NewString(name, descr, def, false))
}
// RequiredStrParam tells flagconfig to look for a param called name of type
// string in either the config file or on the command line, or return an error
// from Parse if it's not specified anywhere
func (f *FlagConfig) RequiredStrParam(name, descr string) {
f.add(params.NewString(name, descr, "", true))
}
// GetStr looks for a configuration parameter of the given name and
// returns its value (assuming the parameter is a string)
func (f *FlagConfig) GetStr(name string) string {
return f.get(name).Value().(string)
}
// StrParams tells flagconfig to look for zero or more parameters of type string
// in either the config file or on the command line, or use the given default
// instead. If any are defined in one location they overwrite all from the
// other. For example, if there are three defined in the config file and one
// defined on the command-line, that one will be the only one in the returned
// value.
func (f *FlagConfig) StrParams(name, descr string, defaults ...string) {
f.add(params.NewStrings(name, descr, defaults))
}
// GetStrs looks for a configuration parameter of the given name and returns its
// value (assuming the parameter is a list of strings)
func (f *FlagConfig) GetStrs(name string) []string {
return f.get(name).Value().([]string)
}
// FlagParam tells flagconfig to look for a param called name on the command
// line or in the config file. Passing the flag on the command-line indicates a
// value of whatever the opposite of the default is (so if the default is false,
// passing it on the command-line would mean true). In the configuration file
// the value can be either "true" or "false".
func (f *FlagConfig) FlagParam(name, descr string, def bool) {
f.add(params.NewFlag(name, descr, def))
}
// GetFlag looks for a configuration parameter of the given name and returns its
// value (assuming the parameter is a flag).
func (f *FlagConfig) GetFlag(name string) bool {
return f.get(name).Value().(bool)
}
// Parse loads flagconfig's runtime configuration, using both command-line
// arguments and a possible configuration file
func (f *FlagConfig) Parse() error {
f.FlagParam(
"help",
"Display help for parameters",
false,
)
if !f.forceNoConfig {
f.FlagParam(
"example",
"Dump example configuration to stdout and exit",
false,
)
f.StrParam(
"config",
"Configuration file to load, empty means don't load any file and"+
" only use command-line args",
f.defaultConfigFile,
)
}
claMap, pos := cla.Parse(f.delim, f.fullConfig)
f.pos = pos
_, printHelp := claMap["help"]
_, printExample := claMap["example"]
var configFile string
if configFiles, ok := claMap["config"]; ok && len(configFiles) > 0 {
configFile = configFiles[0]
} else if f.defaultConfigFile != "" {
configFile = f.defaultConfigFile
}
if printHelp {
fmt.Println(f.Help())
os.Exit(0)
}
//If the flag to dump example config is set to true, do that
if printExample {
fmt.Print(f.dumpExampleConfig(f.projname))
os.Exit(0)
}
if configFile != "" {
if configFile[:2] == "~/" {
// We don't use os/user because it's not supported by osx or
// windows, but this is (usually, I think)
home := os.Getenv("HOME")
if home == "" {
return fmt.Errorf("Could not determine home directory. Please set the $HOME environment variable")
}
configFile = strings.Replace(configFile, "~", home, 1)
}
configFileMap, err := readConfig(configFile)
if err != nil {
// configFileMap isn't nil when the file couldn't be opened
if !(configFileMap != nil && f.ignoreMissingConfig) {
return err
}
}
for _, param := range f.fullConfig {
if vals, ok := configFileMap[param.Name()]; ok {
for i := range vals {
param.ConfFile(vals[i])
}
}
}
}
for _, param := range f.fullConfig {
if vals, ok := claMap[param.Name()]; ok {
for i := range vals {
param.CLA(vals[i])
}
}
}
for _, param := range f.fullConfig {
if err := param.Post(); err != nil {
return fmt.Errorf(
"error in param %s: %s",
param.Name(),
err.Error(),
)
}
}
return nil
}
// Returns a formatted help string for the parameters that have been given so
// far
func (f *FlagConfig) Help() string {
buf := bytes.NewBuffer(make([]byte, 256))
if !f.dontPrintUsage {
fmt.Fprintf(buf, "Usage: %s [FLAGS]\n", os.Args[0])
}
if f.projdescr != "" {
fmt.Fprintf(buf, "%s\n", f.projdescr)
}
fmt.Fprintf(buf, "\n")
w := tablewriter.New(buf)
w.SetTableWidth(150)
w.SetBottomPadding(0)
w.AddColumn(-1, 25)
w.AddColumn(-1, 30)
w.AddColumn(0, -1)
fmtStr := "%s\t%s\t%s\n"
fmt.Fprintf(w, fmtStr, "Flag", "Default(s)", "Description")
fmt.Fprintf(w, fmtStr, "~~~~", "~~~~~~~~~~", "~~~~~~~~~~~")
for _, param := range f.fullConfig {
defj := "<required>"
if defs, ok := param.DefaultAsStrings(); ok {
defj = strings.Join(defs, ", ")
}
_, err := fmt.Fprintf(
w, fmtStr,
f.delim+param.Name(),
defj,
param.Description(),
)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR printing help: %s\n", err)
}
}
if f.projpostdescr != "" {
fmt.Fprintf(buf, "\n%s\n", f.projpostdescr)
}
return buf.String()
}
// If called then flagconfig won't have the ability to read in a configuration
// file. The --config and --example options won't be present in the --help menu.
func (f *FlagConfig) DisallowConfig() {
f.forceNoConfig = true
}
// If set, this will become the default value for the --config parameter. Note
// that this will still generate an error if it's set but the file doesn't
// exist. Use IgnoreMissingConfigFile to have this not be the case.
func (f *FlagConfig) SetDefaultConfigFile(file string) {
f.defaultConfigFile = file
}
// If called then flagconfig won't stop execution if it's given a configuration
// file to read but there isn't a file there. Instead it will act as if the file
// was empty.
func (f *FlagConfig) IgnoreMissingConfigFile() {
f.ignoreMissingConfig = true
}
// GetPositionals returns all positional arguments found after Parse'ing. Will
// return empty slice if none were found, and nil if Parse hasn't been called
// yet
func (f *FlagConfig) GetPositionals() []string {
return f.pos
}