blob: 7abf9445d3010a20b4d23edd5f2f69dbc1244d74 [file] [log] [blame]
// Copyright 2017 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "command_dispatcher.h"
#include <algorithm>
#include <cstdio>
#include "lib/fxl/logging.h"
namespace bluetooth {
namespace tools {
CommandDispatcher::CommandHandlerData::CommandHandlerData(
const std::string& description,
const CommandHandler& handler)
: description(description), handler(handler) {}
bool CommandDispatcher::ExecuteCommand(const std::vector<std::string>& argv,
const fxl::Closure& complete_cb,
bool* out_cmd_found) {
FXL_DCHECK(out_cmd_found);
*out_cmd_found = false;
if (argv.empty())
return false;
const auto& iter = handler_map_.find(argv[0]);
if (iter == handler_map_.end())
return false;
*out_cmd_found = true;
auto cl = fxl::CommandLineFromIterators(argv.begin(), argv.end());
return iter->second.handler(cl, complete_cb);
}
void CommandDispatcher::DescribeAllCommands() {
for (const auto& iter : handler_map_) {
std::printf(" %-20s %s\n", iter.first.c_str(),
iter.second.description.c_str());
}
}
void CommandDispatcher::RegisterHandler(const std::string& name,
const std::string& description,
const CommandHandler& handler) {
FXL_DCHECK(!name.empty());
FXL_DCHECK(!description.empty());
FXL_DCHECK(handler_map_.find(name) == handler_map_.end());
handler_map_[name] = CommandHandlerData(description, handler);
}
std::vector<std::string> CommandDispatcher::GetCommandsThatMatch(
const std::string& prefix) const {
std::vector<std::string> result;
for (auto& iter : handler_map_) {
auto& cmd_name = iter.first;
if (prefix.length() > cmd_name.length())
continue;
if (cmd_name.compare(0, prefix.length(), prefix) == 0) {
result.push_back(cmd_name);
}
}
return result;
}
} // namespace tools
} // namespace bluetooth