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  function to handle unknown commands correctly.
2. When an unknown  and  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>
  • Loading branch information
nnnkkk7 committed Jan 6, 2024
1 parent b55e471 commit 82ee1cf
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 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 Down Expand Up @@ -49,18 +52,19 @@ Simply type ` + c.Name() + ` help [path to command] for full details.`,
}

tools, err := buildTools(c, pkgArgs)
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)
if err != nil {
c.PrintErrf("Unknown help cmd command: %#q\n", strings.Join(args[1:], " "))
os.Exit(1)
}
addCustomCommands(c, cmd, commandSection, tools)
// For the sake of `cue help cmd mycmd`, find the command again.
cmd, _, 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 cmd == nil || e != nil || (len(args) > 0 && args[0] != "cmd") {
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 82ee1cf

Please sign in to comment.