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.
(IOStreams *iostreams.IOStreams, command *cobra.Command, arg string)
| 35 | // This matches Cobra's behavior for root command, which Cobra |
| 36 | // confusingly doesn't apply to nested commands. |
| 37 | func nestedSuggestFunc(IOStreams *iostreams.IOStreams, command *cobra.Command, arg string) { |
| 38 | w := IOStreams.ErrOut |
| 39 | fmt.Fprintf(w, "unknown command %q for %q\n", arg, command.CommandPath()) |
| 40 | |
| 41 | var candidates []string |
| 42 | if arg == "help" { |
| 43 | candidates = []string{"--help"} |
| 44 | } else { |
| 45 | if command.SuggestionsMinimumDistance <= 0 { |
| 46 | command.SuggestionsMinimumDistance = 2 |
| 47 | } |
| 48 | candidates = command.SuggestionsFor(arg) |
| 49 | } |
| 50 | |
| 51 | if len(candidates) > 0 { |
| 52 | fmt.Fprint(w, "\nDid you mean this?\n") |
| 53 | for _, c := range candidates { |
| 54 | fmt.Fprintf(w, "\t%s\n", c) |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | fmt.Fprint(w, "\n") |
| 59 | _ = rootUsageFunc(IOStreams, command)(command) |
| 60 | } |
| 61 | |
| 62 | func isRootCmd(command *cobra.Command) bool { |
| 63 | return command != nil && !command.HasParent() |
no test coverage detected