Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Command suggestions not working for nested commands #2058

Open
rchaganti opened this issue Oct 26, 2023 · 1 comment
Open

Command suggestions not working for nested commands #2058

rchaganti opened this issue Oct 26, 2023 · 1 comment

Comments

@rchaganti
Copy link

rchaganti commented Oct 26, 2023

Consider the following example.

package main

import (
	"fmt"
	"os"

	"github.com/spf13/cobra"
)

var rootCmd = &cobra.Command{
	Use:   "app",
	Short: "A sample CLI program",
	Long:  "A sample CLI program that demonstrates intelligent command suggestions",
	Run: func(cmd *cobra.Command, args []string) {
		fmt.Println("Hello, world!")
	},
}

var serverCmd = &cobra.Command{
	Use:   "server",
	Short: "Server top-level",
	Long:  "This is a top-level server command",
	Run:   func(cmd *cobra.Command, args []string) {},
}

var startCmd = &cobra.Command{
	Use:   "start",
	Short: "Start the server",
	Long:  "Start the server and listen for incoming requests",
	Run: func(cmd *cobra.Command, args []string) {
		fmt.Println("Starting the server...")
	},
}

func init() {
	rootCmd.AddCommand(serverCmd)
	serverCmd.AddCommand(startCmd)
}

func main() {
	if err := rootCmd.Execute(); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
}

Command suggestions for misspelled command names work fine at the top-level.

$ go run main.go serve
Error: unknown command "serve" for "app"

Did you mean this?
        server

Run 'app --help' for usage.
unknown command "serve" for "app"

Did you mean this?
        server

exit status 1

However, it fails at the nested command level.

$ go run main.go server stat

Is this not supported?

Also, when I have these commands separated into individual go files (root.go, server.go, and stat.go etc), nested command suggestions do not even work.

$ go run main.go serve
Error: unknown command "serve" for "app"

Did you mean this?
        server

Run 'app --help' for usage.
exit status 1

$ go run main.go server stat
This is the top-level server command

Usage:
  app server [command]

Available Commands:
  start       Stars a server

Flags:
  -h, --help   help for server

Use "app server [command] --help" for more information about a command.
@marckhouzam
Copy link
Collaborator

I’ve never looked into it but yes the correction is not triggered for sub commands. We’d have to search to see if this was intended or a bug

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants