forked from anchore/fangs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvar.go
175 lines (147 loc) · 3.32 KB
/
var.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
package fangs
import (
"strconv"
"strings"
"github.com/spf13/pflag"
)
// boolPtr is a pointer to a bool pointer field within a struct
type boolPtr struct {
value **bool // consistent name with other pflag.Value types so FieldByName finds it
}
func (b *boolPtr) String() string {
if b.value == nil {
return ""
}
if *b.value == nil {
return ""
}
if **b.value {
return "true"
}
return "false"
}
func (b *boolPtr) Set(s string) error {
if s == "" {
*b.value = nil
return nil
}
b1, err := strconv.ParseBool(s)
if err == nil {
*b.value = &b1
}
return nil
}
func (b *boolPtr) Type() string {
return "*bool"
}
var _ pflag.Value = (*boolPtr)(nil)
// BoolPtrVarP adds a boolean pointer flag with no default
func BoolPtrVarP(flags *pflag.FlagSet, ptr **bool, name string, short string, usage string) {
flag := flags.VarPF(&boolPtr{
value: ptr,
}, name, short, usage)
if *ptr == nil || !**ptr {
flag.NoOptDefVal = "true"
} else {
flag.NoOptDefVal = "false"
}
}
// stringPtr is a pointer to a string pointer field within a struct
type stringPtr struct {
value **string // consistent name with other pflag.Value types so FieldByName finds it
}
func (b *stringPtr) String() string {
if b.value == nil {
return ""
}
if *b.value == nil {
return ""
}
return **b.value
}
func (b *stringPtr) Set(s string) error {
if s == "" {
*b.value = nil
return nil
}
*b.value = &s
return nil
}
func (b *stringPtr) Type() string {
return "*string"
}
var _ pflag.Value = (*stringPtr)(nil)
// StringPtrVarP adds a string pointer flag with no default
func StringPtrVarP(flags *pflag.FlagSet, ptr **string, name string, short string, usage string) {
flags.VarP(&stringPtr{
value: ptr,
}, name, short, usage)
}
// intPtr is a pointer to an int pointer field within a struct
type intPtr struct {
value **int // consistent name with other pflag.Value types so FieldByName finds it
}
func (b *intPtr) String() string {
if b.value == nil {
return ""
}
if *b.value == nil {
return ""
}
return strconv.Itoa(**b.value)
}
func (b *intPtr) Set(s string) error {
if s == "" {
*b.value = nil
return nil
}
v, err := strconv.Atoi(s)
if err == nil {
*b.value = &v
}
return nil
}
func (b *intPtr) Type() string {
return "*int"
}
var _ pflag.Value = (*intPtr)(nil)
// IntPtrVarP adds an int pointer flag with no default
func IntPtrVarP(flags *pflag.FlagSet, ptr **int, name string, short string, usage string) {
flags.VarP(&intPtr{
value: ptr,
}, name, short, usage)
}
// intPtr is a pointer to a float64 pointer field within a struct
type float64Ptr struct {
value **float64 // consistent name with other pflag.Value types so FieldByName finds it
}
func (b *float64Ptr) String() string {
if b.value == nil {
return ""
}
if *b.value == nil {
return ""
}
return strconv.FormatFloat(**b.value, 'f', -1, 64)
}
func (b *float64Ptr) Set(s string) error {
if s == "" {
*b.value = nil
return nil
}
v, err := strconv.ParseFloat(strings.TrimSpace(s), 64)
if err == nil {
*b.value = &v
}
return nil
}
func (b *float64Ptr) Type() string {
return "*float64"
}
var _ pflag.Value = (*float64Ptr)(nil)
// Float64PtrVarP adds a float64 pointer flag with no default
func Float64PtrVarP(flags *pflag.FlagSet, ptr **float64, name string, short string, usage string) {
flags.VarP(&float64Ptr{
value: ptr,
}, name, short, usage)
}