/// /// \file Commander.cpp /// \author kyle /// \created 10/10/23 /// \brief /// \section COPYRIGHT /// \section COPYRIGHT /// Copyright 2023 K. Isom /// /// 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. /// /// @\section DESCRIPTION #include #include "Commander.h" namespace klib { 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); } } // klib