Skip to content

Commit

Permalink
added root.SetApp and more for concise testing
Browse files Browse the repository at this point in the history
  • Loading branch information
hedzr committed Sep 15, 2024
1 parent eef03a5 commit ac0d31e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cli/for_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -925,6 +925,7 @@ func newTestRunner() Runner {
return &workerS{store.New()}
}

// workerS for testing only
type workerS struct {
store store.Store
}
Expand All @@ -946,6 +947,7 @@ func (*workerS) Root() *RootCommand { return nil }

//

// appS for testing only
type appS struct {
Runner
root *RootCommand
Expand Down
35 changes: 35 additions & 0 deletions cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,41 @@ import (
"time"
)

func (c *RootCommand) SetApp(app App) *RootCommand {
c.app = app
c.root = c
if a, ok := app.(interface {
WithRootCommand(command *RootCommand) App
}); ok {
a.WithRootCommand(c)
}
return c
}

func (c *RootCommand) NewCmd(longTitle string) *Command {
cc := &Command{
BaseOpt: BaseOpt{
Long: longTitle,
owner: c.Command,
root: c,
},
}
c.AddSubCommand(cc)
return cc
}

func (c *RootCommand) NewFlg(longTitle string) *Flag {
cc := &Flag{
BaseOpt: BaseOpt{
Long: longTitle,
owner: c.root.Command,
root: c.root,
},
}
c.AddFlag(cc)
return cc
}

// Attach attaches new root command on it
func (c *RootCommand) Attach(newRootCommand *Command) {
c.Command = newRootCommand
Expand Down
3 changes: 3 additions & 0 deletions cli/root_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package cli

var ()

0 comments on commit ac0d31e

Please sign in to comment.