Skip to content

Commit

Permalink
cmd/cue: forbid cue help help in favor of cue help
Browse files Browse the repository at this point in the history
Requesting the help text for the help command itself is a bit silly.
`cue help` already describes how to query help for commands or topics.
Fail even though "help" is a valid command, just a special one.

This means that the help command no longer needs a long help text.
While here, slightly expand its short help text to clarify
that it also covers additional help topics beyond commands.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: I9f8541133981a7c7c3838f1783e9c89dbbe0bbcb
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1195931
Reviewed-by: Jonathan Matthews <github@hello.jonathanmatthews.com>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Reviewed-by: Paul Jolly <paul@myitcv.io>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
  • Loading branch information
mvdan committed Jun 13, 2024
1 parent b095ecb commit 859f1f6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
8 changes: 5 additions & 3 deletions cmd/cue/cmd/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,18 @@ import (
func newHelpCmd(c *Command) *cobra.Command {
cmd := &cobra.Command{
Use: "help [command]",
Short: "Help about any command",
Long: `Help provides help for any command in the application.
Simply type ` + c.Name() + ` help [path to command] for full details.`,
Short: "show help text for a command or topic",
Run: func(_ *cobra.Command, args []string) {
findCmd := func() (*cobra.Command, bool) {
cmd, rest, err := c.Root().Find(args)
found := cmd != nil && err == nil && len(rest) == 0
return cmd, found
}
cmd, found := findCmd()
// Treat `cue help help` as an unknown help topic; just use `cue help`.
if found && cmd.Name() == "help" {
cmd, found = nil, false
}
isCmd := len(args) > 0 && args[0] == "cmd"
if isCmd {
// args is one of:
Expand Down
10 changes: 10 additions & 0 deletions cmd/cue/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,14 @@ func New(args []string) (*Command, error) {
cmd := &cobra.Command{
Use: "cue",
Short: "cue emits configuration files to user-defined commands.",
// TODO(mvdan): the text below should not jump straight into `cue cmd`,
// and instead give a high level overview of CUE and its commands.
// TODO(mvdan): cobra's help text template ends with
// Use "cue [command] --help" for more information about a command.
// which isn't great; use our own template to say
// Use "cue help [command]" for more information about a command.
// TODO(mvdan): cobra's help text template suggests help topics
// such as `cue inputs`; swap with `cue help inputs`.
Long: `cue evaluates CUE files, an extension of JSON, and sends them
to user-defined commands for processing.
Expand Down Expand Up @@ -256,6 +264,8 @@ For more information on writing CUE configuration files see cuelang.org.`,
cmd.Flag("help").Hidden = true

// "help" is treated as a special command by cobra.
// TODO(mvdan): hide this command; `cue help` showing itself in the list of commands
// is not particularly helpful, and we already mention it as the way to get help.
cmd.SetHelpCommand(newHelpCmd(c))

// For `cue mycmd` to be a shortcut for `cue cmd mycmd`.
Expand Down
8 changes: 7 additions & 1 deletion cmd/cue/cmd/testdata/script/help.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ stderr -count=1 '^Available Commands:$'
stderr -count=1 'Unknown help topic: mod missing'
stderr -count=1 '^Available Commands:$'

# Requesting the help text for the help command itself is a bit silly.
# `cue help` already describes how to query help for commands or topics.
# Fail even though technically "help" is a valid command, just a special one.
! exec cue help help
stderr -count=1 'Unknown help topic: help'

-- stdout.golden --
cue evaluates CUE files, an extension of JSON, and sends them
to user-defined commands for processing.
Expand Down Expand Up @@ -81,7 +87,7 @@ Available Commands:
fix rewrite packages to latest standards
fmt formats CUE configuration files
get add dependencies to the current module
help Help about any command
help show help text for a command or topic
import convert other formats to CUE files
login log into a CUE registry
mod module maintenance
Expand Down

0 comments on commit 859f1f6

Please sign in to comment.