Skip to content

Commit

Permalink
✨ up: cflag - app add new hook AfterRun on cmd run after
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Nov 2, 2024
1 parent c5e194a commit aef1d43
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions cflag/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ type App struct {
// AfterHelpBuild hook
AfterHelpBuild func(buf *strutil.Buffer)
// BeforeRun hook func
// - cmdArgs: input args for current command, not parsed.
// - cmdArgs: input raw args for current command.
// - return false to stop run.
BeforeRun func(c *Cmd, cmdArgs []string) bool
// AfterRun hook func
AfterRun func(c *Cmd, err error)
}

// NewApp instance
Expand Down Expand Up @@ -130,7 +132,14 @@ func (a *App) RunWithArgs(args []string) error {
if a.BeforeRun != nil && !a.BeforeRun(cmd, cmdArgs) {
return nil
}
return cmd.Parse(cmdArgs)

err := cmd.Parse(cmdArgs)

// fire after run hook
if a.AfterRun != nil {
a.AfterRun(cmd, err)
}
return err
}

func (a *App) init() {
Expand Down

0 comments on commit aef1d43

Please sign in to comment.