-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflag.go
51 lines (45 loc) · 1.52 KB
/
flag.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
package dynflags
type FlagType string
const (
FlagTypeStringSlice FlagType = "..STRINGs"
FlagTypeString FlagType = "STRING"
FlagTypeInt FlagType = "INT"
FlagTypeIntSlice FlagType = "..INTs"
FlagTypeBool FlagType = "BOOL"
FlagTypeBoolSlice FlagType = "..BOOLs"
FlagTypeDuration FlagType = "DURATION"
FlagTypeDurationSlice FlagType = "..DURATIONs"
FlagTypeFloat FlagType = "FLOAT"
FlagTypeFloatSlice FlagType = "..FLOATs"
FlagTypeIP FlagType = "IP"
FlagTypeIPSlice FlagType = "..IPs"
FlagTypeURL FlagType = "URL"
FlagTypeURLSlice FlagType = "..URLs"
)
// Flag represents a single configuration flag
type Flag struct {
Default interface{} // Default value for the flag
Type FlagType // Type of the flag
Usage string // Description for usage
metaVar string // MetaVar for flag
value FlagValue // Encapsulated parsing and value-setting logic
}
func (f *Flag) MetaVar(metaVar string) {
f.metaVar = metaVar
}
// FlagValue interface encapsulates parsing and value-setting logic
type FlagValue interface {
// Parse parses the given string value into the flag's value type
Parse(value string) (interface{}, error)
// Set sets the flag's value to the given value
Set(value interface{}) error
// GetBound returns the bound value of the flag.
GetBound() interface{}
}
// Value returns the current value of the flag.
func (f *Flag) GetValue() interface{} {
if f == nil || f.value == nil {
return nil
}
return f.value.GetBound()
}