CommandManager: use directly first command parameter as the command name

This commit is contained in:
Maxime Coste 2012-01-14 14:02:54 +00:00
parent 66d91b8828
commit 95db828e17
3 changed files with 13 additions and 22 deletions

View File

@ -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)

View File

@ -54,8 +54,7 @@ class CommandManager : public Singleton<CommandManager>
{
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);

View File

@ -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);
});
}