Skip to content

Commit a81e201

Browse files
authored
Merge pull request #1337 from xwjdsh/feat/flag-action
Flag-level Action
2 parents 9f465af + 47f6782 commit a81e201

24 files changed

+747
-3
lines changed

app.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,10 @@ func (a *App) RunContext(ctx context.Context, arguments []string) (err error) {
342342
}
343343
}
344344

345+
if err = runFlagActions(cCtx, a.Flags); err != nil {
346+
return err
347+
}
348+
345349
var c *Command
346350
args := cCtx.Args()
347351
if args.Present() {
@@ -523,6 +527,10 @@ func (a *App) RunAsSubcommand(ctx *Context) (err error) {
523527
}
524528
}
525529

530+
if err = runFlagActions(cCtx, a.Flags); err != nil {
531+
return err
532+
}
533+
526534
args := cCtx.Args()
527535
if args.Present() {
528536
name := args.First()
@@ -646,6 +654,26 @@ func (a *App) argsWithDefaultCommand(oldArgs Args) Args {
646654
return oldArgs
647655
}
648656

657+
func runFlagActions(c *Context, fs []Flag) error {
658+
for _, f := range fs {
659+
isSet := false
660+
for _, name := range f.Names() {
661+
if c.IsSet(name) {
662+
isSet = true
663+
break
664+
}
665+
}
666+
if isSet {
667+
if af, ok := f.(ActionableFlag); ok {
668+
if err := af.RunAction(c); err != nil {
669+
return err
670+
}
671+
}
672+
}
673+
}
674+
return nil
675+
}
676+
649677
// Author represents someone who has contributed to a cli project.
650678
type Author struct {
651679
Name string // The Authors name

0 commit comments

Comments
 (0)