Skip to content

Commit

Permalink
Merge pull request #1337 from xwjdsh/feat/flag-action
Browse files Browse the repository at this point in the history
Flag-level Action
  • Loading branch information
dearchap authored Sep 22, 2022
2 parents 9f465af + 47f6782 commit a81e201
Show file tree
Hide file tree
Showing 24 changed files with 747 additions and 3 deletions.
28 changes: 28 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,10 @@ func (a *App) RunContext(ctx context.Context, arguments []string) (err error) {
}
}

if err = runFlagActions(cCtx, a.Flags); err != nil {
return err
}

var c *Command
args := cCtx.Args()
if args.Present() {
Expand Down Expand Up @@ -523,6 +527,10 @@ func (a *App) RunAsSubcommand(ctx *Context) (err error) {
}
}

if err = runFlagActions(cCtx, a.Flags); err != nil {
return err
}

args := cCtx.Args()
if args.Present() {
name := args.First()
Expand Down Expand Up @@ -646,6 +654,26 @@ func (a *App) argsWithDefaultCommand(oldArgs Args) Args {
return oldArgs
}

func runFlagActions(c *Context, fs []Flag) error {
for _, f := range fs {
isSet := false
for _, name := range f.Names() {
if c.IsSet(name) {
isSet = true
break
}
}
if isSet {
if af, ok := f.(ActionableFlag); ok {
if err := af.RunAction(c); err != nil {
return err
}
}
}
}
return nil
}

// Author represents someone who has contributed to a cli project.
type Author struct {
Name string // The Authors name
Expand Down
Loading

0 comments on commit a81e201

Please sign in to comment.