2023-10-10 21:01:09 +00:00
|
|
|
///
|
2023-10-15 01:38:01 +00:00
|
|
|
/// \file Commander.cc
|
2023-10-10 21:01:09 +00:00
|
|
|
/// \author kyle
|
2023-10-11 00:24:29 +00:00
|
|
|
/// \date 2023-10-10
|
2023-10-10 21:01:09 +00:00
|
|
|
///
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include "Commander.h"
|
|
|
|
|
2023-10-15 01:38:01 +00:00
|
|
|
namespace scsl {
|
2023-10-10 21:01:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
Subcommand::Status
|
|
|
|
Subcommand::Run(int argc, char **argv)
|
|
|
|
{
|
|
|
|
if (argc < this->args) {
|
|
|
|
std::cerr << "[!] " << this->command << " expects ";
|
|
|
|
std::cerr << this->args << " args, but was given ";
|
|
|
|
std::cerr << argc << " args.\n";
|
|
|
|
return Subcommand::Status::NotEnoughArgs;
|
|
|
|
}
|
|
|
|
if (this->fn(argc, argv)) {
|
|
|
|
return Subcommand::Status::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Subcommand::Status::Failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
Commander::Commander()
|
|
|
|
: cmap()
|
|
|
|
{
|
|
|
|
this->cmap.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Commander::Register(Subcommand scmd)
|
|
|
|
{
|
|
|
|
if (this->cmap.count(scmd.Name()) > 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto *pScmd = new Subcommand(scmd);
|
|
|
|
this->cmap[scmd.Name()] = pScmd;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Subcommand::Status
|
|
|
|
Commander::Run(std::string command, int argc, char **argv)
|
|
|
|
{
|
|
|
|
if (this->cmap.count(command) != 1) {
|
|
|
|
return Subcommand::Status::CommandNotRegistered;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto scmd = this->cmap[command];
|
|
|
|
return scmd->Run(argc, argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-15 01:38:01 +00:00
|
|
|
} // scsl
|