cmd/cue/cmd: better checks for incorrect flags

Fixes #969

Change-Id: I1628c5158c996830576dc65e4b6fc667e0009b46
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/9821
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Paul Jolly <paul@myitcv.org.uk>
diff --git a/cmd/cue/cmd/common.go b/cmd/cue/cmd/common.go
index 1c4381c..31c2f64 100644
--- a/cmd/cue/cmd/common.go
+++ b/cmd/cue/cmd/common.go
@@ -450,7 +450,7 @@
 		files = append(files, b.UnknownFiles...)
 	}
 	for _, f := range files {
-		if !p.matchFile(f.Filename) && f.Filename != "-" {
+		if !b.User && !p.matchFile(f.Filename) {
 			continue
 		}
 		if p.cfg.overrideDefault {
@@ -565,6 +565,11 @@
 		}
 	}
 
+	if len(p.insts) == 0 && flagGlob.String(p.cmd) != "" {
+		return nil, errors.Newf(token.NoPos,
+			"use of -n/--name flag without a directory")
+	}
+
 	if b := p.orphanInstance; b != nil {
 		schemas, values, err := p.getDecoders(b)
 		for _, d := range append(schemas, values...) {
@@ -630,6 +635,9 @@
 				}
 				p.encConfig.Schema = v
 			}
+		} else if p.schema != nil {
+			return nil, errors.Newf(token.NoPos,
+				"-d/--schema flag specified without a schema")
 		}
 
 		switch {
diff --git a/cmd/cue/cmd/flags.go b/cmd/cue/cmd/flags.go
index e616eac..579cbd2 100644
--- a/cmd/cue/cmd/flags.go
+++ b/cmd/cue/cmd/flags.go
@@ -84,7 +84,7 @@
 	f.Bool(string(flagWithContext), false, "import as object with contextual data")
 	f.StringArrayP(string(flagProtoPath), "I", nil, "paths in which to search for imports")
 	f.String(string(flagProtoEnum), "int", "mode for rendering enums (int|json)")
-	f.StringP(string(flagGlob), "n", "", "glob filter for file names")
+	f.StringP(string(flagGlob), "n", "", "glob filter for non-CUE file names in directories")
 	f.Bool(string(flagMerge), true, "merge non-CUE files")
 }
 
diff --git a/cmd/cue/cmd/testdata/script/eval_flags.txt b/cmd/cue/cmd/testdata/script/eval_flags.txt
new file mode 100644
index 0000000..1bb7042
--- /dev/null
+++ b/cmd/cue/cmd/testdata/script/eval_flags.txt
@@ -0,0 +1,50 @@
+# Issue #969
+
+! cue eval test.json -n ".+\.cue"
+cmp stderr expect-stderr1
+
+! cue eval test.json -d '#D2'
+cmp stderr expect-stderr2
+
+! cue eval test.json -n ".+\.cue" -d '#D2'
+cmp stderr expect-stderr3
+
+! cue eval test.json vector.cue -d '#D1'
+cmp stderr expect-stderr4
+
+! cue eval test.json vector.cue -d '#D2'
+cmp stderr expect-stderr5
+
+-- test.json --
+{
+  "X": 1,
+  "Y": 2,
+  "Z": 3
+}
+-- vector.cue --
+package Vector
+
+#D2: {
+    X: float
+    Y: float
+}
+
+#D3: {
+    X: float
+    Y: float
+    Z: float
+}
+
+-- expect-stderr1 --
+use of -n/--name flag without a directory
+-- expect-stderr2 --
+-d/--schema flag specified without a schema
+-- expect-stderr3 --
+use of -n/--name flag without a directory
+-- expect-stderr4 --
+reference "#D1" not found:
+    --schema:1:1
+-- expect-stderr5 --
+X: conflicting values 1 and float (mismatched types int and float):
+    ./test.json:2:8
+    ./vector.cue:4:8