urfave/cli

Execute `runFlagActions` before `c.Before`

axelrindle opened this issue · 3 comments

I have a flag named config which specifies the path to an alternative config file to use.
I do the config loading in the c.Before hook.

However I noticed that the flag actions are run after the c.Before hook.

cli/command.go

Lines 216 to 227 in d391482

if c.Before != nil && !cCtx.shellComplete {
beforeErr := c.Before(cCtx)
if beforeErr != nil {
cCtx.App.handleExitCoder(cCtx, beforeErr)
err = beforeErr
return err
}
}
if err = runFlagActions(cCtx, c.Flags); err != nil {
return err
}

How can I achieve the flag action being run before the c.Before hook?

@axelrindle As of now it is not possible. There has been some discussion on lifecycle hooks for v3. See #1273 . If you have suggestions of how you'd like to improve this behavior it would be great.

The concept of lifecycle hooks sounds great to me. I'd like to propose the following:

  • beforeFlags (runs before flags are parsed)
  • afterFlags (runs after flags are parsed, but before commands are run)
  • beforeCommand (runs before command actions are run; maybe merge with the previous?)
  • afterCommand (runs after a command action has been run)

@axelrindle I'm going to move this conversation to #1273 and close this issue