From 95db828e170c48696fb67e869989a01e34516647 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Sat, 14 Jan 2012 14:02:54 +0000 Subject: [PATCH] CommandManager: use directly first command parameter as the command name --- src/command_manager.cc | 26 ++++++++++---------------- src/command_manager.hh | 3 +-- src/main.cc | 6 ++---- 3 files changed, 13 insertions(+), 22 deletions(-) diff --git a/src/command_manager.cc b/src/command_manager.cc index 5d5e4e69..542466d4 100644 --- a/src/command_manager.cc +++ b/src/command_manager.cc @@ -65,33 +65,27 @@ void CommandManager::execute(const std::string& command_line, if (tokens.empty()) return; - std::string command_name = - command_line.substr(tokens[0].first, - tokens[0].second - tokens[0].first); - - auto command_it = m_commands.find(command_name); - if (command_it == m_commands.end()) - throw command_not_found(command_name); - CommandParameters params; - for (auto it = tokens.begin() + 1; it != tokens.end(); ++it) + for (auto it = tokens.begin(); it != tokens.end(); ++it) { params.push_back(command_line.substr(it->first, it->second - it->first)); } - command_it->second.command(params, context); + execute(params, context); } -void CommandManager::execute(const std::string& command, - const CommandParameters& params, +void CommandManager::execute(const CommandParameters& params, const Context& context) { - auto command_it = m_commands.find(command); - if (command_it == m_commands.end()) - throw command_not_found(command); + if (params.empty()) + return; - command_it->second.command(params, context); + auto command_it = m_commands.find(params[0]); + if (command_it == m_commands.end()) + throw command_not_found(params[0]); + + command_it->second.command(CommandParameters(params.begin() + 1, params.end()), context); } Completions CommandManager::complete(const std::string& command_line, size_t cursor_pos) diff --git a/src/command_manager.hh b/src/command_manager.hh index 852c7d67..6b73b47d 100644 --- a/src/command_manager.hh +++ b/src/command_manager.hh @@ -54,8 +54,7 @@ class CommandManager : public Singleton { public: void execute(const std::string& command_line, const Context& context); - void execute(const std::string& command, const CommandParameters& params, - const Context& context); + void execute(const CommandParameters& params, const Context& context); Completions complete(const std::string& command_line, size_t cursor_pos); diff --git a/src/main.cc b/src/main.cc index 2e81cc81..069ef006 100644 --- a/src/main.cc +++ b/src/main.cc @@ -531,15 +531,13 @@ void add_hook(const CommandParameters& params, const Context& context) if (params.size() < 3) throw wrong_argument_count(); - std::string command = params[2]; - CommandParameters hook_params(params.begin()+3, params.end()); + CommandParameters hook_params(params.begin()+2, params.end()); HooksManager::instance().add_hook( params[0], [=](const std::string& param, const Context& context) { if (boost::regex_match(param, boost::regex(params[1]))) - CommandManager::instance().execute(command, hook_params, - context); + CommandManager::instance().execute(hook_params, context); }); }