Made console command registry a better usage pattern
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
#include <sophia/console/console_command_registry.hpp>
|
||||
|
||||
namespace sophia {
|
||||
|
||||
void ConsoleCommandRegistry::registerCommand(std::string name, std::string help,
|
||||
Handler handler) {
|
||||
commands[std::move(name)] = Command{std::move(help), std::move(handler)};
|
||||
}
|
||||
|
||||
bool ConsoleCommandRegistry::dispatch(const std::string& name,
|
||||
const std::vector<std::string>& args,
|
||||
std::string& outResult) const {
|
||||
auto it = commands.find(name);
|
||||
if (it == commands.end()) {
|
||||
return false;
|
||||
}
|
||||
outResult = it->second.handler(args);
|
||||
return true;
|
||||
}
|
||||
|
||||
std::vector<ConsoleCommandRegistry::CommandInfo> ConsoleCommandRegistry::list()
|
||||
const {
|
||||
std::vector<CommandInfo> result;
|
||||
result.reserve(commands.size());
|
||||
for (const auto& [name, command] : commands) {
|
||||
result.push_back(CommandInfo{name, command.help});
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace sophia
|
||||
Reference in New Issue
Block a user