Skip to content

Commit d5041a1

Browse files
committed
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 <[email protected]> Reviewed-by: Paul Jolly <[email protected]>
1 parent 073c6aa commit d5041a1

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

cmd/cue/cmd/common.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ func (p *buildPlan) getDecoders(b *build.Instance) (schemas, values []*decoderIn
450450
files = append(files, b.UnknownFiles...)
451451
}
452452
for _, f := range files {
453-
if !p.matchFile(f.Filename) && f.Filename != "-" {
453+
if !b.User && !p.matchFile(f.Filename) {
454454
continue
455455
}
456456
if p.cfg.overrideDefault {
@@ -565,6 +565,11 @@ func parseArgs(cmd *Command, args []string, cfg *config) (p *buildPlan, err erro
565565
}
566566
}
567567

568+
if len(p.insts) == 0 && flagGlob.String(p.cmd) != "" {
569+
return nil, errors.Newf(token.NoPos,
570+
"use of -n/--name flag without a directory")
571+
}
572+
568573
if b := p.orphanInstance; b != nil {
569574
schemas, values, err := p.getDecoders(b)
570575
for _, d := range append(schemas, values...) {
@@ -630,6 +635,9 @@ func parseArgs(cmd *Command, args []string, cfg *config) (p *buildPlan, err erro
630635
}
631636
p.encConfig.Schema = v
632637
}
638+
} else if p.schema != nil {
639+
return nil, errors.Newf(token.NoPos,
640+
"-d/--schema flag specified without a schema")
633641
}
634642

635643
switch {

cmd/cue/cmd/flags.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func addOrphanFlags(f *pflag.FlagSet) {
8484
f.Bool(string(flagWithContext), false, "import as object with contextual data")
8585
f.StringArrayP(string(flagProtoPath), "I", nil, "paths in which to search for imports")
8686
f.String(string(flagProtoEnum), "int", "mode for rendering enums (int|json)")
87-
f.StringP(string(flagGlob), "n", "", "glob filter for file names")
87+
f.StringP(string(flagGlob), "n", "", "glob filter for non-CUE file names in directories")
8888
f.Bool(string(flagMerge), true, "merge non-CUE files")
8989
}
9090

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Issue #969
2+
3+
! cue eval test.json -n ".+\.cue"
4+
cmp stderr expect-stderr1
5+
6+
! cue eval test.json -d '#D2'
7+
cmp stderr expect-stderr2
8+
9+
! cue eval test.json -n ".+\.cue" -d '#D2'
10+
cmp stderr expect-stderr3
11+
12+
! cue eval test.json vector.cue -d '#D1'
13+
cmp stderr expect-stderr4
14+
15+
! cue eval test.json vector.cue -d '#D2'
16+
cmp stderr expect-stderr5
17+
18+
-- test.json --
19+
{
20+
"X": 1,
21+
"Y": 2,
22+
"Z": 3
23+
}
24+
-- vector.cue --
25+
package Vector
26+
27+
#D2: {
28+
X: float
29+
Y: float
30+
}
31+
32+
#D3: {
33+
X: float
34+
Y: float
35+
Z: float
36+
}
37+
38+
-- expect-stderr1 --
39+
use of -n/--name flag without a directory
40+
-- expect-stderr2 --
41+
-d/--schema flag specified without a schema
42+
-- expect-stderr3 --
43+
use of -n/--name flag without a directory
44+
-- expect-stderr4 --
45+
reference "#D1" not found:
46+
--schema:1:1
47+
-- expect-stderr5 --
48+
X: conflicting values 1 and float (mismatched types int and float):
49+
./test.json:2:8
50+
./vector.cue:4:8

0 commit comments

Comments
 (0)