-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecargs.go
149 lines (133 loc) · 3.35 KB
/
execargs.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
package arg
// ExecArgs represent the flag arguments passed to
// a Cmd's Exec function.
type ExecArgs interface {
// HasOption returns true if the option given by name is set.
HasOption(name string) bool
// GetOption gets the command option parameter given by name.
// Value is passed as pointer and can be any
// of the following types: string, bool, int64
// uint64, float64, []string, []int64, []uint64,
// or []float64.
// Returns false if the flag name does not exist
// or if the value type cannot be used.
GetOption(name string, value interface{}) bool
// GetOperand gets command operand value given by index.
// Values are passed as pointers and can be any
// of the following types: string, bool, int64
// uint64, or float64.
// Returns false if the argument index is out of range
// or if the value type cannot be used.
GetOperand(index int, value interface{}) bool
}
type execArgs struct {
longFlags map[string][]string
shortFlags map[rune][]string
longOpts map[string]struct{}
shortOpts map[rune]struct{}
operValues map[int]string
}
func newExecArgs() *execArgs {
ea := &execArgs{}
ea.longFlags = make(map[string][]string)
ea.shortFlags = make(map[rune][]string)
ea.longOpts = make(map[string]struct{})
ea.shortOpts = make(map[rune]struct{})
ea.operValues = make(map[int]string)
return ea
}
func (ea *execArgs) HasOption(name string) bool {
runes := getRunes(name)
if len(runes) == 0 {
return false
}
if len(runes) == 1 {
if _, ok := ea.shortOpts[runes[0]]; ok {
return true
}
}
if _, ok := ea.longOpts[name]; ok {
return true
}
return false
}
func (ea *execArgs) addFlag(short rune, long string, value string) {
ea.longFlags[long] = append(ea.longFlags[long], value)
ea.shortFlags[short] = append(ea.shortFlags[short], value)
}
func (ea *execArgs) setOption(short rune, long string) {
ea.shortOpts[short] = struct{}{}
ea.longOpts[long] = struct{}{}
}
func (ea *execArgs) setOperand(position int, value string) {
ea.operValues[position] = value
}
func (ea *execArgs) GetOperand(position int, val interface{}) bool {
argValue, exist := ea.operValues[position]
if !exist {
return false
}
var stat bool
switch v := val.(type) {
case *string:
stat = true
*v = argValue
case *bool:
stat = getBool(argValue, v)
case *int64:
stat = getInt64(argValue, v)
case *uint64:
stat = getUint64(argValue, v)
case *float64:
stat = getFloat64(argValue, v)
default:
stat = false
}
return stat
}
func (ea *execArgs) GetOption(name string, val interface{}) bool {
var flagExist bool
var flagValue []string
runes := getRunes(name)
if len(runes) == 0 {
return false
}
if len(runes) == 1 {
flagValue, flagExist = ea.shortFlags[runes[0]]
if !flagExist {
return false
}
}
if len(runes) > 1 {
flagValue, flagExist = ea.longFlags[name]
if !flagExist {
return false
}
}
var stat bool
switch v := val.(type) {
case *string:
stat = true
*v = flagValue[0]
case *bool:
stat = getBool(flagValue[0], v)
case *int64:
stat = getInt64(flagValue[0], v)
case *uint64:
stat = getUint64(flagValue[0], v)
case *float64:
stat = getFloat64(flagValue[0], v)
case *[]string:
stat = true
*v = flagValue
case *[]int64:
stat = getInt64Slice(flagValue, v)
case *[]uint64:
stat = getUint64Slice(flagValue, v)
case *[]float64:
stat = getFloat64Slice(flagValue, v)
default:
stat = false
}
return stat
}