From 3b21a540942367e362db7c5a3ec9b690ed3a853b Mon Sep 17 00:00:00 2001 From: Alec Thomas Date: Fri, 18 May 2018 21:30:44 +1000 Subject: [PATCH] More validation. --- build.go | 11 +++++++++-- kong_test.go | 10 ++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/build.go b/build.go index 8b4834e..656c452 100644 --- a/build.go +++ b/build.go @@ -23,7 +23,11 @@ func build(ast interface{}) (app *Application, err error) { return nil, fmt.Errorf("expected a pointer to a struct but got %T", ast) } - return buildNode(iv, true), nil + node := buildNode(iv, true) + if len(node.Positional) > 0 && len(node.Children) > 0 { + return nil, fmt.Errorf("can't mix positional arguments and branching arguments on %T", ast) + } + return node, nil } func buildNode(v reflect.Value, cmd bool) *Node { @@ -91,6 +95,10 @@ func buildNode(v reflect.Value, cmd bool) *Node { child.Name = name node.Children = append(node.Children, &Branch{Command: child}) } + + if len(child.Positional) > 0 && len(child.Children) > 0 { + fail("can't mix positional arguments and branching arguments on %s.%s", v.Type().Name(), ft.Name) + } } else { if decoder == nil { fail("no decoder for %s.%s (of type %s)", v.Type(), ft.Name, ft.Type) @@ -118,6 +126,5 @@ func buildNode(v reflect.Value, cmd bool) *Node { } } } - return node } diff --git a/kong_test.go b/kong_test.go index 99f1621..edf7238 100644 --- a/kong_test.go +++ b/kong_test.go @@ -124,3 +124,13 @@ func TestMatchingArgField(t *testing.T) { _, err := New(&cli) require.Error(t, err) } + +func TestCantMixPositionalAndBranches(t *testing.T) { + var cli struct { + Arg string `arg:""` + Command struct { + } `cmd:""` + } + _, err := New(&cli) + require.Error(t, err) +}