/
args.go
95 lines (92 loc) · 1.85 KB
/
args.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
package utils
import (
"reflect"
"strconv"
"strings"
)
func Compact(cmd string) []string {
cmds := strings.Split(cmd, " ")
compactCmds := make([]string, 0)
for _, cmd := range cmds {
frag := strings.TrimSpace(cmd)
if len(frag) > 0 {
compactCmds = append(compactCmds, frag)
}
}
return compactCmds
}
func SimplePrase(cmds *[]string, argNames []string, data interface{}, firstVal bool) bool {
v := reflect.ValueOf(data)
if v.Kind() != reflect.Ptr {
return false
}
// get the value that the pointer v points to.
v = v.Elem()
if (firstVal && len(*cmds) < 1) || (!firstVal && len(*cmds) < 2) {
return false
}
ret := make([]string, 0)
valStr := ""
i := 0
c := ""
flag := false
for i, c = range *cmds {
for _, a := range argNames {
if a == c && i < len(*cmds)-1 {
flag = true
break
}
}
if flag {
break
}
}
if flag {
valStr = (*cmds)[i+1]
for _i, s := range *cmds {
if _i < i || _i > i+1 {
ret = append(ret, s)
}
}
} else if firstVal {
valStr = (*cmds)[0]
ret = (*cmds)[1:]
} else {
return false
}
switch v.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
realV, err := strconv.Atoi(valStr)
if err != nil {
return false
}
if v.OverflowInt(int64(realV)) {
return false
}
v.SetInt(int64(realV))
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
realV, err := strconv.Atoi(valStr)
if err != nil || realV < 0 {
return false
}
if v.OverflowUint(uint64(realV)) {
return false
}
v.SetUint(uint64(realV))
case reflect.Float32, reflect.Float64:
realV, err := strconv.ParseFloat(valStr, 64)
if err != nil {
return false
}
if v.OverflowFloat(realV) {
return false
}
v.SetFloat(realV)
case reflect.String:
v.SetString(valStr)
default:
return false
}
*cmds = ret
return true
}