diff --git a/cmd.go b/cmd.go index c5d2671..0c0bdbb 100644 --- a/cmd.go +++ b/cmd.go @@ -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 @@ -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 @@ -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), ": ") @@ -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 { @@ -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: %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) { @@ -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: %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 *************************************************************/ diff --git a/cmd_test.go b/cmd_test.go index 56280ba..cdb29f7 100644 --- a/cmd_test.go +++ b/cmd_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/gookit/gcli/v3" + "github.com/gookit/goutil/dump" "github.com/stretchr/testify/assert" ) @@ -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 }, }, @@ -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{ @@ -121,7 +122,7 @@ 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 }, }, @@ -129,26 +130,45 @@ var r = &gcli.Command{ }, }, 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) }