Skip to content

Commit

Permalink
cmdr: add App.OnAction
Browse files Browse the repository at this point in the history
  • Loading branch information
hedzr committed Mar 9, 2025
1 parent 2e63664 commit 8652a46
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
27 changes: 27 additions & 0 deletions builder/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 2 additions & 0 deletions cli/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions cli/for_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8652a46

Please sign in to comment.