NamedMapper uses defaults from non-selected commands
Author: hindessmCreated Sep 28, 2026Updated Sep 28, 2026
This is somewhat similar to the issue in #619 but for defaults on named mappers. A simple reproducer would be:
package main
import (
"fmt"
"reflect"
"github.com/alecthomas/kong"
)
func main() {
var cli struct {
CmdA struct {
Val string `optional:"" type:"mytype" default:"foo"`
} `cmd:""`
CmdB struct {
Val string `optional:"" type:"mytype" default:"bar"`
} `cmd:""`
}
defaults := []string{}
k, _ := kong.New(&cli, kong.NamedMapper("mytype", kong.MapperFunc(
func(ctx *kong.DecodeContext, target reflect.Value) error {
var val string
_ = ctx.Scan.PopValueInto("val", &val)
defaults = append(defaults, val)
target.SetString(val)
return nil
},
)))
k.Parse([]string{"cmd-a"})
fmt.Println(defaults) // expected [foo] but is [foo bar]
}Source: alecthomas/kong