-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflag_var.go
More file actions
196 lines (184 loc) · 3.82 KB
/
Copy pathflag_var.go
File metadata and controls
196 lines (184 loc) · 3.82 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package flag
import (
"fmt"
"os"
"strconv"
"strings"
"time"
)
// FlagVar 定义一个支持多来源设置的标志变量
type FlagVar struct {
// Value 存储实际值的指针,确保可以通过反射修改其内容
Value interface{}
// Name 是用于命令行参数解析的名字,例如 "--port"
Name string
// Env 是对应的环境变量名,例如 "APP_PORT"
Env string
// ConfigSection 表示在配置文件中的节标题,例如 "[server]"
ConfigSection string
// ConfigKey 表示在配置文件中的键名,例如 "port"
ConfigKey string
// Usage 提供帮助文档描述此参数的作用
Usage string
// Hidden 控制该参数是否出现在 --help 输出中
Hidden bool
// DefaultValue 设置默认值,当所有来源都没有指定时使用
DefaultValue interface{}
}
func anyToBool(value any) bool {
switch data := value.(type) {
case bool:
return data
case int:
return data != 0
case int64:
return data != 0
case uint:
return data != 0
case uint64:
return data != 0
case string:
return strings.ToLower(data) == "true"
}
return false
}
func anyToString(value any) string {
switch data := value.(type) {
case string:
return data
case int:
return fmt.Sprintf("%d", data)
case int64:
return fmt.Sprintf("%d", data)
case uint:
return fmt.Sprintf("%d", data)
case uint64:
return fmt.Sprintf("%d", data)
case bool:
return fmt.Sprintf("%t", data)
}
return ""
}
func anyToInt(value any) int {
switch data := value.(type) {
case int:
return data
case int64:
return int(data)
case uint:
return int(data)
case uint64:
return int(data)
case string:
i, _ := strconv.Atoi(data)
return i
}
return 0
}
func anyToInt64(value any) int64 {
switch data := value.(type) {
case int:
return int64(data)
case int64:
return data
case uint:
return int64(data)
case uint64:
return int64(data)
case string:
i, _ := strconv.ParseInt(data, 10, 64)
return i
}
return 0
}
func anyToUint(value any) uint {
switch data := value.(type) {
case int:
return uint(data)
case int64:
return uint(data)
case uint:
return data
case uint64:
return uint(data)
case string:
i, _ := strconv.ParseUint(data, 10, 64)
return uint(i)
}
return 0
}
func anyToUint64(value any) uint64 {
switch data := value.(type) {
case int:
return uint64(data)
case int64:
return uint64(data)
case uint:
return uint64(data)
case uint64:
return data
case string:
i, _ := strconv.ParseUint(data, 10, 64)
return i
}
return 0
}
func anyToFloat64(value any) float64 {
switch data := value.(type) {
case float64:
return data
case int:
return float64(data)
case string:
f, _ := strconv.ParseFloat(data, 64)
return f
}
return 0
}
func anyToTimeDuration(value any) time.Duration {
switch data := value.(type) {
case time.Duration:
return data
case string:
d, _ := time.ParseDuration(data)
return d
}
return 0
}
func (f *FlagSet) Var(opt *FlagVar) {
if opt.Name == "" && opt.ConfigSection != "" && opt.ConfigKey != "" {
opt.Name = opt.ConfigSection + "-" + opt.ConfigKey
}
if opt.ConfigKey == "" && opt.ConfigSection == "" {
info := strings.Split(opt.Name, "-")
if len(info) >= 2 {
opt.ConfigSection = info[0]
opt.ConfigKey = strings.Join(info[0:], "-")
} else {
opt.ConfigSection = "config"
opt.ConfigKey = opt.Name
}
}
if opt.ConfigKey == "" && opt.ConfigSection != "" {
opt.ConfigKey = opt.Name
}
value, err := newReflectValue(opt.Value, opt.DefaultValue)
if err != nil {
f.handleError(err)
return
}
f.VarFlag(value, opt.Name, opt.ConfigSection, opt.ConfigKey, opt.Env, opt.Hidden, opt.Usage)
}
func (f *FlagSet) handleError(err error) {
switch f.errorHandling {
case ContinueOnError:
fmt.Fprintln(f.out(), err)
case ExitOnError:
fmt.Fprintln(f.out(), err)
os.Exit(2)
case PanicOnError:
panic(err)
default:
panic(err)
}
}