From decd71dc9ab27e74051594339c992542abcc9f24 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 a88dc49d31..b2e2f8db83 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 a7ad939dc6..4a27823942 100644 --- a/flag.go +++ b/flag.go @@ -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. @@ -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