Skip to content

Commit

Permalink
fix some error on cmd sub match
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Feb 22, 2021
1 parent 4ee716c commit 125ec3a
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 69 deletions.
116 changes: 58 additions & 58 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,24 +106,6 @@ func NewCommand(name, desc string, fn ...func(c *Command)) *Command {
return c
}

// SetFunc Settings command handler func
func (c *Command) Match(nodes []string) *Command {
// must ensure is initialized
c.initialize()

ln := len(nodes)
if ln == 0 {
return c
}

return c.commandBase.Match(nodes)
}

// Match command by path. eg. "top:sub"
func (c *Command) MatchByPath(path string) *Command {
return c.Match(strings.Split(path, CommandSep))
}

// SetFunc Settings command handler func
func (c *Command) SetFunc(fn RunnerFunc) *Command {
c.Func = fn
Expand Down Expand Up @@ -181,6 +163,24 @@ func (c *Command) AddCommand(sub *Command) {
c.commandBase.addCommand(sub)
}

// SetFunc Settings command handler func
func (c *Command) Match(nodes []string) *Command {
// must ensure is initialized
c.initialize()

ln := len(nodes)
if ln == 0 {
return c
}

return c.commandBase.Match(nodes)
}

// Match command by path. eg. "top:sub"
func (c *Command) MatchByPath(path string) *Command {
return c.Match(strings.Split(path, CommandSep))
}

// init core
func (c *Command) initCore(cmdName string) {
c.core.cmdLine = CLI
Expand Down Expand Up @@ -258,20 +258,6 @@ func (c *Command) NotAlone() bool {
return !c.alone
}

// Module name of the grouped command
func (c *Command) ParentName() string {
if c.parent != nil {
return c.parent.Name
}

return ""
}

// SubName name of the grouped command
func (c *Command) SubName() string {
return c.subName
}

// ID get command ID name.
func (c *Command) goodName() string {
name := strings.Trim(strings.TrimSpace(c.Name), ": ")
Expand Down Expand Up @@ -311,6 +297,20 @@ func (c *Command) SetParent(parent *Command) {
c.parent = parent
}

// Module name of the grouped command
func (c *Command) ParentName() string {
if c.parent != nil {
return c.parent.Name
}

return ""
}

// SubName name of the grouped command
func (c *Command) SubName() string {
return c.subName
}

// find sub command by name
// func (c *Command) findSub(name string) *Command {
// if index, ok := c.subName2index[name]; ok {
Expand Down Expand Up @@ -393,36 +393,11 @@ func (c *Command) execute(args []string) (err error) {
return
}

// Fire event handler by name
func (c *Command) Fire(event string, data interface{}) {
Logf(VerbDebug, "command '%s' trigger the event: <mga>%s</>", c.Name, event)

c.Hooks.Fire(event, c, data)
}

// On add hook handler for a hook event
func (c *Command) On(name string, handler HookFunc) {
Logf(VerbDebug, "command '%s' add hook: %s", c.Name, name)

c.Hooks.On(name, handler)
}

// Copy a new command for current
func (c *Command) Copy() *Command {
nc := *c
// reset some fields
nc.Func = nil
nc.Hooks.ClearHooks()
// nc.Flags = flag.FlagSet{}

return &nc
}

/*************************************************************
* alone running
*************************************************************/

var errCallRun = errors.New("this method can only be called in standalone mode")
var errCallRun = errors.New("c.Run() method can only be called in standalone mode")

// MustRun Alone the current command, will panic on error
func (c *Command) MustRun(inArgs []string) {
Expand Down Expand Up @@ -485,6 +460,31 @@ func (c *Command) Run(inArgs []string) (err error) {
return c.execute(inArgs)
}

// Fire event handler by name
func (c *Command) Fire(event string, data interface{}) {
Logf(VerbDebug, "command '%s' trigger the event: <mga>%s</>", c.Name, event)

c.Hooks.Fire(event, c, data)
}

// On add hook handler for a hook event
func (c *Command) On(name string, handler HookFunc) {
Logf(VerbDebug, "command '%s' add hook: %s", c.Name, name)

c.Hooks.On(name, handler)
}

// Copy a new command for current
func (c *Command) Copy() *Command {
nc := *c
// reset some fields
nc.Func = nil
nc.Hooks.ClearHooks()
// nc.Flags = flag.FlagSet{}

return &nc
}

/*************************************************************
* command help
*************************************************************/
Expand Down
42 changes: 31 additions & 11 deletions cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/gookit/gcli/v3"
"github.com/gookit/goutil/dump"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -103,7 +104,7 @@ var r = &gcli.Command{
Name: "add",
Desc: "the clone command for git",
Func: func(c *gcli.Command, args []string) error {
c.Println(c.Name)
dump.Println(c.Name)
return nil
},
},
Expand All @@ -112,7 +113,7 @@ var r = &gcli.Command{
Name: "remote",
Desc: "remote command for git",
Func: func(c *gcli.Command, args []string) error {
c.Println(c.Name)
dump.Println(c.Name)
return nil
},
Subs: []*gcli.Command{
Expand All @@ -121,34 +122,53 @@ var r = &gcli.Command{
Name: "add",
Desc: "add command for git remote",
Func: func(c *gcli.Command, args []string) error {
c.Println(c.Name)
dump.Println(c.Name)
return nil
},
},
},
},
},
Func: func(c *gcli.Command, args []string) error {
c.Println(c.Name)
dump.Println(c.Name)
return nil
},
}

func TestCommand_Match(t *testing.T) {
err := r.Run([]string{"git"})
fmt.Println(err)
c := r.MatchByPath("git:add")
func TestCommand_MatchByPath(t *testing.T) {
c := r.MatchByPath("add")

assert.NotNil(t, c)
assert.Equal(t, "add", c.Name)
assert.Equal(t, "git", c.Parent().Name)
assert.Equal(t, "git", c.ParentName())

c = r.MatchByPath("remote:add")
assert.NotNil(t, c)
assert.Equal(t, "add", c.Name)
assert.Equal(t, "remote", c.Parent().Name)
assert.Equal(t, "git", c.Root().Name)

// empty will return self
c = r.MatchByPath("")
assert.NotNil(t, c)
assert.Equal(t, "git", c.Name)

c = r.MatchByPath("not-exist")
assert.Nil(t, c)
}

func TestCommand_RunWithSubs(t *testing.T) {
func TestCommand_Run_oneLevelSub(t *testing.T) {
err := r.Run([]string{"add", "./"})
fmt.Println(err)
}

err = r.Run([]string{"remote", "add", "origin", "https://github.com/inhere/goblog"})
func TestCommand_Run_moreLevelSub(t *testing.T) {
err := r.Run([]string{
"remote",
"add",
"origin",
"https://github.com/inhere/goblog",
})
fmt.Println(err)
}

Expand Down

0 comments on commit 125ec3a

Please sign in to comment.