Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ad08d47

Browse files
committedApr 26, 2025
chore: upgrade goNixArgParser
1 parent 4e9e758 commit ad08d47

File tree

2 files changed

+138
-66
lines changed

2 files changed

+138
-66
lines changed
 

‎src/goNixArgParser/parseResult.go

+37-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,7 @@ 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)
202215
}
203216

204217
func (r *ParseResult) GetInt64s(key string) (values []int64, found bool) {
@@ -207,9 +220,7 @@ func (r *ParseResult) GetInt64s(key string) (values []int64, found bool) {
207220
return
208221
}
209222

210-
values, err := toInt64s(strs)
211-
found = err == nil
212-
return
223+
return toInt64s(strs)
213224
}
214225

215226
func (r *ParseResult) GetUint64s(key string) (values []uint64, found bool) {
@@ -218,9 +229,7 @@ func (r *ParseResult) GetUint64s(key string) (values []uint64, found bool) {
218229
return
219230
}
220231

221-
values, err := toUint64s(strs)
222-
found = err == nil
223-
return
232+
return toUint64s(strs)
224233
}
225234

226235
func (r *ParseResult) GetFloat64s(key string) (values []float64, found bool) {
@@ -229,9 +238,7 @@ func (r *ParseResult) GetFloat64s(key string) (values []float64, found bool) {
229238
return
230239
}
231240

232-
values, err := toFloat64s(strs)
233-
found = err == nil
234-
return
241+
return toFloat64s(strs)
235242
}
236243

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

‎src/goNixArgParser/util.go

+101-36
Original file line numberDiff line numberDiff line change
@@ -40,102 +40,167 @@ 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+
func toBools(input []string) ([]bool, bool) {
5152
inputLen := len(input)
5253

5354
output := make([]bool, inputLen)
5455
for i, l := 0, inputLen; i < l; i++ {
55-
v, err := toBool(input[i])
56-
if err != nil {
57-
return nil, err
56+
v, ok := toBool(input[i])
57+
if !ok {
58+
return nil, false
5859
}
5960
output[i] = v
6061
}
6162

62-
return output, nil
63+
return output, true
6364
}
6465

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

69-
func toInts(input []string) ([]int, error) {
71+
func toInts(input []string) ([]int, bool) {
7072
inputLen := len(input)
7173

7274
output := make([]int, inputLen)
7375
for i, l := 0, inputLen; i < l; i++ {
74-
v, err := toInt(input[i])
75-
if err != nil {
76-
return nil, err
76+
v, ok := toInt(input[i])
77+
if !ok {
78+
return nil, false
7779
}
7880
output[i] = v
7981
}
8082

81-
return output, nil
83+
return output, true
8284
}
8385

84-
func toInt64(input string) (int64, error) {
85-
return strconv.ParseInt(input, 10, 64)
86+
func toUint(input string) (uint, bool) {
87+
v, err := strconv.ParseUint(input, 10, 0)
88+
return uint(v), err == nil
8689
}
8790

88-
func toInt64s(input []string) ([]int64, error) {
91+
func toUints(input []string) ([]uint, bool) {
92+
inputLen := len(input)
93+
94+
output := make([]uint, inputLen)
95+
for i, l := 0, inputLen; i < l; i++ {
96+
v, ok := toUint(input[i])
97+
if !ok {
98+
return nil, false
99+
}
100+
output[i] = v
101+
}
102+
103+
return output, true
104+
}
105+
106+
func toInt32(input string) (int32, bool) {
107+
v, err := strconv.ParseInt(input, 10, 32)
108+
return int32(v), err == nil
109+
}
110+
111+
func toInt32s(input []string) ([]int32, bool) {
112+
inputLen := len(input)
113+
114+
output := make([]int32, inputLen)
115+
for i, l := 0, inputLen; i < l; i++ {
116+
v, ok := toInt32(input[i])
117+
if !ok {
118+
return nil, false
119+
}
120+
output[i] = v
121+
}
122+
123+
return output, true
124+
}
125+
126+
func toUint32(input string) (uint32, bool) {
127+
v, err := strconv.ParseUint(input, 10, 32)
128+
return uint32(v), err == nil
129+
}
130+
131+
func toUint32s(input []string) ([]uint32, bool) {
132+
inputLen := len(input)
133+
134+
output := make([]uint32, inputLen)
135+
for i, l := 0, inputLen; i < l; i++ {
136+
v, ok := toUint32(input[i])
137+
if !ok {
138+
return nil, false
139+
}
140+
output[i] = v
141+
}
142+
143+
return output, true
144+
}
145+
146+
func toInt64(input string) (int64, bool) {
147+
v, err := strconv.ParseInt(input, 10, 64)
148+
return v, err == nil
149+
}
150+
151+
func toInt64s(input []string) ([]int64, bool) {
89152
inputLen := len(input)
90153

91154
output := make([]int64, inputLen)
92155
for i, l := 0, inputLen; i < l; i++ {
93-
v, err := toInt64(input[i])
94-
if err != nil {
95-
return nil, err
156+
v, ok := toInt64(input[i])
157+
if !ok {
158+
return nil, false
96159
}
97160
output[i] = v
98161
}
99162

100-
return output, nil
163+
return output, true
101164
}
102165

103-
func toUint64(input string) (uint64, error) {
104-
return strconv.ParseUint(input, 10, 64)
166+
func toUint64(input string) (uint64, bool) {
167+
v, err := strconv.ParseUint(input, 10, 64)
168+
return v, err == nil
105169
}
106170

107-
func toUint64s(input []string) ([]uint64, error) {
171+
func toUint64s(input []string) ([]uint64, bool) {
108172
inputLen := len(input)
109173

110174
output := make([]uint64, inputLen)
111175
for i, l := 0, inputLen; i < l; i++ {
112-
v, err := toUint64(input[i])
113-
if err != nil {
114-
return nil, err
176+
v, ok := toUint64(input[i])
177+
if !ok {
178+
return nil, false
115179
}
116180
output[i] = v
117181
}
118182

119-
return output, nil
183+
return output, true
120184
}
121185

122-
func toFloat64(input string) (float64, error) {
123-
return strconv.ParseFloat(input, 64)
186+
func toFloat64(input string) (float64, bool) {
187+
v, err := strconv.ParseFloat(input, 64)
188+
return v, err == nil
124189
}
125190

126-
func toFloat64s(input []string) ([]float64, error) {
191+
func toFloat64s(input []string) ([]float64, bool) {
127192
inputLen := len(input)
128193

129194
output := make([]float64, inputLen)
130195
for i, l := 0, inputLen; i < l; i++ {
131-
v, err := toFloat64(input[i])
132-
if err != nil {
133-
return nil, err
196+
v, ok := toFloat64(input[i])
197+
if !ok {
198+
return nil, false
134199
}
135200
output[i] = v
136201
}
137202

138-
return output, nil
203+
return output, true
139204
}
140205

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

0 commit comments

Comments
 (0)
Please sign in to comment.