Skip to content

Commit 0c0e1b8

Browse files
committed
👔 chore: all - update some tests and example codes
1 parent 0a4a98c commit 0c0e1b8

File tree

6 files changed

+56
-6
lines changed

6 files changed

+56
-6
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ Including running commands, color styles, data display, progress display, intera
5353

5454
**Flag Arguments**:
5555

56-
- Support binding named arguemnt
56+
- Support binding named argument
5757
- Support define array argument
5858

5959
## GoDoc

_examples/cliapp/main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ func main() {
4747
// gcli.GOpts().SetDisable()
4848

4949
// app.BeforeAddOpts = func(opts *gcli.Flags) {
50-
// opts.StrVar(&customGOpt, &gcli.FlagMeta{Name: "custom", Desc: "desc message for the option"})
50+
// opts.StrVar(&customGOpt, &gcli.CliOpt{Name: "custom", Desc: "desc message for the option"})
5151
// }
5252

5353
app.On(events.OnAppBindOptsAfter, func(ctx *gcli.HookCtx) (stop bool) {
54-
ctx.App.Flags().StrVar(&customGOpt, &gcli.FlagMeta{
54+
ctx.App.Flags().StrVar(&customGOpt, &gcli.CliOpt{
5555
Name: "custom",
5656
Desc: "desc message for the option",
5757
})

_examples/multilevel/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ var cmd = gcli.Command{
3232
Name: "l2sub1",
3333
Desc: "desc message",
3434
Config: func(c *gcli.Command) {
35-
c.StrVar(&l2sub1opts.astr, &gcli.FlagMeta{
35+
c.StrVar(&l2sub1opts.astr, &gcli.CliOpt{
3636
Name: "astr",
3737
Desc: "desc for astr",
3838
})

app_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,9 @@ func TestApp_Run_command_withOptions(t *testing.T) {
294294
}()
295295

296296
app.Run([]string{"test", "-h"})
297-
is.Contains(buf.String(), "-o, --opt1 string")
297+
s := buf.String()
298+
fmt.Println(s)
299+
is.Contains(s, "-o, --opt1 string")
298300
}
299301

300302
func TestApp_Run_subcommand(t *testing.T) {

cmd_test.go

+47
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,53 @@ func TestCommand_NewErrf(t *testing.T) {
6767
})
6868
}
6969

70+
func TestCommand_AddArg(t *testing.T) {
71+
is := assert.New(t)
72+
c := gcli.NewCommand("test", "test desc", nil)
73+
74+
arg := c.AddArg("arg0", "arg desc", true)
75+
is.Eq(0, arg.Index())
76+
77+
ret := c.ArgByIndex(0)
78+
is.Eq(ret, arg)
79+
80+
assert.PanicsMsg(t, func() {
81+
c.ArgByIndex(1)
82+
}, "gflag: get not exists argument #1")
83+
84+
arg = c.AddArg("arg1", "arg1 desc")
85+
is.Eq(1, arg.Index())
86+
87+
ret = c.Arg("arg1")
88+
is.Eq(ret, arg)
89+
90+
is.PanicsMsg(func() {
91+
c.Arg("not-exist")
92+
}, "gflag: get not exists argument 'not-exist'")
93+
94+
is.Len(c.Args(), 2)
95+
96+
is.PanicsMsg(func() {
97+
c.AddArg("", "desc")
98+
}, "gflag: the command argument name cannot be empty")
99+
100+
is.PanicsMsg(func() {
101+
c.AddArg(":)&dfd", "desc")
102+
}, "gflag: the argument name ':)&dfd' is invalid, must match: ^[a-zA-Z][\\w-]*$")
103+
104+
is.PanicsMsg(func() {
105+
c.AddArg("arg1", "desc")
106+
}, "gflag: the argument name 'arg1' already exists in command 'test'")
107+
is.PanicsMsg(func() {
108+
c.AddArg("arg2", "arg2 desc", true)
109+
}, "gflag: required argument 'arg2' cannot be defined after optional argument")
110+
111+
c.AddArg("arg3", "arg3 desc", false, true)
112+
is.PanicsMsg(func() {
113+
c.AddArg("argN", "desc", true)
114+
}, "gflag: have defined an array argument, you cannot add argument 'argN'")
115+
}
116+
70117
func TestCommand_Run(t *testing.T) {
71118
is := assert.New(t)
72119

gcli.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ func GCtx() *Context {
7575
// Flags alias of the gflag.Parser
7676
type Flags = gflag.Parser
7777

78-
// FlagMeta alias of the gflag.CliOpt
78+
// FlagMeta alias of the gflag.CliOpt.
79+
// Deprecated: use CliOpt instead
7980
type FlagMeta = gflag.CliOpt
8081

8182
// CliOpt alias of the gflag.CliOpt

0 commit comments

Comments
 (0)