|
17 | 17 | #include "host/commands/cvd/cli/request_context.h" |
18 | 18 |
|
19 | 19 | #include <memory> |
| 20 | +#include <string> |
20 | 21 | #include <vector> |
21 | 22 |
|
22 | 23 | #include <android-base/logging.h> |
| 24 | +#include <android-base/strings.h> |
23 | 25 |
|
24 | 26 | #include "common/libs/utils/result.h" |
25 | 27 | #include "host/commands/cvd/cli/command_request.h" |
|
57 | 59 |
|
58 | 60 | namespace cuttlefish { |
59 | 61 |
|
| 62 | +namespace { |
| 63 | + |
| 64 | +std::vector<std::string> GetPossibleCommands( |
| 65 | + const CommandRequest& request, |
| 66 | + const std::vector<std::unique_ptr<CvdCommandHandler>>& handlers) { |
| 67 | + std::vector<std::string> possibilities; |
| 68 | + if (request.Subcommand().empty()) { |
| 69 | + return possibilities; |
| 70 | + } |
| 71 | + |
| 72 | + for (auto& handler : handlers) { |
| 73 | + for (const std::string& command : handler->CmdList()) { |
| 74 | + if (!command.empty() && |
| 75 | + android::base::StartsWith(command, request.Subcommand().front())) { |
| 76 | + possibilities.push_back(command); |
| 77 | + break; |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + return possibilities; |
| 82 | +} |
| 83 | + |
| 84 | +} // namespace |
| 85 | + |
60 | 86 | RequestContext::RequestContext(InstanceManager& instance_manager) |
61 | 87 | : instance_manager_(instance_manager), |
62 | 88 | command_sequence_executor_(this->request_handlers_) { |
@@ -113,14 +139,25 @@ Result<CvdCommandHandler*> RequestHandler( |
113 | 139 | compatible_handlers.push_back(handler.get()); |
114 | 140 | } |
115 | 141 | } |
| 142 | + |
116 | 143 | CF_EXPECT(compatible_handlers.size() < 2, |
117 | 144 | "The command matched multiple handlers which should not happen. " |
118 | 145 | "Please open a bug with the cvd/Cuttlefish team and include the " |
119 | 146 | "exact command that raised the error so it can be fixed."); |
120 | | - CF_EXPECTF(compatible_handlers.size() == 1, |
121 | | - "Unable to find a matching command for \"cvd {}\". Maybe there " |
122 | | - "is a typo?\nRun `cvd help` for a list of commands.", |
123 | | - request.Subcommand()); |
| 147 | + |
| 148 | + if (compatible_handlers.size() != 1) { |
| 149 | + const std::vector<std::string> possible_commands = |
| 150 | + GetPossibleCommands(request, handlers); |
| 151 | + std::string addendum; |
| 152 | + if (!possible_commands.empty()) { |
| 153 | + addendum = fmt::format("\n\nDid you mean one of:\n\t{}", |
| 154 | + fmt::join(possible_commands, "\n\t")); |
| 155 | + } |
| 156 | + return CF_ERRF( |
| 157 | + "Unable to find a matching command for \"cvd {}\".\nMaybe there " |
| 158 | + "is a typo? Run `cvd help` for a list of commands.{}", |
| 159 | + request.Subcommand(), addendum); |
| 160 | + } |
124 | 161 | return compatible_handlers[0]; |
125 | 162 | } |
126 | 163 |
|
|
0 commit comments