cmd/cue/cmd: clarify location for commands in error message

Issue #126

Change-Id: I6c9d7c92136e99cb0fc90f6308e8a987d494b025
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/3920
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
diff --git a/cmd/cue/cmd/cmd.go b/cmd/cue/cmd/cmd.go
index 6be5382..29fef5f 100644
--- a/cmd/cue/cmd/cmd.go
+++ b/cmd/cue/cmd/cmd.go
@@ -221,7 +221,10 @@
 			if len(args) == 0 {
 				fmt.Fprintln(w, "cmd must be run as one of its subcommands")
 			} else {
-				fmt.Fprintf(w, "cmd must be run as one of its subcommands: unknown subcommand %q\n", args[0])
+				const msg = `cmd must be run as one of its subcommands: unknown subcommand %q
+Ensure commands are defined in a "_tool.cue" file.
+`
+				fmt.Fprintf(w, msg, args[0])
 			}
 			fmt.Fprintln(w, "Run 'cue help cmd' for known subcommands.")
 			os.Exit(1) // TODO: get rid of this
diff --git a/cmd/cue/cmd/mod.go b/cmd/cue/cmd/mod.go
index cc78991..43e397e 100644
--- a/cmd/cue/cmd/mod.go
+++ b/cmd/cue/cmd/mod.go
@@ -36,7 +36,7 @@
 			if len(args) == 0 {
 				fmt.Fprintln(stderr, "mod must be run as one of its subcommands")
 			} else {
-				fmt.Fprintf(stderr, "get must be run as one of its subcommands: unknown subcommand %q\n", args[0])
+				fmt.Fprintf(stderr, "mod must be run as one of its subcommands: unknown subcommand %q\n", args[0])
 			}
 			fmt.Fprintln(stderr, "Run 'cue help mod' for known subcommands.")
 			os.Exit(1) // TODO: get rid of this
diff --git a/cmd/cue/cmd/root.go b/cmd/cue/cmd/root.go
index 75e8e58..896f43d 100644
--- a/cmd/cue/cmd/root.go
+++ b/cmd/cue/cmd/root.go
@@ -253,11 +253,13 @@
 	}
 	_, err = addCustom(cmd, rootCmd, commandSection, args[0], tools)
 	if err != nil {
-		return cmd, errors.Newf(token.NoPos,
-			"command %s %q is not defined\n"+
-				"Run 'cue help' to show available commands.",
+		err = errors.Newf(token.NoPos,
+			`%s %q is not defined
+Ensure commands are defined in a "_tool.cue" file.
+Run 'cue help' to show available commands.`,
 			commandSection, args[0],
 		)
+		return cmd, err
 	}
 	return cmd, nil
 }
diff --git a/cmd/cue/cmd/testdata/script/cmd_notool.txt b/cmd/cue/cmd/testdata/script/cmd_notool.txt
new file mode 100644
index 0000000..0a67913
--- /dev/null
+++ b/cmd/cue/cmd/testdata/script/cmd_notool.txt
@@ -0,0 +1,18 @@
+! cue cmd notool
+! stdout .
+cmp stderr cmd_baddisplay.out
+
+-- cmd_baddisplay.out --
+cmd must be run as one of its subcommands: unknown subcommand "notool"
+Ensure commands are defined in a "_tool.cue" file.
+Run 'cue help cmd' for known subcommands.
+-- task.cue --
+package home
+message: "Hello world!"
+
+command: notool: {
+	task: display: {
+		kind: "print"
+		text: 42
+	}
+}
diff --git a/cmd/cue/cmd/testdata/script/cmd_notool2.txt b/cmd/cue/cmd/testdata/script/cmd_notool2.txt
new file mode 100644
index 0000000..52c941f
--- /dev/null
+++ b/cmd/cue/cmd/testdata/script/cmd_notool2.txt
@@ -0,0 +1,18 @@
+! cue notool
+! stdout .
+cmp stderr cmd_baddisplay.out
+
+-- cmd_baddisplay.out --
+command "notool" is not defined
+Ensure commands are defined in a "_tool.cue" file.
+Run 'cue help' to show available commands.
+-- task.cue --
+package home
+message: "Hello world!"
+
+command: notool: {
+	task: display: {
+		kind: "print"
+		text: 42
+	}
+}