/
plugin.go
65 lines (58 loc) · 2.01 KB
/
plugin.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
package plugin
import (
"fmt"
"go.uber.org/multierr"
)
type flag struct {
groupName string
longDescription string
data interface{}
}
// stores the list of currently registered flags
var _flags []*flag
// AddFlags should be used by embedded modules to inject custom flags into `yab`.
// It adds a set of custom flags with heading `groupName`.
//
// The `data` argument should be a pointer to a struct with one field for each
// command line flag. Each field should use tags like `description` to inform
// the parser of metadata about the flag. (tags are the same as supported by
// github.com/jessevdk/go-flags)
//
// type foo struct {
// Bar string `long:"bar" description:"Sets the 'bar' value."`
// }
//
// AddFlags("Foo Options", "", &foo{})
//
// This would inject a custom flag group that looks like:
//
// Foo Options
// --bar Sets the 'bar' value.
//
// In order to retrieve the mutated results of the set flag, users of AddFlags() should
// retain a reference to the `data` object and check its values after parsing is complete.
func AddFlags(groupName string, longDescription string, data interface{}) {
_flags = append(_flags, &flag{
groupName: groupName,
longDescription: longDescription,
data: data,
})
}
// Parser is any object that can add flag groups to itself before performing its parse.
type Parser interface {
// AddFlagGroup adds an additional flag group to process during parsing.
AddFlagGroup(groupName, longDescription string, data interface{}) error
}
// AddToParser adds all registered flags to the passed Parser.
// This operation is not atomic, flags are applied on a best-effort basis (not "all-or-nothing")
// Returns a slice of errors indicating which flags groups failed to be added.
func AddToParser(p Parser) error {
var err error
for _, f := range _flags {
flagErr := p.AddFlagGroup(f.groupName, f.longDescription, f.data)
if flagErr != nil {
err = multierr.Append(err, fmt.Errorf("adding %v to parser: %v", f.groupName, flagErr))
}
}
return err
}