From c32a8830bd5d8bc203d21d0b921790737d767f4a Mon Sep 17 00:00:00 2001 From: Rob Pike Date: Wed, 14 Aug 2013 07:03:18 +1000 Subject: [PATCH] cmd/go: nicer error diagnosis in go test Before, go test -bench . would just dump the long generic "go help" message. Confusing and unhelpful. Now the message is short and on point and also reminds the user about the oft-forgotten "go help testflag". % go test -bench go test: missing argument for flag bench run "go help test" or "go help testflag" for more information % R=rsc CC=golang-dev https://golang.org/cl/12662046 --- src/cmd/go/testflag.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/cmd/go/testflag.go b/src/cmd/go/testflag.go index 0509fcdfdf..1e0724a993 100644 --- a/src/cmd/go/testflag.go +++ b/src/cmd/go/testflag.go @@ -256,13 +256,13 @@ func testFlag(args []string, i int) (f *testFlagSpec, value string, extra bool) extra = equals < 0 if extra { if i+1 >= len(args) { - usage() + testSyntaxError("missing argument for flag " + f.name) } value = args[i+1] } } if f.present && !f.multiOK { - usage() + testSyntaxError(f.name + " flag may be set only once") } f.present = true return @@ -276,8 +276,7 @@ func testFlag(args []string, i int) (f *testFlagSpec, value string, extra bool) func setBoolFlag(flag *bool, value string) { x, err := strconv.ParseBool(value) if err != nil { - fmt.Fprintf(os.Stderr, "go test: illegal bool flag value %s\n", value) - usage() + testSyntaxError("illegal bool flag value " + value) } *flag = x } @@ -286,8 +285,13 @@ func setBoolFlag(flag *bool, value string) { func setIntFlag(flag *int, value string) { x, err := strconv.Atoi(value) if err != nil { - fmt.Fprintf(os.Stderr, "go test: illegal int flag value %s\n", value) - usage() + testSyntaxError("illegal int flag value " + value) } *flag = x } + +func testSyntaxError(msg string) { + fmt.Fprintf(os.Stderr, "go test: %s\n", msg) + fmt.Fprintf(os.Stderr, `run "go help test" or "go help testflag" for more information`+"\n") + os.Exit(2) +} -- 2.48.1