Traverse parent contexts
This commit is contained in:
parent
e19a34c3c2
commit
4bce542ed7
@ -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…
Reference in New Issue
Block a user