From b4b58270446cd24859c6956afd095568c6cc7343 Mon Sep 17 00:00:00 2001 From: Gerald Kaszuba Date: Fri, 18 May 2018 18:38:53 +1000 Subject: [PATCH] Matching arg field (#1) Check if the first field of an argument branch matches the parent struct field. --- build.go | 4 ++++ kong_test.go | 13 ++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/build.go b/build.go index d867222..8b4834e 100644 --- a/build.go +++ b/build.go @@ -79,6 +79,10 @@ func buildNode(v reflect.Value, cmd bool) *Node { child.Help = value.Help } child.Name = value.Name + if child.Name != name { + fail("first field in positional branch %s.%s must have the same name as the parent field (%s).", + v.Type().Name(), ft.Name, child.Name) + } node.Children = append(node.Children, &Branch{Argument: &Argument{ Node: *child, Argument: value, diff --git a/kong_test.go b/kong_test.go index 72ec784..007a317 100644 --- a/kong_test.go +++ b/kong_test.go @@ -98,10 +98,21 @@ func TestArgSlice(t *testing.T) { require.Equal(t, true, cli.Flag) } -func TestUnsupportedfieldErrors(t *testing.T) { +func TestUnsupportedFieldErrors(t *testing.T) { var cli struct { Keys map[string]string } _, err := New("", "", &cli) require.Error(t, err) } + +func TestMatchingArgField(t *testing.T) { + var cli struct { + ID struct { + NotID int `arg:""` + } `arg:""` + } + + _, err := New("", "", &cli) + require.Error(t, err) +}