Skip to content

Hook ignored on nodes tagged with "embed"  #90

@mkmik

Description

@mkmik

In the following example I created a struct called Common with an AfterApply hook.
When this structure is embedded using Go struct embedding, the AfterApply hook gets invoked as per Go method discovery rules.

However, if the struct is embedded using the kong embedding (via the embed tag), the hook is not invoked.

I think that would be useful since this allows multiple structures with common flags to be embedded and have each of their hooks invoked. This is currently not possible with Go embedding since it doesn't work if more than one structure (including the containing struct) defines the hook method.

package main

import (
	"fmt"

	"github.com/alecthomas/kong"
)

type Common struct {
	Foo bool `help:"FOO"`
}

func (*Common) AfterApply() error {
	fmt.Printf("called Common.AfterApply\n")
	return nil
}

var CLI struct {
	Rm struct {
		Common

		Force     bool `help:"Force removal."`
		Recursive bool `help:"Recursively remove files."`

		Paths []string `arg name:"path" help:"Paths to remove." type:"path"`
	} `cmd help:"Remove files."`

	Ls struct {
		C Common `embed:""`

		Paths []string `arg optional name:"path" help:"Paths to list." type:"path"`
	} `cmd help:"List paths."`
}

func main() {
	ctx := kong.Parse(&CLI)
	switch ctx.Command() {
	case "rm <path>":
	case "ls":
	default:
		panic(ctx.Command())
	}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions