-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathreorder_app_test.go
More file actions
75 lines (67 loc) · 1.72 KB
/
Copy pathreorder_app_test.go
File metadata and controls
75 lines (67 loc) · 1.72 KB
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
package gcli_test
import (
"testing"
"github.com/gookit/gcli/v3"
"github.com/gookit/goutil/x/assert"
)
// options written after arguments are still parsed on the executed command.
func TestApp_Run_reorderArgs(t *testing.T) {
is := assert.New(t)
app := gcli.NewApp(gcli.NotExitOnEnd())
var name string
var force bool
var a0, a1 string
app.Add(&gcli.Command{
Name: "test",
Desc: "desc for test command",
Config: func(c *gcli.Command) {
c.StrOpt(&name, "name", "n", "", "name opt")
c.BoolOpt(&force, "force", "f", false, "force opt")
c.AddArg("arg0", "arg0 desc")
c.AddArg("arg1", "arg1 desc")
},
Func: func(c *gcli.Command, _ []string) error {
a0 = c.Arg("arg0").String()
a1 = c.Arg("arg1").String()
return nil
},
})
code := app.Run([]string{"test", "v0", "--name", "tom", "v1", "-f"})
is.Eq(0, code)
is.Eq("tom", name)
is.True(force)
is.Eq("v0", a0)
is.Eq("v1", a1)
}
// in a multi-level app, only the final executed command's args are reordered.
func TestApp_Run_reorderArgs_multiLevel(t *testing.T) {
is := assert.New(t)
app := gcli.NewApp(gcli.NotExitOnEnd())
var subName string
var a0, a1 string
app.Add(&gcli.Command{
Name: "top",
Desc: "top command",
Subs: []*gcli.Command{
{
Name: "sub",
Desc: "sub command",
Config: func(c *gcli.Command) {
c.StrOpt(&subName, "name", "n", "", "name opt")
c.AddArg("arg0", "arg0 desc")
c.AddArg("arg1", "arg1 desc")
},
Func: func(c *gcli.Command, _ []string) error {
a0 = c.Arg("arg0").String()
a1 = c.Arg("arg1").String()
return nil
},
},
},
})
code := app.Run([]string{"top", "sub", "f1", "--name", "tom", "f2"})
is.Eq(0, code)
is.Eq("tom", subName)
is.Eq("f1", a0)
is.Eq("f2", a1)
}