2023-10-10 21:01:09 +00:00
|
|
|
///
|
2023-10-15 01:38:01 +00:00
|
|
|
/// \file Commander.cc
|
2023-10-16 09:18:09 +00:00
|
|
|
/// \author K. Isom <kyle@imap.cc>
|
2023-10-11 00:24:29 +00:00
|
|
|
/// \date 2023-10-10
|
2023-10-16 09:18:09 +00:00
|
|
|
/// \brief Subprogram tooling.
|
|
|
|
///
|
|
|
|
/// Copyright 2023 K. Isom <kyle@imap.cc>
|
|
|
|
///
|
|
|
|
/// Permission to use, copy, modify, and/or distribute this software for
|
|
|
|
/// any purpose with or without fee is hereby granted, provided that
|
|
|
|
/// the above copyright notice and this permission notice appear in all /// copies.
|
|
|
|
///
|
|
|
|
/// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
|
|
|
|
/// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
|
|
|
|
/// WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
|
|
|
|
/// AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
|
|
|
|
/// DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
|
|
|
|
/// OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
|
|
|
/// TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
|
|
/// PERFORMANCE OF THIS SOFTWARE.
|
2023-10-10 21:01:09 +00:00
|
|
|
///
|
|
|
|
|
|
|
|
#include <iostream>
|
2023-10-16 09:18:09 +00:00
|
|
|
|
2023-10-10 21:01:09 +00:00
|
|
|
#include "Commander.h"
|
|
|
|
|
2023-10-16 09:18:09 +00:00
|
|
|
|
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-16 09:18:09 +00:00
|
|
|
} // namespace scsl
|