/
genFlag.go
107 lines (83 loc) · 2.07 KB
/
genFlag.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
package screw
import (
"bytes"
"fmt"
"go/format"
"strings"
)
func genStructName(k string) string {
return k + "AutoGen"
}
func genVarName(varName string) string {
return varName + "Var"
}
// Generate the structure according to the resolved function name and parameters
func genStructBytes(p *ParseFlag) ([]byte, error) {
var code bytes.Buffer
var allCode bytes.Buffer
for k, funcAndArgs := range p.funcAndArgs {
v := funcAndArgs
if !v.haveParseFunc {
continue
}
if p.haveImportPath {
code.WriteString(`
package main
import (
"github.com/fungolang/screw"
)
`)
}
if !p.haveStruct {
continue
}
code.WriteString(fmt.Sprintf("type %s struct{", genStructName(k)))
for _, arg := range v.args {
//The option name is important. If there is no option name, it will not be generated
if len(arg.optName) == 0 || len(arg.varName) == 0 {
continue
}
//Write field name and type name
varName := arg.varName
if varName[0] >= 'a' && varName[0] <= 'z' {
varName = string(varName[0]-'a'+'A') + varName[1:]
}
code.WriteString(fmt.Sprintf("%s %s", varName, arg.typeName))
var screwTag bytes.Buffer
//Write Option Name
if len(arg.optName) > 0 {
screwTag.WriteString("`screw:\"")
numMinuses := "-"
if len(arg.optName) > 1 {
numMinuses = "--"
}
screwTag.WriteString(fmt.Sprintf("%s%s\" ", numMinuses, arg.optName))
}
//Write Default
if len(arg.defVal) > 0 {
screwTag.WriteString(fmt.Sprintf("default:\"%s\" ", arg.defVal))
}
//Write help information
if len(arg.usage) > 0 {
screwTag.WriteString(fmt.Sprintf("usage:\"%s\" `\n", arg.usage))
}
code.WriteString(screwTag.String())
}
code.WriteString("}")
if p.haveMain {
varName := strings.ToLower(k)
code.WriteString(fmt.Sprintf(`
func main() {
var %s %s
screw.Bind(&%s)
}`, genVarName(varName), genStructName(k), genVarName(varName)))
}
fmtCode, err := format.Source(code.Bytes())
if err != nil {
return nil, err
}
allCode.Write(fmtCode)
code.Reset()
}
return allCode.Bytes(), nil
}