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 5ab70a8

Browse files
committedAug 31, 2024·
test: added example codes
1 parent 71930c9 commit 5ab70a8

7 files changed

+971
-63
lines changed
 

‎errors/example_errors_test.go

+281
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,281 @@
1+
package errors_test
2+
3+
import (
4+
goerrors "errors"
5+
"fmt"
6+
"reflect"
7+
8+
"github.com/sttk/cliargs/errors"
9+
)
10+
11+
func ExampleBadFieldType_Error() {
12+
e := errors.BadFieldType{
13+
Option: "foo-bar",
14+
Field: "FooBar",
15+
Type: reflect.TypeOf(int(0)),
16+
}
17+
18+
fmt.Printf("%s\n", e.Error())
19+
// Output:
20+
// BadFieldType{Option:foo-bar,Field:FooBar,Type:int}
21+
}
22+
23+
func ExampleConfigHasDefaultsButHasNoArg_Error() {
24+
e := errors.ConfigHasDefaultsButHasNoArg{
25+
StoreKey: "FooBar",
26+
Name: "foo-bar",
27+
}
28+
29+
fmt.Printf("%s\n", e.Error())
30+
// Output:
31+
// ConfigHasDefaultsButHasNoArg{StoreKey:FooBar,Name:foo-bar}
32+
}
33+
34+
func ExampleConfigHasDefaultsButHasNoArg_GetOption() {
35+
e := errors.ConfigHasDefaultsButHasNoArg{
36+
StoreKey: "FooBar",
37+
Name: "foo-bar",
38+
}
39+
var ee errors.InvalidOption = e
40+
41+
fmt.Printf("%s\n", e.GetOption())
42+
fmt.Printf("%s\n", ee.GetOption())
43+
// Output:
44+
// foo-bar
45+
// foo-bar
46+
}
47+
48+
func ExampleConfigIsArrayButHasNoArg_Error() {
49+
e := errors.ConfigIsArrayButHasNoArg{
50+
StoreKey: "FooBar",
51+
Name: "foo-bar",
52+
}
53+
54+
fmt.Printf("%s\n", e.Error())
55+
// Output:
56+
// ConfigIsArrayButHasNoArg{StoreKey:FooBar,Name:foo-bar}
57+
}
58+
59+
func ExampleConfigIsArrayButHasNoArg_GetOption() {
60+
e := errors.ConfigIsArrayButHasNoArg{
61+
StoreKey: "FooBar",
62+
Name: "foo-bar",
63+
}
64+
var ee errors.InvalidOption = e
65+
66+
fmt.Printf("%s\n", e.GetOption())
67+
fmt.Printf("%s\n", ee.GetOption())
68+
// Output:
69+
// foo-bar
70+
// foo-bar
71+
}
72+
73+
func ExampleOptionArgIsInvalid_Error() {
74+
e := errors.OptionArgIsInvalid{
75+
StoreKey: "FooBar",
76+
Option: "foo-bar",
77+
OptArg: "123",
78+
TypeKind: reflect.Int,
79+
Cause: fmt.Errorf("Bad number format"),
80+
}
81+
82+
fmt.Printf("%s\n", e.Error())
83+
// Output:
84+
// OptionArgIsInvalid{StoreKey:FooBar,Option:foo-bar,OptArg:123,TypeKind:int,Cause:Bad number format}
85+
}
86+
87+
func ExampleOptionArgIsInvalid_GetOption() {
88+
e := errors.OptionArgIsInvalid{
89+
StoreKey: "FooBar",
90+
Option: "foo-bar",
91+
OptArg: "123",
92+
TypeKind: reflect.Int,
93+
Cause: fmt.Errorf("Bad number format"),
94+
}
95+
var ee errors.InvalidOption = e
96+
97+
fmt.Printf("%s\n", e.GetOption())
98+
fmt.Printf("%s\n", ee.GetOption())
99+
// Output:
100+
// foo-bar
101+
// foo-bar
102+
}
103+
104+
func ExampleOptionArgIsInvalid_Unwrap() {
105+
// import ( goerrors "errors" )
106+
107+
e0 := fmt.Errorf("Bad number format")
108+
109+
e := errors.OptionArgIsInvalid{
110+
StoreKey: "FooBar",
111+
Option: "foo-bar",
112+
OptArg: "123",
113+
TypeKind: reflect.Int,
114+
Cause: e0,
115+
}
116+
117+
fmt.Printf("%t\n", goerrors.Is(e, e0))
118+
// Output:
119+
// true
120+
}
121+
122+
func ExampleOptionIsNotArray_Error() {
123+
e := errors.OptionIsNotArray{
124+
Option: "foo-bar",
125+
StoreKey: "FooBar",
126+
}
127+
128+
fmt.Printf("%s\n", e.Error())
129+
// Output:
130+
// OptionIsNotArray{Option:foo-bar,StoreKey:FooBar}
131+
}
132+
133+
func ExampleOptionIsNotArray_GetOption() {
134+
e := errors.OptionIsNotArray{
135+
Option: "foo-bar",
136+
StoreKey: "FooBar",
137+
}
138+
var ee errors.InvalidOption = e
139+
140+
fmt.Printf("%s\n", e.GetOption())
141+
fmt.Printf("%s\n", ee.GetOption())
142+
// Output:
143+
// foo-bar
144+
// foo-bar
145+
}
146+
147+
func ExampleOptionNameIsDuplicated_Error() {
148+
e := errors.OptionNameIsDuplicated{
149+
StoreKey: "FooBar",
150+
Name: "foo-bar",
151+
}
152+
153+
fmt.Printf("%s\n", e.Error())
154+
// Output:
155+
// OptionNameIsDuplicated{StoreKey:FooBar,Name:foo-bar}
156+
}
157+
158+
func ExampleOptionNameIsDuplicated_GetOption() {
159+
e := errors.OptionNameIsDuplicated{
160+
StoreKey: "FooBar",
161+
Name: "foo-bar",
162+
}
163+
164+
var ee errors.InvalidOption = e
165+
166+
fmt.Printf("%s\n", e.GetOption())
167+
fmt.Printf("%s\n", ee.GetOption())
168+
// Output:
169+
// foo-bar
170+
// foo-bar
171+
}
172+
173+
func ExampleOptionNeedsArg_Error() {
174+
e := errors.OptionNeedsArg{
175+
StoreKey: "FooBar",
176+
Option: "foo-bar",
177+
}
178+
179+
fmt.Printf("%s\n", e.Error())
180+
// Output:
181+
// OptionNeedsArg{Option:foo-bar,StoreKey:FooBar}
182+
}
183+
184+
func ExampleOptionNeedsArg_GetOption() {
185+
e := errors.OptionNeedsArg{
186+
Option: "foo-bar",
187+
StoreKey: "FooBar",
188+
}
189+
190+
var ee errors.InvalidOption = e
191+
192+
fmt.Printf("%s\n", e.GetOption())
193+
fmt.Printf("%s\n", ee.GetOption())
194+
// Output:
195+
// foo-bar
196+
// foo-bar
197+
}
198+
199+
func ExampleOptionStoreIsNotChangeable_Error() {
200+
e := errors.OptionStoreIsNotChangeable{}
201+
202+
fmt.Printf("%s\n", e.Error())
203+
// Output:
204+
// OptionStoreIsNotChangeable{}
205+
}
206+
207+
func ExampleOptionTakesNoArg_Error() {
208+
e := errors.OptionTakesNoArg{
209+
Option: "foo-bar",
210+
StoreKey: "FooBar",
211+
}
212+
213+
fmt.Printf("%s\n", e.Error())
214+
// Output:
215+
// OptionTakesNoArg{Option:foo-bar,StoreKey:FooBar}
216+
}
217+
218+
func ExampleOptionTakesNoArg_GetOption() {
219+
e := errors.OptionTakesNoArg{
220+
Option: "foo-bar",
221+
StoreKey: "FooBar",
222+
}
223+
224+
var ee errors.InvalidOption = e
225+
226+
fmt.Printf("%s\n", e.GetOption())
227+
fmt.Printf("%s\n", ee.GetOption())
228+
// Output:
229+
// foo-bar
230+
// foo-bar
231+
}
232+
233+
func ExampleStoreKeyIsDuplicated_Error() {
234+
e := errors.StoreKeyIsDuplicated{
235+
StoreKey: "FooBar",
236+
Name: "foo-bar",
237+
}
238+
239+
fmt.Printf("%s\n", e.Error())
240+
// Output:
241+
// StoreKeyIsDuplicated{StoreKey:FooBar,Name:foo-bar}
242+
}
243+
244+
func ExampleStoreKeyIsDuplicated_GetOption() {
245+
e := errors.StoreKeyIsDuplicated{
246+
StoreKey: "FooBar",
247+
Name: "foo-bar",
248+
}
249+
250+
var ee errors.InvalidOption = e
251+
252+
fmt.Printf("%s\n", e.GetOption())
253+
fmt.Printf("%s\n", ee.GetOption())
254+
// Output:
255+
// foo-bar
256+
// foo-bar
257+
}
258+
259+
func ExampleUnconfiguredOption_Error() {
260+
e := errors.UnconfiguredOption{
261+
Option: "foo-bar",
262+
}
263+
264+
fmt.Printf("%s\n", e.Error())
265+
// Output:
266+
// UnconfiguredOption{Option:foo-bar}
267+
}
268+
269+
func ExampleUnconfiguredOption_GetOption() {
270+
e := errors.UnconfiguredOption{
271+
Option: "foo-bar",
272+
}
273+
274+
var ee errors.InvalidOption = e
275+
276+
fmt.Printf("%s\n", e.GetOption())
277+
fmt.Printf("%s\n", ee.GetOption())
278+
// Output:
279+
// foo-bar
280+
// foo-bar
281+
}

‎example_help_test.go

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package cliargs_test
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/sttk/cliargs"
7+
)
8+
9+
func ExampleHelp_Print() {
10+
type MyOptions struct {
11+
FooBar bool `optcfg:"foo-bar,f" optdesc:"FooBar is a flag.\nThis flag is foo bar."`
12+
Baz int `optcfg:"baz,b=99" optdesc:"Baz is a integer." optarg:"<num>"`
13+
Qux string `optcfg:"=XXX" optdesc:"Qux is a string." optarg:"<text>"`
14+
Quux []string `optcfg:"quux=[A,B,C]" optdesc:"Quux is a string array."`
15+
}
16+
options := MyOptions{}
17+
optCfgs, _ := cliargs.MakeOptCfgsFor(&options)
18+
19+
help := cliargs.NewHelpWithMargins(5, 2)
20+
help.AddText("This is the usage section.")
21+
help.AddOptsWithIndentAndMargins(optCfgs, 10, 1, 0)
22+
23+
help.Print()
24+
25+
// Output:
26+
// This is the usage section.
27+
// --foo-bar, -f
28+
// FooBar is a flag.
29+
// This flag is foo bar.
30+
// --baz, -b <num>
31+
// Baz is a integer.
32+
// --Qux <text>
33+
// Qux is a string.
34+
// --quux Quux is a string array.
35+
}
36+
37+
func ExampleHelp_Iter() {
38+
type MyOptions struct {
39+
FooBar bool `optcfg:"foo-bar,f" optdesc:"FooBar is a flag.\nThis flag is foo bar."`
40+
Baz int `optcfg:"baz,b=99" optdesc:"Baz is a integer." optarg:"<num>"`
41+
Qux string `optcfg:"=XXX" optdesc:"Qux is a string." optarg:"<text>"`
42+
Quux []string `optcfg:"quux=[A,B,C]" optdesc:"Quux is a string array."`
43+
}
44+
options := MyOptions{}
45+
optCfgs, _ := cliargs.MakeOptCfgsFor(&options)
46+
47+
help := cliargs.NewHelpWithMargins(5, 2)
48+
help.AddText("This is the usage section.")
49+
help.AddOptsWithIndentAndMargins(optCfgs, 10, 1, 0)
50+
iter := help.Iter()
51+
52+
for {
53+
line, exists := iter.Next()
54+
if !exists {
55+
break
56+
}
57+
fmt.Println(line)
58+
}
59+
60+
// Output:
61+
// This is the usage section.
62+
// --foo-bar, -f
63+
// FooBar is a flag.
64+
// This flag is foo bar.
65+
// --baz, -b <num>
66+
// Baz is a integer.
67+
// --Qux <text>
68+
// Qux is a string.
69+
// --quux Quux is a string array.
70+
}

‎example_parse-for_test.go

+305
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,305 @@
1+
package cliargs_test
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/sttk/cliargs"
8+
)
9+
10+
func ExampleCmd_ParseFor() {
11+
type MyOptions struct {
12+
FooBar bool `optcfg:"foo-bar,f" optdesc:"FooBar description."`
13+
Baz int `optcfg:"baz,b=99" optdesc:"Baz description." optarg:"<num>"`
14+
Qux []string `optcfg:"qux,q=[A,B,C]" optdesc:"Qux description." optarg:"<text>"`
15+
}
16+
options := MyOptions{}
17+
18+
os.Args = []string{
19+
"path/to/app",
20+
"--foo-bar", "c1", "-b", "12", "--qux", "D", "c2", "-q", "E",
21+
}
22+
23+
cmd := cliargs.NewCmd()
24+
err := cmd.ParseFor(&options)
25+
optCfgs := cmd.OptCfgs
26+
27+
fmt.Printf("err = %v\n", err)
28+
fmt.Printf("cmd.Name = %v\n", cmd.Name)
29+
fmt.Printf("cmd.Args = %v\n", cmd.Args)
30+
fmt.Printf("cmd.HasOpt(\"FooBar\") = %v\n", cmd.HasOpt("FooBar"))
31+
fmt.Printf("cmd.HasOpt(\"Baz\") = %v\n", cmd.HasOpt("Baz"))
32+
fmt.Printf("cmd.HasOpt(\"Qux\") = %v\n", cmd.HasOpt("Qux"))
33+
fmt.Printf("cmd.OptArgs(\"FooBar\") = %v\n", cmd.OptArgs("FooBar"))
34+
fmt.Printf("cmd.OptArgs(\"Baz\") = %v\n", cmd.OptArgs("Baz"))
35+
fmt.Printf("cmd.OptArgs(\"Qux\") = %v\n", cmd.OptArgs("Qux"))
36+
37+
fmt.Printf("optCfgs[0].StoreKey = %v\n", optCfgs[0].StoreKey)
38+
fmt.Printf("optCfgs[0].Names = %v\n", optCfgs[0].Names)
39+
fmt.Printf("optCfgs[0].HasArg = %v\n", optCfgs[0].HasArg)
40+
fmt.Printf("optCfgs[0].IsArray = %v\n", optCfgs[0].IsArray)
41+
fmt.Printf("optCfgs[0].Defaults = %v\n", optCfgs[0].Defaults)
42+
fmt.Printf("optCfgs[0].Desc = %v\n", optCfgs[0].Desc)
43+
44+
fmt.Printf("optCfgs[1].StoreKey = %v\n", optCfgs[1].StoreKey)
45+
fmt.Printf("optCfgs[1].Names = %v\n", optCfgs[1].Names)
46+
fmt.Printf("optCfgs[1].HasArg = %v\n", optCfgs[1].HasArg)
47+
fmt.Printf("optCfgs[1].IsArray = %v\n", optCfgs[1].IsArray)
48+
fmt.Printf("optCfgs[1].Defaults = %v\n", optCfgs[1].Defaults)
49+
fmt.Printf("optCfgs[1].Desc = %v\n", optCfgs[1].Desc)
50+
fmt.Printf("optCfgs[1].ArgInHelp = %v\n", optCfgs[1].ArgInHelp)
51+
52+
fmt.Printf("optCfgs[2].StoreKey = %v\n", optCfgs[2].StoreKey)
53+
fmt.Printf("optCfgs[2].Names = %v\n", optCfgs[2].Names)
54+
fmt.Printf("optCfgs[2].HasArg = %v\n", optCfgs[2].HasArg)
55+
fmt.Printf("optCfgs[2].IsArray = %v\n", optCfgs[2].IsArray)
56+
fmt.Printf("optCfgs[2].Defaults = %v\n", optCfgs[2].Defaults)
57+
fmt.Printf("optCfgs[2].Desc = %v\n", optCfgs[2].Desc)
58+
fmt.Printf("optCfgs[2].ArgInHelp = %v\n", optCfgs[2].ArgInHelp)
59+
60+
fmt.Printf("options.FooBar = %v\n", options.FooBar)
61+
fmt.Printf("options.Baz = %v\n", options.Baz)
62+
fmt.Printf("options.Qux = %v\n", options.Qux)
63+
// Output:
64+
// err = <nil>
65+
// cmd.Name = app
66+
// cmd.Args = [c1 c2]
67+
// cmd.HasOpt("FooBar") = true
68+
// cmd.HasOpt("Baz") = true
69+
// cmd.HasOpt("Qux") = true
70+
// cmd.OptArgs("FooBar") = []
71+
// cmd.OptArgs("Baz") = [12]
72+
// cmd.OptArgs("Qux") = [D E]
73+
// optCfgs[0].StoreKey = FooBar
74+
// optCfgs[0].Names = [foo-bar f]
75+
// optCfgs[0].HasArg = false
76+
// optCfgs[0].IsArray = false
77+
// optCfgs[0].Defaults = []
78+
// optCfgs[0].Desc = FooBar description.
79+
// optCfgs[1].StoreKey = Baz
80+
// optCfgs[1].Names = [baz b]
81+
// optCfgs[1].HasArg = true
82+
// optCfgs[1].IsArray = false
83+
// optCfgs[1].Defaults = [99]
84+
// optCfgs[1].Desc = Baz description.
85+
// optCfgs[1].ArgInHelp = <num>
86+
// optCfgs[2].StoreKey = Qux
87+
// optCfgs[2].Names = [qux q]
88+
// optCfgs[2].HasArg = true
89+
// optCfgs[2].IsArray = true
90+
// optCfgs[2].Defaults = [A B C]
91+
// optCfgs[2].Desc = Qux description.
92+
// optCfgs[2].ArgInHelp = <text>
93+
// options.FooBar = true
94+
// options.Baz = 12
95+
// options.Qux = [D E]
96+
97+
reset()
98+
}
99+
100+
func ExampleCmd_ParseUntilSubCmdFor() {
101+
type MyOptions struct {
102+
FooBar bool `optcfg:"foo-bar,f" optdesc:"FooBar description."`
103+
Baz int `optcfg:"baz,b=99" optdesc:"Baz description." optarg:"<num>"`
104+
Qux []string `optcfg:"qux,q=[A,B,C]" optdesc:"Qux description." optarg:"<text>"`
105+
}
106+
options := MyOptions{}
107+
108+
os.Args = []string{
109+
"path/to/app",
110+
"--foo-bar", "c1", "-b", "12", "--qux", "D", "c2", "-q", "E",
111+
}
112+
113+
cmd := cliargs.NewCmd()
114+
subCmd, err := cmd.ParseUntilSubCmdFor(&options)
115+
optCfgs := cmd.OptCfgs
116+
117+
errSub := subCmd.Parse()
118+
119+
fmt.Printf("err = %v\n", err)
120+
fmt.Printf("cmd.Name = %v\n", cmd.Name)
121+
fmt.Printf("cmd.Args = %v\n", cmd.Args)
122+
fmt.Printf("cmd.HasOpt(\"FooBar\") = %v\n", cmd.HasOpt("FooBar"))
123+
fmt.Printf("cmd.HasOpt(\"Baz\") = %v\n", cmd.HasOpt("Baz"))
124+
fmt.Printf("cmd.HasOpt(\"Qux\") = %v\n", cmd.HasOpt("Qux"))
125+
fmt.Printf("cmd.OptArgs(\"FooBar\") = %v\n", cmd.OptArgs("FooBar"))
126+
fmt.Printf("cmd.OptArgs(\"Baz\") = %v\n", cmd.OptArgs("Baz"))
127+
fmt.Printf("cmd.OptArgs(\"Qux\") = %v\n", cmd.OptArgs("Qux"))
128+
129+
fmt.Printf("errSub = %v\n", errSub)
130+
fmt.Printf("subCmd.Name = %v\n", subCmd.Name)
131+
fmt.Printf("subCmd.Args = %v\n", subCmd.Args)
132+
fmt.Printf("subCmd.HasOpt(\"b\") = %v\n", subCmd.HasOpt("b"))
133+
fmt.Printf("subCmd.HasOpt(\"qux\") = %v\n", subCmd.HasOpt("qux"))
134+
fmt.Printf("subCmd.HasOpt(\"q\") = %v\n", subCmd.HasOpt("q"))
135+
fmt.Printf("subCmd.OptArgs(\"b\") = %v\n", subCmd.OptArgs("b"))
136+
fmt.Printf("subCmd.OptArgs(\"qux\") = %v\n", subCmd.OptArgs("qux"))
137+
fmt.Printf("subCmd.OptArgs(\"q\") = %v\n", subCmd.OptArgs("q"))
138+
139+
fmt.Printf("optCfgs[0].StoreKey = %v\n", optCfgs[0].StoreKey)
140+
fmt.Printf("optCfgs[0].Names = %v\n", optCfgs[0].Names)
141+
fmt.Printf("optCfgs[0].HasArg = %v\n", optCfgs[0].HasArg)
142+
fmt.Printf("optCfgs[0].IsArray = %v\n", optCfgs[0].IsArray)
143+
fmt.Printf("optCfgs[0].Defaults = %v\n", optCfgs[0].Defaults)
144+
fmt.Printf("optCfgs[0].Desc = %v\n", optCfgs[0].Desc)
145+
146+
fmt.Printf("optCfgs[1].StoreKey = %v\n", optCfgs[1].StoreKey)
147+
fmt.Printf("optCfgs[1].Names = %v\n", optCfgs[1].Names)
148+
fmt.Printf("optCfgs[1].HasArg = %v\n", optCfgs[1].HasArg)
149+
fmt.Printf("optCfgs[1].IsArray = %v\n", optCfgs[1].IsArray)
150+
fmt.Printf("optCfgs[1].Defaults = %v\n", optCfgs[1].Defaults)
151+
fmt.Printf("optCfgs[1].Desc = %v\n", optCfgs[1].Desc)
152+
fmt.Printf("optCfgs[1].ArgInHelp = %v\n", optCfgs[1].ArgInHelp)
153+
154+
fmt.Printf("optCfgs[2].StoreKey = %v\n", optCfgs[2].StoreKey)
155+
fmt.Printf("optCfgs[2].Names = %v\n", optCfgs[2].Names)
156+
fmt.Printf("optCfgs[2].HasArg = %v\n", optCfgs[2].HasArg)
157+
fmt.Printf("optCfgs[2].IsArray = %v\n", optCfgs[2].IsArray)
158+
fmt.Printf("optCfgs[2].Defaults = %v\n", optCfgs[2].Defaults)
159+
fmt.Printf("optCfgs[2].Desc = %v\n", optCfgs[2].Desc)
160+
fmt.Printf("optCfgs[2].ArgInHelp = %v\n", optCfgs[2].ArgInHelp)
161+
162+
fmt.Printf("options.FooBar = %v\n", options.FooBar)
163+
fmt.Printf("options.Baz = %v\n", options.Baz)
164+
fmt.Printf("options.Qux = %v\n", options.Qux)
165+
// Output:
166+
// err = <nil>
167+
// cmd.Name = app
168+
// cmd.Args = []
169+
// cmd.HasOpt("FooBar") = true
170+
// cmd.HasOpt("Baz") = true
171+
// cmd.HasOpt("Qux") = true
172+
// cmd.OptArgs("FooBar") = []
173+
// cmd.OptArgs("Baz") = [99]
174+
// cmd.OptArgs("Qux") = [A B C]
175+
// errSub = <nil>
176+
// subCmd.Name = c1
177+
// subCmd.Args = [12 D c2 E]
178+
// subCmd.HasOpt("b") = true
179+
// subCmd.HasOpt("qux") = true
180+
// subCmd.HasOpt("q") = true
181+
// subCmd.OptArgs("b") = []
182+
// subCmd.OptArgs("qux") = []
183+
// subCmd.OptArgs("q") = []
184+
// optCfgs[0].StoreKey = FooBar
185+
// optCfgs[0].Names = [foo-bar f]
186+
// optCfgs[0].HasArg = false
187+
// optCfgs[0].IsArray = false
188+
// optCfgs[0].Defaults = []
189+
// optCfgs[0].Desc = FooBar description.
190+
// optCfgs[1].StoreKey = Baz
191+
// optCfgs[1].Names = [baz b]
192+
// optCfgs[1].HasArg = true
193+
// optCfgs[1].IsArray = false
194+
// optCfgs[1].Defaults = [99]
195+
// optCfgs[1].Desc = Baz description.
196+
// optCfgs[1].ArgInHelp = <num>
197+
// optCfgs[2].StoreKey = Qux
198+
// optCfgs[2].Names = [qux q]
199+
// optCfgs[2].HasArg = true
200+
// optCfgs[2].IsArray = true
201+
// optCfgs[2].Defaults = [A B C]
202+
// optCfgs[2].Desc = Qux description.
203+
// optCfgs[2].ArgInHelp = <text>
204+
// options.FooBar = true
205+
// options.Baz = 99
206+
// options.Qux = [A B C]
207+
208+
reset()
209+
}
210+
211+
func ExampleMakeOptCfgsFor() {
212+
type MyOptions struct {
213+
FooBar bool `optcfg:"foo-bar,f" optdesc:"FooBar description"`
214+
Baz int `optcfg:"baz,b=99" optdesc:"Baz description" optarg:"<number>"`
215+
Qux string `optcfg:"=XXX" optdesc:"Qux description" optarg:"<string>"`
216+
Quux []string `optcfg:"quux=[A,B,C]" optdesc:"Quux description" optarg:"<array elem>"`
217+
Corge []int
218+
}
219+
options := MyOptions{}
220+
221+
optCfgs, err := cliargs.MakeOptCfgsFor(&options)
222+
fmt.Printf("err = %v\n", err)
223+
fmt.Printf("len(optCfgs) = %v\n", len(optCfgs))
224+
fmt.Println()
225+
fmt.Printf("optCfgs[0].StoreKey = %v\n", optCfgs[0].StoreKey)
226+
fmt.Printf("optCfgs[0].Names = %v\n", optCfgs[0].Names)
227+
fmt.Printf("optCfgs[0].HasArg = %v\n", optCfgs[0].HasArg)
228+
fmt.Printf("optCfgs[0].IsArray = %v\n", optCfgs[0].IsArray)
229+
fmt.Printf("optCfgs[0].Defaults = %v\n", optCfgs[0].Defaults)
230+
fmt.Printf("optCfgs[0].Desc = %v\n", optCfgs[0].Desc)
231+
fmt.Println()
232+
fmt.Printf("optCfgs[1].StoreKey = %v\n", optCfgs[1].StoreKey)
233+
fmt.Printf("optCfgs[1].Names = %v\n", optCfgs[1].Names)
234+
fmt.Printf("optCfgs[1].HasArg = %v\n", optCfgs[1].HasArg)
235+
fmt.Printf("optCfgs[1].IsArray = %v\n", optCfgs[1].IsArray)
236+
fmt.Printf("optCfgs[1].Defaults = %v\n", optCfgs[1].Defaults)
237+
fmt.Printf("optCfgs[1].Desc = %v\n", optCfgs[1].Desc)
238+
fmt.Printf("optCfgs[1].ArgInHelp = %v\n", optCfgs[1].ArgInHelp)
239+
fmt.Println()
240+
fmt.Printf("optCfgs[2].StoreKey = %v\n", optCfgs[2].StoreKey)
241+
fmt.Printf("optCfgs[2].Names = %v\n", optCfgs[2].Names)
242+
fmt.Printf("optCfgs[2].HasArg = %v\n", optCfgs[2].HasArg)
243+
fmt.Printf("optCfgs[2].IsArray = %v\n", optCfgs[2].IsArray)
244+
fmt.Printf("optCfgs[2].Defaults = %v\n", optCfgs[2].Defaults)
245+
fmt.Printf("optCfgs[2].Desc = %v\n", optCfgs[2].Desc)
246+
fmt.Printf("optCfgs[2].ArgInHelp = %v\n", optCfgs[2].ArgInHelp)
247+
fmt.Println()
248+
fmt.Printf("optCfgs[3].StoreKey = %v\n", optCfgs[3].StoreKey)
249+
fmt.Printf("optCfgs[3].Names = %v\n", optCfgs[3].Names)
250+
fmt.Printf("optCfgs[3].HasArg = %v\n", optCfgs[3].HasArg)
251+
fmt.Printf("optCfgs[3].IsArray = %v\n", optCfgs[3].IsArray)
252+
fmt.Printf("optCfgs[3].Defaults = %v\n", optCfgs[3].Defaults)
253+
fmt.Printf("optCfgs[3].Desc = %v\n", optCfgs[3].Desc)
254+
fmt.Printf("optCfgs[3].ArgInHelp = %v\n", optCfgs[3].ArgInHelp)
255+
fmt.Println()
256+
fmt.Printf("optCfgs[4].StoreKey = %v\n", optCfgs[4].StoreKey)
257+
fmt.Printf("optCfgs[4].Names = %v\n", optCfgs[4].Names)
258+
fmt.Printf("optCfgs[4].HasArg = %v\n", optCfgs[4].HasArg)
259+
fmt.Printf("optCfgs[4].IsArray = %v\n", optCfgs[4].IsArray)
260+
fmt.Printf("optCfgs[4].Defaults = %v\n", optCfgs[4].Defaults)
261+
fmt.Printf("optCfgs[4].Desc = %v\n", optCfgs[4].Desc)
262+
// Output:
263+
// err = <nil>
264+
// len(optCfgs) = 5
265+
//
266+
// optCfgs[0].StoreKey = FooBar
267+
// optCfgs[0].Names = [foo-bar f]
268+
// optCfgs[0].HasArg = false
269+
// optCfgs[0].IsArray = false
270+
// optCfgs[0].Defaults = []
271+
// optCfgs[0].Desc = FooBar description
272+
//
273+
// optCfgs[1].StoreKey = Baz
274+
// optCfgs[1].Names = [baz b]
275+
// optCfgs[1].HasArg = true
276+
// optCfgs[1].IsArray = false
277+
// optCfgs[1].Defaults = [99]
278+
// optCfgs[1].Desc = Baz description
279+
// optCfgs[1].ArgInHelp = <number>
280+
//
281+
// optCfgs[2].StoreKey = Qux
282+
// optCfgs[2].Names = [Qux]
283+
// optCfgs[2].HasArg = true
284+
// optCfgs[2].IsArray = false
285+
// optCfgs[2].Defaults = [XXX]
286+
// optCfgs[2].Desc = Qux description
287+
// optCfgs[2].ArgInHelp = <string>
288+
//
289+
// optCfgs[3].StoreKey = Quux
290+
// optCfgs[3].Names = [quux]
291+
// optCfgs[3].HasArg = true
292+
// optCfgs[3].IsArray = true
293+
// optCfgs[3].Defaults = [A B C]
294+
// optCfgs[3].Desc = Quux description
295+
// optCfgs[3].ArgInHelp = <array elem>
296+
//
297+
// optCfgs[4].StoreKey = Corge
298+
// optCfgs[4].Names = [Corge]
299+
// optCfgs[4].HasArg = true
300+
// optCfgs[4].IsArray = true
301+
// optCfgs[4].Defaults = []
302+
// optCfgs[4].Desc =
303+
304+
reset()
305+
}

‎example_parse-with_test.go

+144
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
package cliargs_test
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/sttk/cliargs"
8+
)
9+
10+
func ExampleCmd_ParseWith() {
11+
os.Args = []string{
12+
"path/to/app", "--foo-bar", "qux", "--baz", "1", "-z=2", "-X", "quux",
13+
}
14+
15+
optCfgs := []cliargs.OptCfg{
16+
cliargs.OptCfg{
17+
Names: []string{"foo-bar"},
18+
},
19+
cliargs.OptCfg{
20+
StoreKey: "Bazoo",
21+
Names: []string{"baz", "z"},
22+
HasArg: true,
23+
IsArray: true,
24+
},
25+
cliargs.OptCfg{
26+
Names: []string{"corge"},
27+
HasArg: true,
28+
Defaults: []string{"99"},
29+
},
30+
cliargs.OptCfg{
31+
StoreKey: "*",
32+
},
33+
}
34+
35+
cmd := cliargs.NewCmd()
36+
err := cmd.ParseWith(optCfgs)
37+
fmt.Printf("err = %v\n", err)
38+
fmt.Printf("cmd.Name = %v\n", cmd.Name)
39+
fmt.Printf("cmd.Args = %v\n", cmd.Args)
40+
fmt.Printf("cmd.HasOpt(\"foo-bar\") = %v\n", cmd.HasOpt("foo-bar"))
41+
fmt.Printf("cmd.HasOpt(\"Bazoo\") = %v\n", cmd.HasOpt("Bazoo"))
42+
fmt.Printf("cmd.HasOpt(\"X\") = %v\n", cmd.HasOpt("X"))
43+
fmt.Printf("cmd.HasOpt(\"corge\") = %v\n", cmd.HasOpt("corge"))
44+
fmt.Printf("cmd.OptArg(\"Bazoo\") = %v\n", cmd.OptArg("Bazoo"))
45+
fmt.Printf("cmd.OptArg(\"corge\") = %v\n", cmd.OptArg("corge"))
46+
fmt.Printf("cmd.OptArgs(\"Bazoo\") = %v\n", cmd.OptArgs("Bazoo"))
47+
fmt.Printf("cmd.OptArgs(\"corge\") = %v\n", cmd.OptArgs("corge"))
48+
49+
// Output:
50+
// err = <nil>
51+
// cmd.Name = app
52+
// cmd.Args = [qux quux]
53+
// cmd.HasOpt("foo-bar") = true
54+
// cmd.HasOpt("Bazoo") = true
55+
// cmd.HasOpt("X") = true
56+
// cmd.HasOpt("corge") = true
57+
// cmd.OptArg("Bazoo") = 1
58+
// cmd.OptArg("corge") = 99
59+
// cmd.OptArgs("Bazoo") = [1 2]
60+
// cmd.OptArgs("corge") = [99]
61+
62+
reset()
63+
}
64+
65+
func ExampleCmd_ParseUntilSubCmdWith() {
66+
os.Args = []string{
67+
"path/to/app", "--foo-bar", "qux", "--baz", "1", "-z=2", "-X", "quux",
68+
}
69+
70+
optCfgs := []cliargs.OptCfg{
71+
cliargs.OptCfg{
72+
Names: []string{"foo-bar"},
73+
},
74+
cliargs.OptCfg{
75+
StoreKey: "Bazoo",
76+
Names: []string{"baz", "z"},
77+
HasArg: true,
78+
IsArray: true,
79+
},
80+
cliargs.OptCfg{
81+
Names: []string{"corge"},
82+
HasArg: true,
83+
Defaults: []string{"99"},
84+
},
85+
cliargs.OptCfg{
86+
StoreKey: "*",
87+
},
88+
}
89+
90+
cmd := cliargs.NewCmd()
91+
subCmd, err := cmd.ParseUntilSubCmdWith(optCfgs)
92+
errSub := subCmd.Parse()
93+
94+
fmt.Printf("err = %v\n", err)
95+
fmt.Printf("cmd.Name = %v\n", cmd.Name)
96+
fmt.Printf("cmd.Args = %v\n", cmd.Args)
97+
fmt.Printf("cmd.HasOpt(\"foo-bar\") = %v\n", cmd.HasOpt("foo-bar"))
98+
fmt.Printf("cmd.HasOpt(\"Bazoo\") = %v\n", cmd.HasOpt("Bazoo"))
99+
fmt.Printf("cmd.HasOpt(\"X\") = %v\n", cmd.HasOpt("X"))
100+
fmt.Printf("cmd.HasOpt(\"corge\") = %v\n", cmd.HasOpt("corge"))
101+
fmt.Printf("len(cmd.OptArg(\"Bazoo\")) = %v\n", len(cmd.OptArg("Bazoo")))
102+
fmt.Printf("cmd.OptArg(\"corge\") = %v\n", cmd.OptArg("corge"))
103+
fmt.Printf("cmd.OptArgs(\"Bazoo\") = %v\n", cmd.OptArgs("Bazoo"))
104+
fmt.Printf("cmd.OptArgs(\"corge\") = %v\n", cmd.OptArgs("corge"))
105+
106+
fmt.Printf("errSub = %v\n", errSub)
107+
fmt.Printf("subCmd.Name = %v\n", subCmd.Name)
108+
fmt.Printf("subCmd.Args = %v\n", subCmd.Args)
109+
fmt.Printf("subCmd.HasOpt(\"baz\") = %v\n", subCmd.HasOpt("baz"))
110+
fmt.Printf("subCmd.HasOpt(\"z\") = %v\n", subCmd.HasOpt("z"))
111+
fmt.Printf("subCmd.HasOpt(\"X\") = %v\n", subCmd.HasOpt("X"))
112+
fmt.Printf("len(subCmd.OptArg(\"baz\")) = %v\n", len(subCmd.OptArg("baz")))
113+
fmt.Printf("subCmd.OptArg(\"z\") = %v\n", subCmd.OptArg("z"))
114+
fmt.Printf("len(subCmd.OptArg(\"X\")) = %v\n", len(subCmd.OptArg("X")))
115+
fmt.Printf("subCmd.OptArgs(\"baz\") = %v\n", subCmd.OptArgs("baz"))
116+
fmt.Printf("subCmd.OptArgs(\"z\") = %v\n", subCmd.OptArgs("z"))
117+
fmt.Printf("subCmd.OptArgs(\"X\") = %v\n", subCmd.OptArgs("X"))
118+
// Output:
119+
// err = <nil>
120+
// cmd.Name = app
121+
// cmd.Args = []
122+
// cmd.HasOpt("foo-bar") = true
123+
// cmd.HasOpt("Bazoo") = false
124+
// cmd.HasOpt("X") = false
125+
// cmd.HasOpt("corge") = true
126+
// len(cmd.OptArg("Bazoo")) = 0
127+
// cmd.OptArg("corge") = 99
128+
// cmd.OptArgs("Bazoo") = []
129+
// cmd.OptArgs("corge") = [99]
130+
// errSub = <nil>
131+
// subCmd.Name = qux
132+
// subCmd.Args = [1 quux]
133+
// subCmd.HasOpt("baz") = true
134+
// subCmd.HasOpt("z") = true
135+
// subCmd.HasOpt("X") = true
136+
// len(subCmd.OptArg("baz")) = 0
137+
// subCmd.OptArg("z") = 2
138+
// len(subCmd.OptArg("X")) = 0
139+
// subCmd.OptArgs("baz") = []
140+
// subCmd.OptArgs("z") = [2]
141+
// subCmd.OptArgs("X") = []
142+
143+
reset()
144+
}

‎example_parse_test.go

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package cliargs_test
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/sttk/cliargs"
8+
)
9+
10+
func ExampleCmd_Parse() {
11+
os.Args = []string{
12+
"path/to/app", "--foo-bar=A", "-a", "--baz", "-bc=3", "qux", "-c=4", "quux",
13+
}
14+
15+
cmd := cliargs.NewCmd()
16+
err := cmd.Parse()
17+
fmt.Printf("err = %v\n", err)
18+
fmt.Printf("cmd.Name = %s\n", cmd.Name)
19+
fmt.Printf("cmd.Args = %v\n", cmd.Args)
20+
fmt.Printf("cmd.HasOpt(\"a\") = %v\n", cmd.HasOpt("a"))
21+
fmt.Printf("cmd.HasOpt(\"b\") = %v\n", cmd.HasOpt("b"))
22+
fmt.Printf("cmd.HasOpt(\"c\") = %v\n", cmd.HasOpt("c"))
23+
fmt.Printf("cmd.HasOpt(\"foo-bar\") = %v\n", cmd.HasOpt("foo-bar"))
24+
fmt.Printf("cmd.HasOpt(\"baz\") = %v\n", cmd.HasOpt("baz"))
25+
fmt.Printf("cmd.OptArg(\"c\") = %v\n", cmd.OptArg("c"))
26+
fmt.Printf("cmd.OptArg(\"foo-bar\") = %v\n", cmd.OptArg("foo-bar"))
27+
fmt.Printf("cmd.OptArgs(\"c\") = %v\n", cmd.OptArgs("c"))
28+
fmt.Printf("cmd.OptArgs(\"foo-bar\") = %v\n", cmd.OptArgs("foo-bar"))
29+
// Output:
30+
// err = <nil>
31+
// cmd.Name = app
32+
// cmd.Args = [qux quux]
33+
// cmd.HasOpt("a") = true
34+
// cmd.HasOpt("b") = true
35+
// cmd.HasOpt("c") = true
36+
// cmd.HasOpt("foo-bar") = true
37+
// cmd.HasOpt("baz") = true
38+
// cmd.OptArg("c") = 3
39+
// cmd.OptArg("foo-bar") = A
40+
// cmd.OptArgs("c") = [3 4]
41+
// cmd.OptArgs("foo-bar") = [A]
42+
43+
reset()
44+
}
45+
46+
func ExampleCmd_ParseUntilSubCmd() {
47+
os.Args = []string{
48+
"path/to/app", "--foo-bar=A", "-a", "--baz", "-bc=3", "qux", "-c=4", "quux",
49+
}
50+
51+
cmd := cliargs.NewCmd()
52+
subCmd, err := cmd.ParseUntilSubCmd()
53+
errSub := subCmd.Parse()
54+
55+
fmt.Printf("err = %v\n", err)
56+
fmt.Printf("cmd.Name = %s\n", cmd.Name)
57+
fmt.Printf("cmd.Args = %v\n", cmd.Args)
58+
fmt.Printf("cmd.HasOpt(\"a\") = %v\n", cmd.HasOpt("a"))
59+
fmt.Printf("cmd.HasOpt(\"b\") = %v\n", cmd.HasOpt("b"))
60+
fmt.Printf("cmd.HasOpt(\"c\") = %v\n", cmd.HasOpt("c"))
61+
fmt.Printf("cmd.HasOpt(\"foo-bar\") = %v\n", cmd.HasOpt("foo-bar"))
62+
fmt.Printf("cmd.HasOpt(\"baz\") = %v\n", cmd.HasOpt("baz"))
63+
fmt.Printf("cmd.OptArg(\"c\") = %v\n", cmd.OptArg("c"))
64+
fmt.Printf("cmd.OptArg(\"foo-bar\") = %v\n", cmd.OptArg("foo-bar"))
65+
fmt.Printf("cmd.OptArgs(\"c\") = %v\n", cmd.OptArgs("c"))
66+
fmt.Printf("cmd.OptArgs(\"foo-bar\") = %v\n", cmd.OptArgs("foo-bar"))
67+
68+
fmt.Printf("errSub = %v\n", errSub)
69+
fmt.Printf("subCmd.Name = %s\n", subCmd.Name)
70+
fmt.Printf("subCmd.Args = %v\n", subCmd.Args)
71+
fmt.Printf("subCmd.HasOpt(\"c\") = %v\n", subCmd.HasOpt("c"))
72+
fmt.Printf("subCmd.OptArg(\"c\") = %v\n", subCmd.OptArg("c"))
73+
fmt.Printf("subCmd.OptArgs(\"c\") = %v\n", subCmd.OptArgs("c"))
74+
75+
// Output:
76+
// err = <nil>
77+
// cmd.Name = app
78+
// cmd.Args = []
79+
// cmd.HasOpt("a") = true
80+
// cmd.HasOpt("b") = true
81+
// cmd.HasOpt("c") = true
82+
// cmd.HasOpt("foo-bar") = true
83+
// cmd.HasOpt("baz") = true
84+
// cmd.OptArg("c") = 3
85+
// cmd.OptArg("foo-bar") = A
86+
// cmd.OptArgs("c") = [3]
87+
// cmd.OptArgs("foo-bar") = [A]
88+
// errSub = <nil>
89+
// subCmd.Name = qux
90+
// subCmd.Args = [quux]
91+
// subCmd.HasOpt("c") = true
92+
// subCmd.OptArg("c") = 4
93+
// subCmd.OptArgs("c") = [4]
94+
95+
reset()
96+
}

‎help.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -440,11 +440,11 @@ type blockIter struct {
440440
indent string
441441
margin string
442442
lineIter linebreak.LineIter
443+
isEnded bool
443444
}
444445

445-
// Next is a method which returns a line of a help text and a status which indicates this HelpIter
446-
// has more texts or not.
447-
// If there are more lines, the returned IterStatus value is true, otherwise the value is false.
446+
// Next is a method that returns a line of a help text and a flag which indicates the line is not
447+
// end.
448448
func (iter *HelpIter) Next() (string, bool) {
449449
for {
450450
line, exists := iter.blockIter.next()

‎validators/validators.go

+72-60
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,7 @@ import (
77
"github.com/sttk/cliargs/errors"
88
)
99

10-
// ValidateInt is the function that validates an opton argument string whether it is valid as
11-
// a int value.
12-
//
13-
// If the option argument is invalid, this function returns an OptionArgIsInvalid error.
14-
var ValidateInt = func(storeKey string, option string, optArg string) error {
10+
func validateInt(storeKey string, option string, optArg string) error {
1511
_, e := strconv.ParseInt(optArg, 0, strconv.IntSize)
1612
if e != nil {
1713
return errors.OptionArgIsInvalid{
@@ -20,11 +16,7 @@ var ValidateInt = func(storeKey string, option string, optArg string) error {
2016
return nil
2117
}
2218

23-
// ValidateInt8 is the function that validates an opton argument string whether it is valid as
24-
// a int8 value.
25-
//
26-
// If the option argument is invalid, this function returns an OptionArgIsInvalid error.
27-
var ValidateInt8 = func(storeKey string, option string, optArg string) error {
19+
func validateInt8(storeKey string, option string, optArg string) error {
2820
_, e := strconv.ParseInt(optArg, 0, 8)
2921
if e != nil {
3022
return errors.OptionArgIsInvalid{
@@ -33,11 +25,7 @@ var ValidateInt8 = func(storeKey string, option string, optArg string) error {
3325
return nil
3426
}
3527

36-
// ValidateInt16 is the function that validates an opton argument string whether it is valid as
37-
// a int16 value.
38-
//
39-
// If the option argument is invalid, this function returns an OptionArgIsInvalid error.
40-
var ValidateInt16 = func(storeKey string, option string, optArg string) error {
28+
func validateInt16(storeKey string, option string, optArg string) error {
4129
_, e := strconv.ParseInt(optArg, 0, 16)
4230
if e != nil {
4331
return errors.OptionArgIsInvalid{
@@ -46,11 +34,7 @@ var ValidateInt16 = func(storeKey string, option string, optArg string) error {
4634
return nil
4735
}
4836

49-
// ValidateInt32 is the function that validates an opton argument string whether it is valid as
50-
// a int32 value.
51-
//
52-
// If the option argument is invalid, this function returns an OptionArgIsInvalid error.
53-
var ValidateInt32 = func(storeKey string, option string, optArg string) error {
37+
func validateInt32(storeKey string, option string, optArg string) error {
5438
_, e := strconv.ParseInt(optArg, 0, 32)
5539
if e != nil {
5640
return errors.OptionArgIsInvalid{
@@ -59,11 +43,7 @@ var ValidateInt32 = func(storeKey string, option string, optArg string) error {
5943
return nil
6044
}
6145

62-
// ValidateInt64 is the function that validates an opton argument string whether it is valid as
63-
// a int64 value.
64-
//
65-
// If the option argument is invalid, this function returns an OptionArgIsInvalid error.
66-
var ValidateInt64 = func(storeKey string, option string, optArg string) error {
46+
func validateInt64(storeKey string, option string, optArg string) error {
6747
_, e := strconv.ParseInt(optArg, 0, 64)
6848
if e != nil {
6949
return errors.OptionArgIsInvalid{
@@ -72,11 +52,7 @@ var ValidateInt64 = func(storeKey string, option string, optArg string) error {
7252
return nil
7353
}
7454

75-
// ValidateUint is the function that validates an opton argument string whether it is valid as
76-
// a uint value.
77-
//
78-
// If the option argument is invalid, this function returns an OptionArgIsInvalid error.
79-
var ValidateUint = func(storeKey string, option string, optArg string) error {
55+
func validateUint(storeKey string, option string, optArg string) error {
8056
_, e := strconv.ParseUint(optArg, 0, strconv.IntSize)
8157
if e != nil {
8258
return errors.OptionArgIsInvalid{
@@ -85,11 +61,7 @@ var ValidateUint = func(storeKey string, option string, optArg string) error {
8561
return nil
8662
}
8763

88-
// ValidateUint8 is the function that validates an opton argument string whether it is valid as
89-
// a uint8 value.
90-
//
91-
// If the option argument is invalid, this function returns an OptionArgIsInvalid error.
92-
var ValidateUint8 = func(storeKey string, option string, optArg string) error {
64+
func validateUint8(storeKey string, option string, optArg string) error {
9365
_, e := strconv.ParseUint(optArg, 0, 8)
9466
if e != nil {
9567
return errors.OptionArgIsInvalid{
@@ -98,11 +70,7 @@ var ValidateUint8 = func(storeKey string, option string, optArg string) error {
9870
return nil
9971
}
10072

101-
// ValidateUint16 is the function that validates an opton argument string whether it is valid as
102-
// a uint16 value.
103-
//
104-
// If the option argument is invalid, this function returns an OptionArgIsInvalid error.
105-
var ValidateUint16 = func(storeKey string, option string, optArg string) error {
73+
func validateUint16(storeKey string, option string, optArg string) error {
10674
_, e := strconv.ParseUint(optArg, 0, 16)
10775
if e != nil {
10876
return errors.OptionArgIsInvalid{
@@ -111,11 +79,7 @@ var ValidateUint16 = func(storeKey string, option string, optArg string) error {
11179
return nil
11280
}
11381

114-
// ValidateUint32 is the function that validates an opton argument string whether it is valid as
115-
// a uint32 value.
116-
//
117-
// If the option argument is invalid, this function returns an OptionArgIsInvalid error.
118-
var ValidateUint32 = func(storeKey string, option string, optArg string) error {
82+
func validateUint32(storeKey string, option string, optArg string) error {
11983
_, e := strconv.ParseUint(optArg, 0, 32)
12084
if e != nil {
12185
return errors.OptionArgIsInvalid{
@@ -124,11 +88,7 @@ var ValidateUint32 = func(storeKey string, option string, optArg string) error {
12488
return nil
12589
}
12690

127-
// ValidateUint64 is the function that validates an opton argument string whether it is valid as
128-
// a uint64 value.
129-
//
130-
// If the option argument is invalid, this function returns an OptionArgIsInvalid error.
131-
var ValidateUint64 = func(storeKey string, option string, optArg string) error {
91+
func validateUint64(storeKey string, option string, optArg string) error {
13292
_, e := strconv.ParseUint(optArg, 0, 64)
13393
if e != nil {
13494
return errors.OptionArgIsInvalid{
@@ -137,11 +97,7 @@ var ValidateUint64 = func(storeKey string, option string, optArg string) error {
13797
return nil
13898
}
13999

140-
// ValidateFloat32 is the function that validates an opton argument string whether it is valid as
141-
// a float32 value.
142-
//
143-
// If the option argument is invalid, this function returns an OptionArgIsInvalid error.
144-
var ValidateFloat32 = func(storeKey string, option string, optArg string) error {
100+
func validateFloat32(storeKey string, option string, optArg string) error {
145101
_, e := strconv.ParseFloat(optArg, 32)
146102
if e != nil {
147103
return errors.OptionArgIsInvalid{
@@ -150,15 +106,71 @@ var ValidateFloat32 = func(storeKey string, option string, optArg string) error
150106
return nil
151107
}
152108

153-
// ValidateFloat64 is the function that validates an opton argument string whether it is valid as
154-
// a float64 value.
155-
//
156-
// If the option argument is invalid, this function returns an OptionArgIsInvalid error.
157-
var ValidateFloat64 = func(storeKey string, option string, optArg string) error {
109+
func validateFloat64(storeKey string, option string, optArg string) error {
158110
_, e := strconv.ParseFloat(optArg, 64)
159111
if e != nil {
160112
return errors.OptionArgIsInvalid{
161113
StoreKey: storeKey, Option: option, OptArg: optArg, TypeKind: reflect.Float64, Cause: e}
162114
}
163115
return nil
164116
}
117+
118+
// ValidateInt is the function that validates an opton argument string whether it is valid as
119+
// a int value.
120+
// If the option argument is invalid, this function returns an OptionArgIsInvalid error.
121+
var ValidateInt func(storeKey, option, optArg string) error = validateInt
122+
123+
// ValidateInt8 is the function that validates an opton argument string whether it is valid as
124+
// a int8 value.
125+
// If the option argument is invalid, this function returns an OptionArgIsInvalid error.
126+
var ValidateInt8 func(storeKey, option, optArg string) error = validateInt8
127+
128+
// ValidateInt16 is the function that validates an opton argument string whether it is valid as
129+
// a int16 value.
130+
// If the option argument is invalid, this function returns an OptionArgIsInvalid error.
131+
var ValidateInt16 func(storeKey, option, optArg string) error = validateInt16
132+
133+
// ValidateInt32 is the function that validates an opton argument string whether it is valid as
134+
// a int32 value.
135+
// If the option argument is invalid, this function returns an OptionArgIsInvalid error.
136+
var ValidateInt32 func(storeKey, option, optArg string) error = validateInt32
137+
138+
// ValidateInt64 is the function that validates an opton argument string whether it is valid as
139+
// a int64 value.
140+
// If the option argument is invalid, this function returns an OptionArgIsInvalid error.
141+
var ValidateInt64 func(storeKey, option, optArg string) error = validateInt64
142+
143+
// ValidateUint is the function that validates an opton argument string whether it is valid as
144+
// a uint value.
145+
// If the option argument is invalid, this function returns an OptionArgIsInvalid error.
146+
var ValidateUint func(storeKey, option, optArg string) error = validateUint
147+
148+
// ValidateUint8 is the function that validates an opton argument string whether it is valid as
149+
// a uint8 value.
150+
// If the option argument is invalid, this function returns an OptionArgIsInvalid error.
151+
var ValidateUint8 func(storeKey, option, optArg string) error = validateUint8
152+
153+
// ValidateUint16 is the function that validates an opton argument string whether it is valid as
154+
// a uint16 value.
155+
// If the option argument is invalid, this function returns an OptionArgIsInvalid error.
156+
var ValidateUint16 func(storeKey, option, optArg string) error = validateUint16
157+
158+
// ValidateUint32 is the function that validates an opton argument string whether it is valid as
159+
// a uint32 value.
160+
// If the option argument is invalid, this function returns an OptionArgIsInvalid error.
161+
var ValidateUint32 func(storeKey, option, optArg string) error = validateUint32
162+
163+
// ValidateUint64 is the function that validates an opton argument string whether it is valid as
164+
// a uint64 value.
165+
// If the option argument is invalid, this function returns an OptionArgIsInvalid error.
166+
var ValidateUint64 func(storeKey, option, optArg string) error = validateUint64
167+
168+
// ValidateFloat32 is the function that validates an opton argument string whether it is valid as
169+
// a float32 value.
170+
// If the option argument is invalid, this function returns an OptionArgIsInvalid error.
171+
var ValidateFloat32 func(storeKey, option, optArg string) error = validateFloat32
172+
173+
// ValidateFloat64 is the function that validates an opton argument string whether it is valid as
174+
// a float64 value.
175+
// If the option argument is invalid, this function returns an OptionArgIsInvalid error.
176+
var ValidateFloat64 func(storeKey, option, optArg string) error = validateFloat64

0 commit comments

Comments
 (0)
Please sign in to comment.