Skip to content

Commit 6ab9f11

Browse files
committed
cmd/cue: avoid confusing "unknown flag" errors in cue mod
If the user runs cue mod nosuchcommand --nosuchflag we'd fail with "unknown flag --nosuchflag" which is technically correct, but "no such subcommand" is a much more useful error message. For example, if a user on CUE v0.12.0 tries to run a newer command which doesn't exist yet, such as `cue mod mirror --to=...`, the error would not clearly point to `mod mirror` not existing yet. Fixes #3810. Signed-off-by: Daniel Martí <[email protected]> Change-Id: Iabd3780335f3d30488bb667d9115ec2af843d0d9 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1211267 Unity-Result: CUE porcuepine <[email protected]> TryBot-Result: CUEcueckoo <[email protected]> Reviewed-by: Roger Peppe <[email protected]>
1 parent 078125e commit 6ab9f11

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

cmd/cue/cmd/root.go

+7
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,13 @@ type Stats struct {
9090
// do anything useful, but we need RunE for good error messages, as Cobra itself is lacking:
9191
// https://github.com/spf13/cobra/issues/706
9292
func commandGroup(c *Command, cmd *cobra.Command) *cobra.Command {
93+
// If the user ran `cue mod nosuchcommand --nosuchflag`, we run the `cue mod` command
94+
// because `nosuchcommand` was not a known subcommand.
95+
// In such a case, do not fail with "unknown flag"; what matters to the user is that
96+
// `nosuchcommand`, which might have such a flag if it existed, was not found at all.
97+
// The logic below does not need to parse flags in any case.
98+
cmd.FParseErrWhitelist.UnknownFlags = true
99+
93100
name := cmd.Name()
94101
cmd.RunE = mkRunE(c, func(cmd *Command, args []string) error {
95102
stderr := cmd.Stderr()

cmd/cue/cmd/testdata/script/unknown_args.txtar

+10-4
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,32 @@ cmp stderr unknown_mod_cmd.out
1616

1717
! exec cue mod --unknown
1818
! stdout .
19-
cmp stderr unknown_flag.out
19+
cmp stderr missing_mod_cmd.out
2020

21-
# TODO: this should result in an "unknown subcommand" error.
2221
! exec cue mod unknown --unknown
2322
! stdout .
24-
cmp stderr unknown_flag.out
23+
cmp stderr unknown_mod_cmd.out
24+
25+
! exec cue -v mod -E unknown --unknown
26+
! stdout .
27+
cmp stderr unknown_mod_cmd.out
2528

2629
# A rather convoluted edge case, but Cobra allows mixing flags with args,
2730
# even when the args are subcommand names. -v and -E are global flags.
2831
# TODO: this should result in an "unknown subcommand" error.
2932
! exec cue -v mod -E unknown --unknown
3033
! stdout .
31-
cmp stderr unknown_flag.out
34+
cmp stderr unknown_mod_cmd.out
3235

3336
! exec cue evla
3437
! stdout .
3538
cmp stderr typo_cmd.stdout
3639

3740
-- unknown_cmd.out --
3841
unknown command "unknown" for "cue"
42+
-- missing_mod_cmd.out --
43+
mod must be run as one of its subcommands
44+
Run 'cue help mod' for known subcommands.
3945
-- unknown_mod_cmd.out --
4046
mod must be run as one of its subcommands: unknown subcommand "unknown"
4147
Run 'cue help mod' for known subcommands.

0 commit comments

Comments
 (0)