From 8652a46dd7a38633a8c7f5bfe06822a6964dd9af Mon Sep 17 00:00:00 2001 From: Hedzr Yeh Date: Sun, 9 Mar 2025 09:14:58 +0800 Subject: [PATCH] cmdr: add App.OnAction --- builder/app.go | 27 +++++++++++++++++++++++++++ cli/app.go | 2 ++ cli/for_test.go | 4 ++++ 3 files changed, 33 insertions(+) diff --git a/builder/app.go b/builder/app.go index b45b01f..6f0d6ad 100644 --- a/builder/app.go +++ b/builder/app.go @@ -221,6 +221,33 @@ func (s *appS) RootBuilder(cb func(b cli.CommandBuilder)) cli.App { return s } +func (s *appS) OnAction(handler cli.OnInvokeHandler) cli.App { + s.RootBuilder(func(b cli.CommandBuilder) { + b.OnAction(handler) + }) + return s +} + +// func (s *ccb) OnAction(handler cli.OnInvokeHandler) cli.CommandBuilder { +// s.SetAction(handler) +// return s +// } +// +// func (s *ccb) OnPreAction(handlers ...cli.OnPreInvokeHandler) cli.CommandBuilder { +// s.SetPreActions(handlers...) +// return s +// } +// +// func (s *ccb) OnPostAction(handlers ...cli.OnPostInvokeHandler) cli.CommandBuilder { +// s.SetPostActions(handlers...) +// return s +// } +// +// func (s *ccb) OnMatched(handler cli.OnCommandMatchedHandler) cli.CommandBuilder { +// s.SetOnMatched(handler) +// return s +// } + func (s *appS) AddCmd(cb func(b cli.CommandBuilder)) cli.App { b := newCommandBuilderShort(s, "") defer b.Build() diff --git a/cli/app.go b/cli/app.go index 7f8dce9..8a83bd0 100644 --- a/cli/app.go +++ b/cli/app.go @@ -45,6 +45,8 @@ type App interface { Header(headerLine string) App // setup header line(s) instead of copyright+author fields Footer(footerLine string) App // setup footer line(s) + OnAction(handler OnInvokeHandler) App + // Examples(examples ...string) App // set examples field of root command RootCommand() *RootCommand // get root command diff --git a/cli/for_test.go b/cli/for_test.go index 571b383..a3fff30 100644 --- a/cli/for_test.go +++ b/cli/for_test.go @@ -1091,6 +1091,10 @@ func (s *appS) Footer(footerLine string) App { return s } +func (s *appS) OnAction(handler OnInvokeHandler) App { + return s +} + func (s *appS) SetRootCommand(root *RootCommand) App { s.root = root return s