Skip to content

Commit a5ac24b

Browse files
authored
test: added example codes (#55)
1 parent 71930c9 commit a5ac24b

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+
}

0 commit comments

Comments
 (0)