Skip to content

Commit

Permalink
Traverse parent contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
icholy committed Aug 15, 2022
1 parent 75cb426 commit 84daa4a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
8 changes: 6 additions & 2 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,12 @@ func (cCtx *Context) checkRequiredFlags(flags []Flag) requiredFlagsErr {
}

func (cCtx *Context) onInvalidFlag(name string) {
if cCtx.App != nil && cCtx.App.InvalidFlagAccessHandler != nil {
cCtx.App.InvalidFlagAccessHandler(cCtx, name)
for cCtx != nil {
if cCtx.App != nil && cCtx.App.InvalidFlagAccessHandler != nil {
cCtx.App.InvalidFlagAccessHandler(cCtx, name)
break
}
cCtx = cCtx.parentContext
}
}

Expand Down
18 changes: 15 additions & 3 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 +151,27 @@ func TestContext_Value(t *testing.T) {
}

func TestContext_Value_InvalidFlagAccessHandler(t *testing.T) {
set := flag.NewFlagSet("test", 0)
var flagName string
app := &App{
InvalidFlagAccessHandler: func(_ *Context, name string) {
flagName = name
},
Commands: []*Command{
{
Name: "command",
Subcommands: []*Command{
{
Name: "subcommand",
Action: func(ctx *Context) error {
ctx.Value("missing")
return nil
},
},
},
},
},
}
c := NewContext(app, set, nil)
c.Value("missing")
expect(t, app.Run([]string{"run", "command", "subcommand"}), nil)
expect(t, flagName, "missing")
}

Expand Down

0 comments on commit 84daa4a

Please sign in to comment.