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) {
|
func (cCtx *Context) onInvalidFlag(name string) {
|
||||||
if cCtx.App != nil && cCtx.App.InvalidFlagAccessHandler != nil {
|
for cCtx != nil {
|
||||||
cCtx.App.InvalidFlagAccessHandler(cCtx, name)
|
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) {
|
func TestContext_Value_InvalidFlagAccessHandler(t *testing.T) {
|
||||||
set := flag.NewFlagSet("test", 0)
|
|
||||||
var flagName string
|
var flagName string
|
||||||
app := &App{
|
app := &App{
|
||||||
InvalidFlagAccessHandler: func(_ *Context, name string) {
|
InvalidFlagAccessHandler: func(_ *Context, name string) {
|
||||||
flagName = name
|
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)
|
expect(t, app.Run([]string{"run", "command", "subcommand"}), nil)
|
||||||
c.Value("missing")
|
|
||||||
expect(t, flagName, "missing")
|
expect(t, flagName, "missing")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user