Open
Description
Consider this (contrived simplified) example:
package main
import (
"fmt"
"github.com/alecthomas/kong"
)
type Cli struct {
CommonOptions
}
type CommonOptions struct {
VerboseOption
}
type VerboseOption struct {
Verbose bool
}
func (v VerboseOption) AfterApply() error {
fmt.Printf("Verbose is set to %v\n", v.Verbose)
return nil
}
func main() {
kong.Parse(&Cli{})
}
Upon execution, the output is:
Verbose is set to false
Verbose is set to false
Verbose is set to false
The hook has fired three times, unexpectedly. The issue is that the code at
Lines 149 to 170 in 9bc3bf9
(&Cli{}).AfterApply()
(&Cli{}).CommonOptions.AfterApply()
(&Cli{}).CommonOptions.VerboseOption.AfterApply()
This promotion into the embedding struct's method set only applies if there is no name conflict... so if the above is modified to add, e.g. a DebugOption
which also has an AfterApply
hook defined, as a sibling to VerboseOption
, then the problematic behaviour is suppressed.
Metadata
Metadata
Assignees
Labels
No labels