63 lines
1.4 KiB
C++
Executable File
63 lines
1.4 KiB
C++
Executable File
#include "CmdRegistry.h"
|
|
|
|
#include "ICmd.h"
|
|
|
|
#include <boost/algorithm/string/predicate.hpp>
|
|
#include <boost/filesystem.hpp>
|
|
#include <boost/lexical_cast.hpp>
|
|
#include <fstream>
|
|
#include <vector>
|
|
|
|
namespace bts {
|
|
namespace cmd {
|
|
|
|
std::map<std::string, std::unique_ptr<ICmd>> CmdRegistry::_cmds;
|
|
|
|
int CmdRegistry::dispatch (int argc, char **argv)
|
|
{
|
|
if (argc < 2)
|
|
return _cmds["help"]->execute (std::vector<std::string> ());
|
|
else {
|
|
std::vector<std::string> args;
|
|
for (int i = 1; i < argc; i++) {
|
|
std::string arg (argv[i]);
|
|
if (boost::starts_with(boost::to_lower_copy(arg),"-configserver")) {
|
|
// skip configserver flag as well as url
|
|
i++;
|
|
}
|
|
else
|
|
args.push_back (arg);
|
|
}
|
|
|
|
ICmd *cmd = retrieve (std::string (args[0]));
|
|
if (cmd == 0) {
|
|
return _cmds["help"]->execute(std::vector<std::string>());
|
|
}
|
|
else {
|
|
args.erase (args.begin ());
|
|
return cmd->execute (args);
|
|
}
|
|
}
|
|
}
|
|
|
|
ICmd * CmdRegistry::retrieve (std::string const &name)
|
|
{
|
|
std::string lname (name);
|
|
boost::to_lower (lname);
|
|
if (_cmds.find (lname) == _cmds.end ())
|
|
return 0;
|
|
else
|
|
return _cmds[lname].get ();
|
|
}
|
|
|
|
std::vector<std::string> CmdRegistry::cmds ()
|
|
{
|
|
std::vector<std::string> result;
|
|
for (auto&p : _cmds)
|
|
result.push_back (p.first);
|
|
return result;
|
|
}
|
|
|
|
}}
|
|
|