-
Notifications
You must be signed in to change notification settings - Fork 10
/
pflag.go
258 lines (214 loc) · 4.75 KB
/
pflag.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package clop
import (
"go/ast"
"go/parser"
"go/token"
"strconv"
)
// flag库的解析函数名, 白名称
type argsNumAndType struct {
size int
typeName string
}
var funcName = map[string]argsNumAndType{
//"Func":true, TODO
"Bool": {3, "bool"},
"BoolVar": {4, "bool"},
"Duration": {3, "time.Duration"},
"DurationVar": {4, "time.Duration"},
"Float64": {3, "float64"},
"Float64Var": {4, "float64"},
"Int": {3, "int"},
"IntVar": {4, "int"},
"Int64": {3, "int64"},
"Int64Var": {4, "int64"},
"String": {3, "string"},
"StringVar": {4, "string"},
"Uint": {3, "uint"},
"UintVar": {4, "uint"},
"Uint64": {3, "uint64"},
"Uint64Var": {4, "uint64"},
}
// 解析flag
type ParseFlag struct {
astFile *ast.File
fileName string
funcAndArgs map[string]funcAndArgs
haveStruct bool
haveImportPath bool
haveMain bool
}
// 构造函数
func NewParseFlag() *ParseFlag {
return &ParseFlag{}
}
// 仅生成struct
func (p *ParseFlag) OnlyStruct() *ParseFlag {
p.haveStruct = true
return p
}
// 生成所有
func (p *ParseFlag) All() *ParseFlag {
p.haveStruct = true
p.haveImportPath = true
p.haveMain = true
return p
}
// 设置文件名
func (p *ParseFlag) FromFile(fileName string) *ParseFlag {
p.fileName = fileName
return p
}
// 解析
func (p *ParseFlag) Parse() ([]byte, error) {
p.funcAndArgs = make(map[string]funcAndArgs)
if err := p.getFuncCallsToken(); err != nil {
return nil, err
}
return genStructBytes(p)
}
// 每个flag库调用的地址都解析成 funcAndArgs结构
type funcAndArgs struct {
args []flagOpt
haveParseFunc bool
}
// 保存从ast里面提取出来的元数据
type flagOpt struct {
varName string
optName string
defVal string
usage string
typeName string
}
// 可以判断是你要的函数, 比如flag.String
func isFunc(expr ast.Expr, pkg, fn string) bool {
f, ok := expr.(*ast.SelectorExpr)
return ok && isIdent(f.X, pkg) && isIdent(f.Sel, fn)
}
func getPtrArgName(arg ast.Expr) string {
a, ok := arg.(*ast.UnaryExpr)
if !ok {
return ""
}
return getIdentName(a.X)
}
// 获取函数名
func getArgName(arg ast.Expr) string {
a, ok := arg.(*ast.BasicLit)
if !ok {
return ""
}
return a.Value
}
// 提取函数名和形参
func (p *ParseFlag) takeFuncNameAndArgs(expr ast.Expr, args []ast.Expr) (err error) {
f, ok := expr.(*ast.SelectorExpr)
if !ok {
return nil
}
obj := getIdentName(f.X)
fn := getIdentName(f.Sel)
argsNumType, ok := funcName[fn]
if !ok {
if fn == "Parse" {
if v, ok := p.funcAndArgs[obj]; ok {
v.haveParseFunc = true
p.funcAndArgs[obj] = v
}
}
return nil
}
if _, ok := p.funcAndArgs[obj]; !ok {
p.funcAndArgs[obj] = funcAndArgs{}
}
if argsNumType.size != len(args) {
return nil
}
var opt flagOpt
strArgs := make([]string, len(args))
for i := range args {
arg := ""
arg2 := ""
if len(strArgs) == 4 && i == 0 {
arg = getPtrArgName(args[i])
goto next
}
arg = getArgName(args[i])
next:
arg2, err = strconv.Unquote(arg)
if err != nil {
arg2 = arg
//return err
}
strArgs[i] = arg2
}
if argsNumType.size == 3 {
opt.varName = obj
opt.optName = strArgs[0]
opt.defVal = strArgs[1]
opt.usage = strArgs[2]
} else {
opt.varName = strArgs[0]
opt.optName = strArgs[1]
opt.defVal = strArgs[2]
opt.usage = strArgs[3]
}
opt.typeName = argsNumType.typeName
oldVal := p.funcAndArgs[obj]
oldVal.args = append(oldVal.args, opt)
p.funcAndArgs[obj] = oldVal
return nil
}
func isIdent(expr ast.Expr, name string) bool {
ident, ok := expr.(*ast.Ident)
return ok && ident.Name == name
}
func getIdentName(expr ast.Expr) string {
ident, ok := expr.(*ast.Ident)
if ok {
return ident.Name
}
return ""
}
// 解析flag.NewFlagSet代码
func (p *ParseFlag) parserFlagNewFlagSet(stmt *ast.AssignStmt) {
if (stmt.Tok == token.ASSIGN || stmt.Tok == token.DEFINE) && len(stmt.Rhs) > 0 {
if isFunc(stmt.Rhs[0], "flag", "NewFlagSet") && len(stmt.Lhs) > 0 {
name := getIdentName(stmt.Lhs[0])
if len(name) == 0 {
return
}
p.funcAndArgs[name] = funcAndArgs{}
}
}
}
// 解析函数调用的代码
func (p *ParseFlag) findFuncCalls(node ast.Node) bool {
stmt, ok := node.(*ast.AssignStmt)
if ok {
p.parserFlagNewFlagSet(stmt)
return true
}
call, ok := node.(*ast.CallExpr)
if !ok {
return true
}
err := p.takeFuncNameAndArgs(call.Fun, call.Args)
if err != nil {
// debug
//panic(err.Error())
}
return true
}
// 获取函数和形参
func (p *ParseFlag) getFuncCallsToken() (err error) {
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, p.fileName, nil, 0)
if err != nil {
return err
}
p.funcAndArgs["flag"] = funcAndArgs{}
p.astFile = f
ast.Inspect(p.astFile, p.findFuncCalls)
return nil
}