Add ActionableFlag interface instead of modifying Flag interface directly
This commit is contained in:
parent
14366f7030
commit
619958c3d2
6
app.go
6
app.go
@ -664,8 +664,10 @@ func runFlagActions(c *Context, fs []Flag) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if isSet {
|
if isSet {
|
||||||
if err := f.RunAction(c); err != nil {
|
if af, ok := f.(ActionableFlag); ok {
|
||||||
return err
|
if err := af.RunAction(c); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
7
flag.go
7
flag.go
@ -83,6 +83,12 @@ func (f FlagsByName) Swap(i, j int) {
|
|||||||
f[i], f[j] = f[j], f[i]
|
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.
|
// Flag is a common interface related to parsing flags in cli.
|
||||||
// For more advanced flag parsing techniques, it is recommended that
|
// For more advanced flag parsing techniques, it is recommended that
|
||||||
// this interface be implemented.
|
// this interface be implemented.
|
||||||
@ -92,7 +98,6 @@ type Flag interface {
|
|||||||
Apply(*flag.FlagSet) error
|
Apply(*flag.FlagSet) error
|
||||||
Names() []string
|
Names() []string
|
||||||
IsSet() bool
|
IsSet() bool
|
||||||
RunAction(*Context) error
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// RequiredFlag is an interface that allows us to mark flags as required
|
// RequiredFlag is an interface that allows us to mark flags as required
|
||||||
|
Loading…
x
Reference in New Issue
Block a user