Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flag-level Action #1337

Merged
merged 6 commits into from Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 28 additions & 0 deletions app.go
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
xwjdsh marked this conversation as resolved.
Show resolved Hide resolved
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