Skip to content

Commit

Permalink
✨ up: cflag - app add new hook BeforeRun and call os.Exit on run error
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Oct 23, 2024
1 parent a2decab commit e0591f5
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion cflag/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ type App struct {

// AfterHelpBuild hook
AfterHelpBuild func(buf *strutil.Buffer)
// BeforeRun hook func
BeforeRun func(c *Cmd) bool
}

// NewApp instance
Expand Down Expand Up @@ -97,6 +99,7 @@ func (a *App) Run() {
err := a.RunWithArgs(os.Args[1:])
if err != nil {
cliutil.Errorln("ERROR:", err)
os.Exit(1)
}
}

Expand All @@ -112,7 +115,6 @@ func (a *App) RunWithArgs(args []string) error {
if name == "help" || name == "--help" || name == "-h" {
return a.showHelp()
}

if name[0] == '-' {
return fmt.Errorf("provide undefined flag option %q", name)
}
Expand All @@ -122,6 +124,9 @@ func (a *App) RunWithArgs(args []string) error {
return fmt.Errorf("input not exists command %q", name)
}

if a.BeforeRun != nil && !a.BeforeRun(cmd) {
return nil
}
return cmd.Parse(args[1:])
}

Expand Down

0 comments on commit e0591f5

Please sign in to comment.