Traverse parent contexts

This commit is contained in:
Ilia Choly 2022-08-15 16:46:17 -04:00 committed by Dan Buch
parent e19a34c3c2
commit 4bce542ed7
Signed by: meatballhat
GPG Key ID: A12F782281063434
2 changed files with 21 additions and 5 deletions

View File

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

View File

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