From ad8c121a84d5baa522054e7b84f2c24c4ecbdbe2 Mon Sep 17 00:00:00 2001 From: Wendell Sun Date: Sun, 1 May 2022 00:24:13 +0800 Subject: [PATCH] Add ActionableFlag interface instead of modifying Flag interface directly --- app.go | 6 ++++-- flag.go | 7 ++++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/app.go b/app.go index 99f31a8532..7b217b5b0f 100644 --- a/app.go +++ b/app.go @@ -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 + } } } } diff --git a/flag.go b/flag.go index b8d49c9254..9aaed98315 100644 --- a/flag.go +++ b/flag.go @@ -84,6 +84,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. @@ -93,7 +99,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