Skip to content

Commit a701a59

Browse files
committed
chore: upgrade goNixArgParser
1 parent 4e9e758 commit a701a59

File tree

2 files changed

+174
-83
lines changed

2 files changed

+174
-83
lines changed

src/goNixArgParser/parseResult.go

+64-30
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,7 @@ func (r *ParseResult) GetBool(key string) (value bool, found bool) {
102102
return
103103
}
104104

105-
value, err := toBool(str)
106-
found = err == nil
107-
return
105+
return toBool(str)
108106
}
109107

110108
func (r *ParseResult) GetInt(key string) (value int, found bool) {
@@ -113,9 +111,34 @@ func (r *ParseResult) GetInt(key string) (value int, found bool) {
113111
return
114112
}
115113

116-
value, err := toInt(str)
117-
found = err == nil
118-
return
114+
return toInt(str)
115+
}
116+
117+
func (r *ParseResult) GetUint(key string) (value uint, found bool) {
118+
str, found := r.GetString(key)
119+
if !found {
120+
return
121+
}
122+
123+
return toUint(str)
124+
}
125+
126+
func (r *ParseResult) GetInt32(key string) (value int32, found bool) {
127+
str, found := r.GetString(key)
128+
if !found {
129+
return
130+
}
131+
132+
return toInt32(str)
133+
}
134+
135+
func (r *ParseResult) GetUint32(key string) (value uint32, found bool) {
136+
str, found := r.GetString(key)
137+
if !found {
138+
return
139+
}
140+
141+
return toUint32(str)
119142
}
120143

121144
func (r *ParseResult) GetInt64(key string) (value int64, found bool) {
@@ -124,9 +147,7 @@ func (r *ParseResult) GetInt64(key string) (value int64, found bool) {
124147
return
125148
}
126149

127-
value, err := toInt64(str)
128-
found = err == nil
129-
return
150+
return toInt64(str)
130151
}
131152

132153
func (r *ParseResult) GetUint64(key string) (value uint64, found bool) {
@@ -135,9 +156,7 @@ func (r *ParseResult) GetUint64(key string) (value uint64, found bool) {
135156
return
136157
}
137158

138-
value, err := toUint64(str)
139-
found = err == nil
140-
return
159+
return toUint64(str)
141160
}
142161

143162
func (r *ParseResult) GetFloat64(key string) (value float64, found bool) {
@@ -146,9 +165,7 @@ func (r *ParseResult) GetFloat64(key string) (value float64, found bool) {
146165
return
147166
}
148167

149-
value, err := toFloat64(str)
150-
found = err == nil
151-
return
168+
return toFloat64(str)
152169
}
153170

154171
// =============================
@@ -185,9 +202,7 @@ func (r *ParseResult) GetBools(key string) (values []bool, found bool) {
185202
return
186203
}
187204

188-
values, err := toBools(strs)
189-
found = err == nil
190-
return
205+
return toBools(strs)
191206
}
192207

193208
func (r *ParseResult) GetInts(key string) (values []int, found bool) {
@@ -196,9 +211,34 @@ func (r *ParseResult) GetInts(key string) (values []int, found bool) {
196211
return
197212
}
198213

199-
values, err := toInts(strs)
200-
found = err == nil
201-
return
214+
return toInts(strs)
215+
}
216+
217+
func (r *ParseResult) GetUints(key string) (values []uint, found bool) {
218+
strs, found := r.GetStrings(key)
219+
if !found {
220+
return
221+
}
222+
223+
return toUints(strs)
224+
}
225+
226+
func (r *ParseResult) GetInt32s(key string) (values []int32, found bool) {
227+
strs, found := r.GetStrings(key)
228+
if !found {
229+
return
230+
}
231+
232+
return toInt32s(strs)
233+
}
234+
235+
func (r *ParseResult) GetUint32s(key string) (values []uint32, found bool) {
236+
strs, found := r.GetStrings(key)
237+
if !found {
238+
return
239+
}
240+
241+
return toUint32s(strs)
202242
}
203243

204244
func (r *ParseResult) GetInt64s(key string) (values []int64, found bool) {
@@ -207,9 +247,7 @@ func (r *ParseResult) GetInt64s(key string) (values []int64, found bool) {
207247
return
208248
}
209249

210-
values, err := toInt64s(strs)
211-
found = err == nil
212-
return
250+
return toInt64s(strs)
213251
}
214252

215253
func (r *ParseResult) GetUint64s(key string) (values []uint64, found bool) {
@@ -218,9 +256,7 @@ func (r *ParseResult) GetUint64s(key string) (values []uint64, found bool) {
218256
return
219257
}
220258

221-
values, err := toUint64s(strs)
222-
found = err == nil
223-
return
259+
return toUint64s(strs)
224260
}
225261

226262
func (r *ParseResult) GetFloat64s(key string) (values []float64, found bool) {
@@ -229,9 +265,7 @@ func (r *ParseResult) GetFloat64s(key string) (values []float64, found bool) {
229265
return
230266
}
231267

232-
values, err := toFloat64s(strs)
233-
found = err == nil
234-
return
268+
return toFloat64s(strs)
235269
}
236270

237271
func (r *ParseResult) GetRests() (rests []string) {

src/goNixArgParser/util.go

+110-53
Original file line numberDiff line numberDiff line change
@@ -40,102 +40,159 @@ func stringToSlice(input string) []string {
4040
return []string{input}
4141
}
4242

43-
func toBool(input string) (bool, error) {
43+
func toBool(input string) (bool, bool) {
4444
if len(input) == 0 {
45-
return false, nil
45+
return false, false
4646
}
47-
return strconv.ParseBool(input)
47+
v, err := strconv.ParseBool(input)
48+
return v, err == nil
4849
}
4950

50-
func toBools(input []string) ([]bool, error) {
51-
inputLen := len(input)
52-
53-
output := make([]bool, inputLen)
54-
for i, l := 0, inputLen; i < l; i++ {
55-
v, err := toBool(input[i])
56-
if err != nil {
57-
return nil, err
51+
func toBools(input []string) ([]bool, bool) {
52+
l := len(input)
53+
output := make([]bool, l)
54+
for i := 0; i < l; i++ {
55+
v, ok := toBool(input[i])
56+
if !ok {
57+
return nil, false
5858
}
5959
output[i] = v
6060
}
6161

62-
return output, nil
62+
return output, true
6363
}
6464

65-
func toInt(input string) (int, error) {
66-
return strconv.Atoi(input)
65+
func toInt(input string) (int, bool) {
66+
v, err := strconv.Atoi(input)
67+
return v, err == nil
6768
}
6869

69-
func toInts(input []string) ([]int, error) {
70-
inputLen := len(input)
71-
72-
output := make([]int, inputLen)
73-
for i, l := 0, inputLen; i < l; i++ {
74-
v, err := toInt(input[i])
75-
if err != nil {
76-
return nil, err
70+
func toInts(input []string) ([]int, bool) {
71+
l := len(input)
72+
output := make([]int, l)
73+
for i := 0; i < l; i++ {
74+
v, ok := toInt(input[i])
75+
if !ok {
76+
return nil, false
7777
}
7878
output[i] = v
7979
}
8080

81-
return output, nil
81+
return output, true
82+
}
83+
84+
func toUint(input string) (uint, bool) {
85+
v, err := strconv.ParseUint(input, 10, 0)
86+
return uint(v), err == nil
8287
}
8388

84-
func toInt64(input string) (int64, error) {
85-
return strconv.ParseInt(input, 10, 64)
89+
func toUints(input []string) ([]uint, bool) {
90+
l := len(input)
91+
output := make([]uint, l)
92+
for i := 0; i < l; i++ {
93+
v, ok := toUint(input[i])
94+
if !ok {
95+
return nil, false
96+
}
97+
output[i] = v
98+
}
99+
100+
return output, true
86101
}
87102

88-
func toInt64s(input []string) ([]int64, error) {
89-
inputLen := len(input)
103+
func toInt32(input string) (int32, bool) {
104+
v, err := strconv.ParseInt(input, 10, 32)
105+
return int32(v), err == nil
106+
}
90107

91-
output := make([]int64, inputLen)
92-
for i, l := 0, inputLen; i < l; i++ {
93-
v, err := toInt64(input[i])
94-
if err != nil {
95-
return nil, err
108+
func toInt32s(input []string) ([]int32, bool) {
109+
l := len(input)
110+
output := make([]int32, l)
111+
for i := 0; i < l; i++ {
112+
v, ok := toInt32(input[i])
113+
if !ok {
114+
return nil, false
96115
}
97116
output[i] = v
98117
}
99118

100-
return output, nil
119+
return output, true
101120
}
102121

103-
func toUint64(input string) (uint64, error) {
104-
return strconv.ParseUint(input, 10, 64)
122+
func toUint32(input string) (uint32, bool) {
123+
v, err := strconv.ParseUint(input, 10, 32)
124+
return uint32(v), err == nil
105125
}
106126

107-
func toUint64s(input []string) ([]uint64, error) {
108-
inputLen := len(input)
127+
func toUint32s(input []string) ([]uint32, bool) {
128+
l := len(input)
129+
output := make([]uint32, l)
130+
for i := 0; i < l; i++ {
131+
v, ok := toUint32(input[i])
132+
if !ok {
133+
return nil, false
134+
}
135+
output[i] = v
136+
}
109137

110-
output := make([]uint64, inputLen)
111-
for i, l := 0, inputLen; i < l; i++ {
112-
v, err := toUint64(input[i])
113-
if err != nil {
114-
return nil, err
138+
return output, true
139+
}
140+
141+
func toInt64(input string) (int64, bool) {
142+
v, err := strconv.ParseInt(input, 10, 64)
143+
return v, err == nil
144+
}
145+
146+
func toInt64s(input []string) ([]int64, bool) {
147+
l := len(input)
148+
output := make([]int64, l)
149+
for i := 0; i < l; i++ {
150+
v, ok := toInt64(input[i])
151+
if !ok {
152+
return nil, false
115153
}
116154
output[i] = v
117155
}
118156

119-
return output, nil
157+
return output, true
120158
}
121159

122-
func toFloat64(input string) (float64, error) {
123-
return strconv.ParseFloat(input, 64)
160+
func toUint64(input string) (uint64, bool) {
161+
v, err := strconv.ParseUint(input, 10, 64)
162+
return v, err == nil
124163
}
125164

126-
func toFloat64s(input []string) ([]float64, error) {
127-
inputLen := len(input)
165+
func toUint64s(input []string) ([]uint64, bool) {
166+
l := len(input)
167+
output := make([]uint64, l)
168+
for i := 0; i < l; i++ {
169+
v, ok := toUint64(input[i])
170+
if !ok {
171+
return nil, false
172+
}
173+
output[i] = v
174+
}
175+
176+
return output, true
177+
}
178+
179+
func toFloat64(input string) (float64, bool) {
180+
v, err := strconv.ParseFloat(input, 64)
181+
return v, err == nil
182+
}
128183

129-
output := make([]float64, inputLen)
130-
for i, l := 0, inputLen; i < l; i++ {
131-
v, err := toFloat64(input[i])
132-
if err != nil {
133-
return nil, err
184+
func toFloat64s(input []string) ([]float64, bool) {
185+
l := len(input)
186+
output := make([]float64, l)
187+
for i := 0; i < l; i++ {
188+
v, ok := toFloat64(input[i])
189+
if !ok {
190+
return nil, false
134191
}
135192
output[i] = v
136193
}
137194

138-
return output, nil
195+
return output, true
139196
}
140197

141198
func contains(collection []string, find string) bool {

0 commit comments

Comments
 (0)