-
Notifications
You must be signed in to change notification settings - Fork 191
/
app_test.go
145 lines (120 loc) · 3.25 KB
/
app_test.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
package cflag_test
import (
"bytes"
"os"
"testing"
"github.com/gookit/goutil/cflag"
"github.com/gookit/goutil/dump"
"github.com/gookit/goutil/strutil"
"github.com/gookit/goutil/testutil/assert"
)
func ExampleNewApp() {
app := cflag.NewApp()
app.Desc = "this is my cli application"
app.Version = "1.0.2"
var c1Opts = struct {
age int
name string
}{}
c1 := cflag.NewCmd("demo", "this is a demo command")
c1.OnAdd = func(c *cflag.Cmd) {
c.IntVar(&c1Opts.age, "age", 0, "this is a int option;;a")
c.StringVar(&c1Opts.name, "name", "", "this is a string option and required;true")
c.AddArg("arg1", "this is arg1", true, nil)
c.AddArg("arg2", "this is arg2", false, nil)
}
c1.Func = func(c *cflag.Cmd) error {
dump.P(c1Opts, c.Args())
return nil
}
var c2Opts = struct {
str1 string
lOpt string
bol bool
}{}
c2 := cflag.NewCmd("other", "this is another demo command")
{
c2.StringVar(&c2Opts.str1, "str1", "def-val", "this is a string option with default value;;s")
c2.StringVar(&c2Opts.lOpt, "long-opt", "", "this is a string option with shorts;;lo")
c2.Func = func(c *cflag.Cmd) error {
dump.P(c2Opts)
return nil
}
}
app.Add(c1, c2)
app.Run()
}
func TestApp_Run(t *testing.T) {
app := cflag.NewApp(func(app *cflag.App) {
app.Name = "myapp"
app.Desc = "this is my cli application"
app.Version = "1.0.2"
})
// test invalid
assert.Panics(t, func() {
app.Add(cflag.NewCmd("", "empty name"))
})
// add command
var c1Opts = struct {
age int
name string
}{}
c1 := cflag.NewCmd("demo", "this is a demo command")
c1.OnAdd = func(c *cflag.Cmd) {
c.IntVar(&c1Opts.age, "age", 0, "this is a int option;;a")
c.StringVar(&c1Opts.name, "name", "", "this is a string option and required;true")
c.AddArg("arg1", "this is arg1", true, nil)
c.AddArg("arg2", "this is arg2", false, nil)
}
c1.Config(func(c *cflag.Cmd) {
c.Func = func(c *cflag.Cmd) error {
dump.P(c1Opts, c.Args())
return nil
}
})
app.Add(c1)
// repeat name
assert.Panics(t, func() {
app.Add(c1)
})
// add cmd by struct
app.Add(&cflag.Cmd{
Name: "demo2",
Desc: "this is demo2 command",
Func: func(c *cflag.Cmd) error {
dump.P("hi, on demo2 command")
return nil
},
})
// show help1
osArgs := os.Args
os.Args = []string{"./myapp"}
app.AfterHelpBuild = func(buf *strutil.Buffer) {
help := buf.String()
assert.StrContains(t, help, "-h, --help")
assert.StrContains(t, help, "demo")
assert.StrContains(t, help, "This is a demo command")
}
app.Run()
os.Args = osArgs
// show help2
buf := new(bytes.Buffer)
app.HelpWriter = buf
err := app.RunWithArgs([]string{"--help"})
assert.NoErr(t, err)
help := buf.String()
assert.StrContains(t, help, "-h, --help")
assert.StrContains(t, help, "demo")
assert.StrContains(t, help, "This is a demo command")
// run ... error
err = app.RunWithArgs([]string{"notExists"})
assert.ErrMsg(t, err, `input not exists command "notExists"`)
err = app.RunWithArgs([]string{"--invalid"})
assert.ErrMsg(t, err, `provide undefined flag option "--invalid"`)
// run
err = app.RunWithArgs([]string{"demo", "-a", "230", "--name", "inhere", "val1"})
assert.NoErr(t, err)
assert.Eq(t, 230, c1Opts.age)
assert.Eq(t, "inhere", c1Opts.name)
assert.Eq(t, "val1", c1.Arg("arg1").String())
}