-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandRouter.cpp
More file actions
63 lines (58 loc) · 1.55 KB
/
CommandRouter.cpp
File metadata and controls
63 lines (58 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "CommandRouter.h"
std::string CommandRouter::execute(std::string args)
{
std::string ret;
bool use = false;
size_t first_delimitor = args.find_first_of(' ');
std::string command_name = args;
std::string new_args;
if (first_delimitor != std::string::npos)
{
command_name = args.substr(0, first_delimitor);
new_args = args.substr(first_delimitor + 1);
}
for (size_t i = 0; i < this->commands.size(); i++)
{
if (this->commands.at(i)->getName() == command_name)
{
try
{
ret = this->commands.at(i)->execute(new_args);
}
catch (std::exception e)
{
throw CommandRuntimeException(this->commands.at(i)->getName(), e.what());
}
catch (...)
{
throw CommandRuntimeException(this->commands.at(i)->getName(), "unknown");
}
use = true;
break;
}
}
if (!use)
{
throw CommandNotFoundException(this->getName() + "." + args);
}
return ret;
}
std::string CommandRouter::getHelp()
{
std::string ret = "";
ret.append(Command::getHelp() + "\n");
ret.append(std::string("subcommand:") + "\n");
for (size_t i = 0; i < this->commands.size(); i++)
{
ret.append(this->commands.at(i)->getName() + "\n");
}
return ret;
}
void CommandRouter::add(Command* command)
{
this->commands.push_back(command);
}
std::vector<Command*>* CommandRouter::getRoutes()
{
return &this->commands;
}