diff --git a/core/runner.go b/core/runner.go index ac9618b..3089a24 100644 --- a/core/runner.go +++ b/core/runner.go @@ -24,7 +24,7 @@ func (r *RadRunner) Run() error { pflag.CommandLine.ParseErrorsWhitelist.UnknownFlags = true pflag.Usage = func() { - r.RunUsageExit() + r.RunUsage() } r.globalFlags = CreateAndRegisterGlobalFlags() @@ -106,6 +106,7 @@ func (r *RadRunner) Run() error { pflag.CommandLine.ParseErrorsWhitelist.UnknownFlags = false // todo apparently this is not recommended, I should be using flagsets? I THINK I DO, FOR TESTS? + // RAD-67 will prevent double-error print (see https://github.com/spf13/pflag/issues/352) pflag.Parse() posArgsIndex := 0 diff --git a/core/testing/args_test.go b/core/testing/args_test.go index 64c01ed..2dd20d5 100644 --- a/core/testing/args_test.go +++ b/core/testing/args_test.go @@ -1,6 +1,8 @@ package testing -import "testing" +import ( + "testing" +) const ( setupArgRsl = ` @@ -312,3 +314,64 @@ print(name, isTall) assertOnlyOutput(t, stdOutBuffer, expected) resetTestState() } + +func TestArgs_MissingArgsPrintsUsageAndReturnsError(t *testing.T) { + rsl := ` +args: + name string + age int +` + setupAndRunCode(t, rsl, "alice", "--NO-COLOR") + expected := `Missing required arguments: [age] +Usage: + test + +Script args: + --name string + --age int + +` + globalFlagHelp + assertError(t, 1, expected) + resetTestState() +} + +func TestArgs_TooManyArgsPrintsUsageAndReturnsError(t *testing.T) { + rsl := ` +args: + name string + age int +` + setupAndRunCode(t, rsl, "alice", "2", "3", "--NO-COLOR") + expected := `Too many positional arguments. Unused: [3] +Usage: + test + +Script args: + --name string + --age int + +` + globalFlagHelp + assertError(t, 1, expected) + resetTestState() +} + +// todo RAD-67 - pflag currently ExitsOnError, I think that's why this test doesn't work +//func TestArgs_InvalidFlagPrintsUsageAndReturnsError(t *testing.T) { +// rsl := ` +//args: +// name string +// age int +//` +// fmt.Println("hi") +// setupAndRunCode(t, rsl, "alice", "2", "-s", "--NO-COLOR") +// expected := `Usage: +// test +// +//Script args: +// --name string +// --age int +// +//` + globalFlagHelp +// assertError(t, 1, expected) +// resetTestState() +//}