From ac0d31e06d737e6834ec5e9e640e1ffc928e87c6 Mon Sep 17 00:00:00 2001 From: Hedzr Yeh Date: Mon, 16 Sep 2024 07:09:36 +0800 Subject: [PATCH] added root.SetApp and more for concise testing --- cli/for_test.go | 2 ++ cli/root.go | 35 +++++++++++++++++++++++++++++++++++ cli/root_test.go | 3 +++ 3 files changed, 40 insertions(+) create mode 100644 cli/root_test.go diff --git a/cli/for_test.go b/cli/for_test.go index 5e982ff..ab52916 100644 --- a/cli/for_test.go +++ b/cli/for_test.go @@ -925,6 +925,7 @@ func newTestRunner() Runner { return &workerS{store.New()} } +// workerS for testing only type workerS struct { store store.Store } @@ -946,6 +947,7 @@ func (*workerS) Root() *RootCommand { return nil } // +// appS for testing only type appS struct { Runner root *RootCommand diff --git a/cli/root.go b/cli/root.go index 34351ae..c749c28 100644 --- a/cli/root.go +++ b/cli/root.go @@ -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 diff --git a/cli/root_test.go b/cli/root_test.go new file mode 100644 index 0000000..4c58e5e --- /dev/null +++ b/cli/root_test.go @@ -0,0 +1,3 @@ +package cli + +var ()