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

Consistency in handling unknown commands/Expose findSuggestions() #870

Closed
colin-axner opened this issue May 31, 2019 · 8 comments
Closed

Comments

@colin-axner
Copy link

The default handling of subcommands is to accept arbitrary arguments. If a subcommand is misspelled it prints out the help screen, but if a flag is added then it prints out the error was an unknown flag instead of an unknown command. For example, root subcommand unknown --flag returns ERROR: unknown flag: --flag. Expected behavior should be ERROR: unknown command "unknown".

One workaround I found is to DisableFlagParsing which turns flags into arguments. However, then the help screen is returned rather than the unknown command error. I found adding validation in the run works as a quick fix:

func validateArgs(cmd *cobra.Command, args []string) {
    if len(args) > 0 {
        fmt.Printf("ERROR: unknown command \"%s\" for \"%s\" \n\nDid you mean this?\n\t%v\n", args[0], cmd.CalledAs(), cmd.SuggestionsFor(args[0])[0])

    } else {
        fmt.Println(cmd.Help())
    }  
    return
}

Is there any other way of achieving this desired result? This seems like a function that cobra should support.

Furthermore why is cmd.findSuggestions() private where as cmd.SuggestionsFor public? I think it would be useful to expose findSuggestions so users could simply call a function that returns the full suggestion string instead of manually implementing it like I did above. Not sure if that should be a separate issue, but I think it would be a useful and easy fix.

@mkrump
Copy link

mkrump commented Jul 12, 2019

Have the same question. Was curious as to the best way to handle this.

@chrisgilmerproj
Copy link

Can you give an example of how you used the workaround? I'd love to take a shot at using it myself.

@colin-axner
Copy link
Author

Here is my workaround. Here is how I setup the command fields. Hope this helps!

@umarcor
Copy link
Contributor

umarcor commented Jul 13, 2019

This is partially being addressed in #842.

@chrisgilmerproj
Copy link

Thanks a ton for the example!

@github-actions
Copy link

github-actions bot commented Apr 5, 2020

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

@johnSchnake
Copy link
Collaborator

Seems to have been resolved:

➜ ./tmp sub2
Error: unknown command "sub2" for "main"

Did you mean this?
	sub

Run 'main --help' for usage.
➜ ./tmp sub2 --foo
Error: unknown command "sub2" for "main"

Did you mean this?
	sub

Run 'main --help' for usage.

@colin-axner
Copy link
Author

Hi! Has it been included in a release? From quick testing, I'm able to reproduce the issue on v1.3.0. The example you reference uses subcommands in relation to the root command main. That works fine, but I'd expect something like:

./tmp sub sub2 --foo
Error: unknown flag: --foo

to occur. Can you confirm this?

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

5 participants