-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgua.go
154 lines (132 loc) · 2.97 KB
/
gua.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
package gua
import (
"flag"
"fmt"
"os"
"reflect"
"strings"
"text/tabwriter"
"github.com/wrfly/ecp"
)
var splitter = "#|-_-|#" // tricky word
type gua struct {
m map[string]key
f *flag.FlagSet
}
func (g *gua) getKey(structure, field string, tag reflect.StructTag) string {
structure = strings.ToLower(structure)
field = strings.ToLower(field)
key := tag.Get("name")
if key == "-" { // ignore this key
return ""
}
if key == "" && field != "" {
if structure == "" {
key = field
} else {
key = structure + "." + field
}
}
desc := tag.Get("desc")
if desc != "" {
key += splitter + desc
}
return key
}
func (g *gua) getFlagValue(key string) (string, bool) {
key = strings.Split(key, splitter)[0]
f := g.m[key]
v := g.f.Lookup(f.name)
if v != nil {
str := v.Value.String()
str = strings.Trim(str, "\"")
return str, true
}
return "", false
}
type key struct {
name string
value *string
desc string
isBool bool
boolV *bool
}
// ParseWithNew use a fresh new flag set
func ParseWithNew(c interface{}, name string) error {
return ParseWithFlagSet(c, flag.NewFlagSet(name, flag.ExitOnError))
}
// ParseWithFlagSet can use a flag set you give
func ParseWithFlagSet(c interface{}, f *flag.FlagSet) error {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
w := tabwriter.NewWriter(
os.Stderr, 10, 4, 3, ' ',
tabwriter.StripEscape)
f.VisitAll(func(f *flag.Flag) {
f.DefValue = strings.Trim(f.DefValue, "\"")
var format string
switch {
case f.Usage == "" && f.DefValue == "":
format = fmt.Sprintf(" -%s", f.Name)
case f.Usage != "" && f.DefValue == "":
format = fmt.Sprintf(" -%s\t%s", f.Name, f.Usage)
case f.Usage == "" && f.DefValue != "":
format = fmt.Sprintf(" -%s\t[%s]", f.Name, f.DefValue)
case f.Usage != "" && f.DefValue != "":
format = fmt.Sprintf(" -%s\t%s\t[%s]", f.Name, f.Usage, f.DefValue)
}
w.Write([]byte(format + "\n"))
})
w.Flush()
}
f.Usage = flag.Usage
frog := gua{make(map[string]key, 20), f}
ecp := ecp.New()
ecp.BuildKey = frog.getKey
ecp.LookupValue = frog.getFlagValue
if err := ecp.Parse(c, ""); err != nil {
return err
}
for _, fullKey := range ecp.List(c, "") {
var (
value, desc string
isBool bool
)
x := strings.Split(fullKey, "=")
name := x[0]
if len(x) == 2 {
value = x[1]
}
// set bool flag
boolV, err := ecp.GetBool(c, name)
if err == nil {
isBool = true
}
xx := strings.Split(x[0], splitter)
if len(xx) >= 2 {
name = xx[0]
desc = xx[1]
}
frog.m[name] = key{
name: name,
value: &value,
desc: desc,
isBool: isBool,
boolV: &boolV,
}
}
for _, v := range frog.m {
if v.isBool {
v.boolV = f.Bool(v.name, *v.boolV, v.desc)
} else {
v.value = f.String(v.name, *v.value, v.desc)
}
}
f.Parse(os.Args[1:])
// parse from cli again
return ecp.Parse(c, "")
}
// Parse the structure
func Parse(c interface{}) error {
return ParseWithFlagSet(c, flag.CommandLine)
}