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