-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
flag_generic.go
67 lines (55 loc) · 1.27 KB
/
flag_generic.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
package cli
type GenericFlag = FlagBase[Value, NoConfig, genericValue]
// -- Value Value
type genericValue struct {
val Value
}
// Below functions are to satisfy the ValueCreator interface
func (f genericValue) Create(val Value, p *Value, c NoConfig) Value {
*p = val
return &genericValue{
val: *p,
}
}
func (f genericValue) ToString(b Value) string {
if b != nil {
return b.String()
}
return ""
}
// Below functions are to satisfy the flag.Value interface
func (f *genericValue) Set(s string) error {
if f.val != nil {
return f.val.Set(s)
}
return nil
}
func (f *genericValue) Get() any {
if f.val != nil {
return f.val.Get()
}
return nil
}
func (f *genericValue) String() string {
if f.val != nil {
return f.val.String()
}
return ""
}
func (f *genericValue) IsBoolFlag() bool {
if f.val == nil {
return false
}
bf, ok := f.val.(boolFlag)
return ok && bf.IsBoolFlag()
}
// Generic looks up the value of a local GenericFlag, returns
// nil if not found
func (cmd *Command) Generic(name string) Value {
if v, ok := cmd.Value(name).(Value); ok {
tracef("generic available for flag name %[1]q with value=%[2]v (cmd=%[3]q)", name, v, cmd.Name)
return v
}
tracef("generic NOT available for flag name %[1]q (cmd=%[2]q)", name, cmd.Name)
return nil
}