Skip to content

Commit

Permalink
👔 chore: all - update some tests and example codes
Browse files Browse the repository at this point in the history
inhere committed Aug 25, 2023
1 parent 0a4a98c commit 0c0e1b8
Showing 6 changed files with 56 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -53,7 +53,7 @@ Including running commands, color styles, data display, progress display, intera

**Flag Arguments**:

- Support binding named arguemnt
- Support binding named argument
- Support define array argument

## GoDoc
4 changes: 2 additions & 2 deletions _examples/cliapp/main.go
Original file line number Diff line number Diff line change
@@ -47,11 +47,11 @@ func main() {
// gcli.GOpts().SetDisable()

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

app.On(events.OnAppBindOptsAfter, func(ctx *gcli.HookCtx) (stop bool) {
ctx.App.Flags().StrVar(&customGOpt, &gcli.FlagMeta{
ctx.App.Flags().StrVar(&customGOpt, &gcli.CliOpt{
Name: "custom",
Desc: "desc message for the option",
})
2 changes: 1 addition & 1 deletion _examples/multilevel/main.go
Original file line number Diff line number Diff line change
@@ -32,7 +32,7 @@ var cmd = gcli.Command{
Name: "l2sub1",
Desc: "desc message",
Config: func(c *gcli.Command) {
c.StrVar(&l2sub1opts.astr, &gcli.FlagMeta{
c.StrVar(&l2sub1opts.astr, &gcli.CliOpt{
Name: "astr",
Desc: "desc for astr",
})
4 changes: 3 additions & 1 deletion app_test.go
Original file line number Diff line number Diff line change
@@ -294,7 +294,9 @@ func TestApp_Run_command_withOptions(t *testing.T) {
}()

app.Run([]string{"test", "-h"})
is.Contains(buf.String(), "-o, --opt1 string")
s := buf.String()
fmt.Println(s)
is.Contains(s, "-o, --opt1 string")
}

func TestApp_Run_subcommand(t *testing.T) {
47 changes: 47 additions & 0 deletions cmd_test.go
Original file line number Diff line number Diff line change
@@ -67,6 +67,53 @@ func TestCommand_NewErrf(t *testing.T) {
})
}

func TestCommand_AddArg(t *testing.T) {
is := assert.New(t)
c := gcli.NewCommand("test", "test desc", nil)

arg := c.AddArg("arg0", "arg desc", true)
is.Eq(0, arg.Index())

ret := c.ArgByIndex(0)
is.Eq(ret, arg)

assert.PanicsMsg(t, func() {
c.ArgByIndex(1)
}, "gflag: get not exists argument #1")

arg = c.AddArg("arg1", "arg1 desc")
is.Eq(1, arg.Index())

ret = c.Arg("arg1")
is.Eq(ret, arg)

is.PanicsMsg(func() {
c.Arg("not-exist")
}, "gflag: get not exists argument 'not-exist'")

is.Len(c.Args(), 2)

is.PanicsMsg(func() {
c.AddArg("", "desc")
}, "gflag: the command argument name cannot be empty")

is.PanicsMsg(func() {
c.AddArg(":)&dfd", "desc")
}, "gflag: the argument name ':)&dfd' is invalid, must match: ^[a-zA-Z][\\w-]*$")

is.PanicsMsg(func() {
c.AddArg("arg1", "desc")
}, "gflag: the argument name 'arg1' already exists in command 'test'")
is.PanicsMsg(func() {
c.AddArg("arg2", "arg2 desc", true)
}, "gflag: required argument 'arg2' cannot be defined after optional argument")

c.AddArg("arg3", "arg3 desc", false, true)
is.PanicsMsg(func() {
c.AddArg("argN", "desc", true)
}, "gflag: have defined an array argument, you cannot add argument 'argN'")
}

func TestCommand_Run(t *testing.T) {
is := assert.New(t)

3 changes: 2 additions & 1 deletion gcli.go
Original file line number Diff line number Diff line change
@@ -75,7 +75,8 @@ func GCtx() *Context {
// Flags alias of the gflag.Parser
type Flags = gflag.Parser

// FlagMeta alias of the gflag.CliOpt
// FlagMeta alias of the gflag.CliOpt.
// Deprecated: use CliOpt instead
type FlagMeta = gflag.CliOpt

// CliOpt alias of the gflag.CliOpt

0 comments on commit 0c0e1b8

Please sign in to comment.