Skip to content

Commit

Permalink
cmd/cue: fix help cmd incorrect behavior
Browse files Browse the repository at this point in the history
This fixes the issue of incorrect behavior when requesting help for an unknown command in the CUE CLI.
1. Modified the `newHelpCmd` function to handle unknown commands correctly.
2. When an unknown `cue help`  commands are entered, an error message is displayed and the program exits with a non-zero status code.
3. This ensures that the CLI behaves as expected and provides useful feedback to the user when an unknown command is entered.
Fixes #2560

Signed-off-by: nnnkkk7 <kurodanaoki0711pana@gmail.com>

Signed-off-by: nnnkkk7 <kurodanaoki0711pana@gmail.com>
  • Loading branch information
nnnkkk7 committed Jan 8, 2024
1 parent b55e471 commit 697b950
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions cmd/cue/cmd/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
package cmd

import (
"os"
"strings"

"github.com/spf13/cobra"
)

Expand All @@ -34,7 +37,7 @@ func newHelpCmd(c *Command) *cobra.Command {
Long: `Help provides help for any command in the application.
Simply type ` + c.Name() + ` help [path to command] for full details.`,
Run: func(_ *cobra.Command, args []string) {
cmd, _, e := c.Root().Find(args)
cmd, rests, e := c.Root().Find(args)
if len(args) > 0 && args[0] == "cmd" {
// args is one of:
//
Expand All @@ -52,15 +55,15 @@ Simply type ` + c.Name() + ` help [path to command] for full details.`,
if err == nil {
addCustomCommands(c, cmd, commandSection, tools)
// For the sake of `cue help cmd mycmd`, find the command again.
cmd, _, e = c.Root().Find(args)
cmd, rests, e = c.Root().Find(args)
}
}
if cmd == nil || e != nil {
c.Printf("Unknown help topic %#q\n", args)
cobra.CheckErr(c.Root().Usage())
} else {
cobra.CheckErr(cmd.Help())
// If rests are not empty, args are invalid.
if cmd == nil || e != nil || len(rests) != 0 {
c.PrintErrf("Unknown help command: %#q\n", strings.Join(args, " "))
os.Exit(1)
}
cobra.CheckErr(cmd.Help())
},
}
return cmd
Expand Down

0 comments on commit 697b950

Please sign in to comment.