Commit c32a8830 authored by Rob Pike's avatar Rob Pike

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
parent 992374f8
...@@ -256,13 +256,13 @@ func testFlag(args []string, i int) (f *testFlagSpec, value string, extra bool) ...@@ -256,13 +256,13 @@ func testFlag(args []string, i int) (f *testFlagSpec, value string, extra bool)
extra = equals < 0 extra = equals < 0
if extra { if extra {
if i+1 >= len(args) { if i+1 >= len(args) {
usage() testSyntaxError("missing argument for flag " + f.name)
} }
value = args[i+1] value = args[i+1]
} }
} }
if f.present && !f.multiOK { if f.present && !f.multiOK {
usage() testSyntaxError(f.name + " flag may be set only once")
} }
f.present = true f.present = true
return return
...@@ -276,8 +276,7 @@ func testFlag(args []string, i int) (f *testFlagSpec, value string, extra bool) ...@@ -276,8 +276,7 @@ func testFlag(args []string, i int) (f *testFlagSpec, value string, extra bool)
func setBoolFlag(flag *bool, value string) { func setBoolFlag(flag *bool, value string) {
x, err := strconv.ParseBool(value) x, err := strconv.ParseBool(value)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "go test: illegal bool flag value %s\n", value) testSyntaxError("illegal bool flag value " + value)
usage()
} }
*flag = x *flag = x
} }
...@@ -286,8 +285,13 @@ func setBoolFlag(flag *bool, value string) { ...@@ -286,8 +285,13 @@ func setBoolFlag(flag *bool, value string) {
func setIntFlag(flag *int, value string) { func setIntFlag(flag *int, value string) {
x, err := strconv.Atoi(value) x, err := strconv.Atoi(value)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "go test: illegal int flag value %s\n", value) testSyntaxError("illegal int flag value " + value)
usage()
} }
*flag = x *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)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment