Display helpful error message in case subcommand name was mistyped. This matches Cobra's behavior for root command, which Cobra confusingly doesn't apply to nested commands.
(w io.Writer, command *cobra.Command, arg string)
| 62 | // This matches Cobra's behavior for root command, which Cobra |
| 63 | // confusingly doesn't apply to nested commands. |
| 64 | func nestedSuggestFunc(w io.Writer, command *cobra.Command, arg string) { |
| 65 | fmt.Fprintf(w, "unknown command %q for %q\n", arg, command.CommandPath()) |
| 66 | |
| 67 | var candidates []string |
| 68 | if arg == "help" { |
| 69 | candidates = []string{"--help"} |
| 70 | } else { |
| 71 | if command.SuggestionsMinimumDistance <= 0 { |
| 72 | command.SuggestionsMinimumDistance = 2 |
| 73 | } |
| 74 | candidates = command.SuggestionsFor(arg) |
| 75 | } |
| 76 | |
| 77 | if len(candidates) > 0 { |
| 78 | fmt.Fprint(w, "\nDid you mean this?\n") |
| 79 | for _, c := range candidates { |
| 80 | fmt.Fprintf(w, "\t%s\n", c) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | fmt.Fprint(w, "\n") |
| 85 | _ = rootUsageFunc(w, command) |
| 86 | } |
| 87 | |
| 88 | func isRootCmd(command *cobra.Command) bool { |
| 89 | return command != nil && !command.HasParent() |
no test coverage detected