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

RFE: Support OnlySubCommands arg validation to make subcommand get suggestions #636

Closed
nak3 opened this issue Feb 17, 2018 · 1 comment
Closed

Comments

@nak3
Copy link
Contributor

nak3 commented Feb 17, 2018

Motivation

Currently, suggestions for "unknown command" are not working for
subcommands. For example code[1](wants to accept only when
subcommand specified), I expected that cobra give us suggestions like:

Expected

  $ go run main.go echo time
  Error: unknown command "time" for "echo"

  Did you mean this?
   	times

However, cobra does not suggest anything unfortunately.
SuggestFor: attribute also does not work currently.

Workaround

If I use OnlyValidArgs for the subcommands, this could be
solve. However, it does not suit when a command has many subcommands
and makes it makes difficult to maintain.

Proposal patch

#635

[1] example code

package main

import (
        "fmt"
        "strings"

        "github.com/spf13/cobra"
)

func main() {
        var echoTimes int
        var cmdEcho = &cobra.Command{
                Use:   "echo",
                Short: "My root command",
                Run: func(cmd *cobra.Command, args []string) {
                        fmt.Println("Don't want to call this...")
                },
        }

        var cmdTimes = &cobra.Command{
                Use: "times [# times] [string to echo]",
                Run: func(cmd *cobra.Command, args []string) {
                        for i := 0; i < echoTimes; i++ {
                                fmt.Println("Echo: " + strings.Join(args, " "))
                        }
                },
        }
        cmdTimes.Flags().IntVarP(&echoTimes, "times", "t", 1, "times to echo the input")
        var rootCmd = &cobra.Command{Use: "echo"}
        rootCmd.AddCommand(cmdEcho)
        cmdEcho.AddCommand(cmdTimes)
        rootCmd.Execute()
}
@github-actions
Copy link

This issue is being marked as stale due to a long period of inactivity

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