Suggestions when “unknown command” happens

Cobra will print automatic suggestions when “unknown command” errors happen. This allows Cobra to behave similarly to the git command when a typo happens. For example:

  1. $ hugo srever
  2. Error: unknown command "srever" for "hugo"
  3. Did you mean this?
  4. server
  5. Run 'hugo --help' for usage.

Suggestions are automatic based on every subcommand registered and use an implementation of Levenshtein distance. Every registered command that matches a minimum distance of 2 (ignoring case) will be displayed as a suggestion.

If you need to disable suggestions or tweak the string distance in your command, use:

  1. command.DisableSuggestions = true

or

  1. command.SuggestionsMinimumDistance = 1

You can also explicitly set names for which a given command will be suggested using the SuggestFor attribute. This allows suggestions for strings that are not close in terms of string distance, but makes sense in your set of commands and for some which you don’t want aliases. Example:

  1. $ kubectl remove
  2. Error: unknown command "remove" for "kubectl"
  3. Did you mean this?
  4. delete
  5. Run 'kubectl help' for usage.