-
Notifications
You must be signed in to change notification settings - Fork 191
/
cflag_test.go
131 lines (108 loc) · 3.18 KB
/
cflag_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
package cflag_test
import (
"fmt"
"os"
"testing"
"github.com/gookit/goutil/cflag"
"github.com/gookit/goutil/cliutil"
"github.com/gookit/goutil/errorx"
"github.com/gookit/goutil/testutil/assert"
)
func Example() {
opts := struct {
age int
name string
str1 string
bol bool
}{}
c := cflag.New(func(c *cflag.CFlags) {
c.Desc = "this is a demo command"
c.Version = "0.5.1"
})
c.IntVar(&opts.age, "age", 0, "this is a int option;;a")
c.StringVar(&opts.name, "name", "", "this is a string option and required;true")
c.StringVar(&opts.str1, "str1", "def-val", "this is a string option with default value;;s")
c.AddArg("arg1", "this is arg1", true, nil)
c.AddArg("arg2", "this is arg2", true, nil)
c.AddArg("arg3", "this is arg3 with default", false, "def-val")
c.Func = func(c *cflag.CFlags) error {
// do something ...
cliutil.Infoln("hello, this is", c.Name())
cliutil.Infoln("option.age =", opts.age)
cliutil.Infoln("option.name =", opts.name)
cliutil.Infoln("option.str1 =", opts.str1)
cliutil.Infoln("arg1 =", c.Arg("arg1").String())
cliutil.Infoln("arg2 =", c.Arg("arg2").String())
cliutil.Infoln("arg3 =", c.Arg("arg3").String())
return nil
}
// c.MustParse(os.Args[1:])
c.MustParse(nil)
}
func TestSetDebug(t *testing.T) {
cflag.SetDebug(true)
assert.True(t, cflag.Debug)
cflag.SetDebug(false)
}
var opts = struct {
int int
str string
str1 string
bol bool
}{}
func TestNew(t *testing.T) {
c := cflag.New(
cflag.WithDesc("desc for the console command"),
cflag.WithVersion("1.0.2"),
)
c.IntVar(&opts.int, "int", 0, "this is a int option;true;i")
c.StringVar(&opts.str, "str", "", "this is a string option;;s")
c.StringVar(&opts.str1, "str1", "def-val", "this is a string option with default;;s1")
c.AddValidator("int", func(val any) error {
iv := val.(int)
if iv < 10 {
return errorx.Raw("value should >= 10")
}
return nil
})
c.LongHelp = "this is a long help\nthis is a long help\nthis is a long help"
c.Example = "this is some example for {{cmd}}\nthis is some example for {{cmd}}\nthis is some example for {{cmd}}"
c.AddArg("ag1", "this is a int option", false, nil)
c.AddArg("arg3", "this is arg2 with default", false, "def-val")
inArgs := []string{"--help"}
err := c.Parse(inArgs)
assert.NoErr(t, err)
inArgs = []string{"--int", "23"}
err = c.Parse(inArgs)
assert.NoErr(t, err)
assert.Eq(t, 23, opts.int)
// use validate
inArgs = []string{"--int", "3"}
err = c.Parse(inArgs)
assert.Err(t, err)
assert.Eq(t, "flag option 'int': value should >= 10", err.Error())
}
func TestCFlags_Parse(t *testing.T) {
var opts = struct {
int int
str string
str1 string
bol bool
}{}
c := cflag.New(func(c *cflag.CFlags) {
c.Desc = "this is a demo command"
c.Version = "0.5.1"
})
c.IntVar(&opts.int, "int", 0, "this is a int option;false;i")
assert.PanicsMsg(t, func() {
c.AddShortcuts("notExist", "d,e")
}, "cflag: option 'notExist' is not registered")
// assert.PanicsMsg(t, func() {
// c.AddShortcuts("int", "i,n")
// }, "cflag: option 'notExist' is not registered")
osArgs := os.Args
os.Args = []string{"./myapp", "ag1", "ag2"}
c.QuickRun()
assert.Eq(t, "[ag1 ag2]", fmt.Sprint(c.RemainArgs()))
os.Args = osArgs
}