cmd/cue/cmd: fix bug with help flag
Fixes https://github.com/cuelang/cue/issues/340
- [x] add more tests for help system
- [x] find and fix bug
Closes #341
https://github.com/cuelang/cue/pull/341
GitOrigin-RevId: 811ee28c1c902211e78370e63286af07b167ce34
Change-Id: I92949d544d8623f3cb90994e14685f56fd1ad708
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/5647
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
diff --git a/cmd/cue/cmd/root.go b/cmd/cue/cmd/root.go
index 587845c..0673ada 100644
--- a/cmd/cue/cmd/root.go
+++ b/cmd/cue/cmd/root.go
@@ -234,7 +234,8 @@
// "fix": {"fix", nil},
}
- if args[0] == "help" {
+ // handle help and --help on root 'cue' command
+ if args[0] == "help" || args[0] == "--help" {
// Allow errors.
_ = addSubcommands(cmd, sub, args[1:], true)
return cmd, nil
@@ -250,6 +251,7 @@
if err != nil {
return nil, err
}
+
tags, err := cmd.cmd.Flags().GetStringArray(string(flagInject))
if err != nil {
return nil, err
@@ -294,6 +296,13 @@
oldargs := []string{args[0]}
args = args[1:]
+ // Check for 'cue cmd --help'
+ // it is behaving differently for this one command, cobra does not seem to pick it up
+ // See issue #340
+ if len(args) == 1 && args[0] == "--help" {
+ return cmd.Usage()
+ }
+
if !isHelp {
err := cmd.cmd.ParseFlags(args)
if err != nil {
diff --git a/cmd/cue/cmd/root_test.go b/cmd/cue/cmd/root_test.go
index 1a5a14f..31ef2ec 100644
--- a/cmd/cue/cmd/root_test.go
+++ b/cmd/cue/cmd/root_test.go
@@ -17,7 +17,32 @@
import "testing"
func TestHelp(t *testing.T) {
- cmd, err := New([]string{"help", "cmd"})
+ cmd, err := New([]string{"help"})
+ if err != nil || cmd == nil {
+ t.Error("help command failed unexpectedly")
+ }
+
+ cmd, err = New([]string{"--help"})
+ if err != nil || cmd == nil {
+ t.Error("help command failed unexpectedly")
+ }
+
+ cmd, err = New([]string{"help", "cmd"})
+ if err != nil || cmd == nil {
+ t.Error("help command failed unexpectedly")
+ }
+
+ cmd, err = New([]string{"cmd", "--help"})
+ if err != nil || cmd == nil {
+ t.Error("help command failed unexpectedly")
+ }
+
+ cmd, err = New([]string{"help", "eval"})
+ if err != nil || cmd == nil {
+ t.Error("help command failed unexpectedly")
+ }
+
+ cmd, err = New([]string{"eval", "--help"})
if err != nil || cmd == nil {
t.Error("help command failed unexpectedly")
}