Skip to content

Commit

Permalink
Add ActionableFlag interface instead of modifying Flag interface dire…
Browse files Browse the repository at this point in the history
…ctly
  • Loading branch information
xwjdsh committed Apr 30, 2022
1 parent fcfc8c0 commit decd71d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
6 changes: 4 additions & 2 deletions app.go
Expand Up @@ -524,8 +524,10 @@ func runFlagActions(c *Context, fs []Flag) error {
}
}
if isSet {
if err := f.RunAction(c); err != nil {
return err
if af, ok := f.(ActionableFlag); ok {
if err := af.RunAction(c); err != nil {
return err
}
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion flag.go
Expand Up @@ -85,6 +85,12 @@ func (f FlagsByName) Swap(i, j int) {
f[i], f[j] = f[j], f[i]
}

// ActionableFlag is an interface that wraps Flag interface and RunAction operation.
type ActionableFlag interface {
Flag
RunAction(*Context) error
}

// Flag is a common interface related to parsing flags in cli.
// For more advanced flag parsing techniques, it is recommended that
// this interface be implemented.
Expand All @@ -94,7 +100,6 @@ type Flag interface {
Apply(*flag.FlagSet) error
Names() []string
IsSet() bool
RunAction(*Context) error
}

// RequiredFlag is an interface that allows us to mark flags as required
Expand Down

0 comments on commit decd71d

Please sign in to comment.