Traverse parent contexts

main
Ilia Choly 2 years ago committed by Dan Buch
parent e19a34c3c2
commit 4bce542ed7
Signed by: meatballhat
GPG Key ID: A12F782281063434

@ -194,8 +194,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
}
}

@ -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")
}

Loading…
Cancel
Save