@@ -99,6 +99,9 @@ func flattenedFields(v reflect.Value) (out []flattenedField, err error) {
|
|||||||
return out, nil
|
return out, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Build a Node in the Kong data model.
|
||||||
|
//
|
||||||
|
// "v" is the value to create the node from, "typ" is the output Node type.
|
||||||
func buildNode(k *Kong, v reflect.Value, typ NodeType, seenFlags map[string]bool) (*Node, error) {
|
func buildNode(k *Kong, v reflect.Value, typ NodeType, seenFlags map[string]bool) (*Node, error) {
|
||||||
node := &Node{
|
node := &Node{
|
||||||
Type: typ,
|
Type: typ,
|
||||||
@@ -121,8 +124,16 @@ func buildNode(k *Kong, v reflect.Value, typ NodeType, seenFlags map[string]bool
|
|||||||
name = tag.Prefix + name
|
name = tag.Prefix + name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fieldType := ft.Type
|
||||||
|
// Hydrate command structs that are pointers.
|
||||||
|
if tag.Cmd && fieldType.Kind() == reflect.Ptr {
|
||||||
|
fv = reflect.New(fieldType.Elem()).Elem()
|
||||||
|
field.value = fv
|
||||||
|
v.FieldByIndex(field.field.Index).Set(fv.Addr())
|
||||||
|
}
|
||||||
|
|
||||||
// Nested structs are either commands or args, unless they implement the Mapper interface.
|
// Nested structs are either commands or args, unless they implement the Mapper interface.
|
||||||
if ft.Type.Kind() == reflect.Struct && (tag.Cmd || tag.Arg) && k.registry.ForValue(fv) == nil {
|
if field.value.Kind() == reflect.Struct && (tag.Cmd || tag.Arg) && k.registry.ForValue(fv) == nil {
|
||||||
typ := CommandNode
|
typ := CommandNode
|
||||||
if tag.Arg {
|
if tag.Arg {
|
||||||
typ = ArgumentNode
|
typ = ArgumentNode
|
||||||
|
|||||||
@@ -1282,3 +1282,18 @@ func TestDuplicateNestedShortFlags(t *testing.T) {
|
|||||||
_, err := kong.New(&cli)
|
_, err := kong.New(&cli)
|
||||||
require.EqualError(t, err, "<anonymous struct>.Flag2: duplicate short flag -t")
|
require.EqualError(t, err, "<anonymous struct>.Flag2: duplicate short flag -t")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestHydratePointerCommands(t *testing.T) {
|
||||||
|
type cmd struct {
|
||||||
|
Flag bool
|
||||||
|
}
|
||||||
|
|
||||||
|
var cli struct {
|
||||||
|
Cmd *cmd `cmd:""`
|
||||||
|
}
|
||||||
|
|
||||||
|
k := mustNew(t, &cli)
|
||||||
|
_, err := k.Parse([]string{"cmd", "--flag"})
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, &cmd{Flag: true}, cli.Cmd)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user