-
Notifications
You must be signed in to change notification settings - Fork 526
/
base.go
46 lines (38 loc) · 871 Bytes
/
base.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
package helper
import (
"flag"
"github.com/mitchellh/cli"
)
type FlagDefiner interface {
DefineFlags(map[string]FlagDescriptor)
}
type FlagSetter interface {
FlagSet(*flag.FlagSet)
}
type CommandResult interface {
// Return result for stdout
Output() string
}
// Base has common fields for each command
type Base struct {
UI cli.Ui
FlagMap map[string]FlagDescriptor
}
// DefineFlags initializes and defines the common command flags
func (c *Base) DefineFlags(ds ...FlagDefiner) {
if c.FlagMap == nil {
// Flag map not initialized
c.FlagMap = make(map[string]FlagDescriptor)
}
for _, d := range ds {
d.DefineFlags(c.FlagMap)
}
}
// FlagSet initializes Flag Set
func (m *Base) NewFlagSet(n string, ss ...FlagSetter) *flag.FlagSet {
flag := flag.NewFlagSet(n, flag.ContinueOnError)
for _, s := range ss {
s.FlagSet(flag)
}
return flag
}