81 lines
2.0 KiB
C++
81 lines
2.0 KiB
C++
// Copyright (c) 2023 Dominic Masters
|
|
//
|
|
// This software is released under the MIT License.
|
|
// https://opensource.org/licenses/MIT
|
|
|
|
#include "DawnTool.hpp"
|
|
#include "util/memory.hpp"
|
|
|
|
#if !defined(DAWN_TOOL_INSTANCE)
|
|
#error Dawn Tool Instance, e.g. LanguageTool has not been defined
|
|
#endif
|
|
|
|
#if !defined(DAWN_TOOL_HEADER)
|
|
#error Dawn Tool Include, e.g. LanguageTool.hpp has not been defined
|
|
#endif
|
|
|
|
#include DAWN_TOOL_HEADER
|
|
|
|
using namespace Dawn;
|
|
|
|
std::vector<std::string> DawnTool::getRequiredFlags() {
|
|
return std::vector<std::string>();
|
|
}
|
|
|
|
std::map<std::string, std::string> DawnTool::getOptionalFlags() {
|
|
return std::map<std::string, std::string>();
|
|
}
|
|
|
|
int32_t DawnTool::exec(const int32_t argc, const char *argv[]) {
|
|
// Set up flags
|
|
flags = this->getOptionalFlags();
|
|
|
|
// Parse args
|
|
for(int32_t i = 0; i < argc; i++) {
|
|
std::string a(argv[i]);
|
|
this->args.push_back(a);
|
|
|
|
// First arg is the path, we ignore it.
|
|
if(i == 0) continue;
|
|
|
|
// Is this a flag-like arg, as in has "--[name]=[value]"
|
|
if(a.size() < 5) continue;
|
|
if(a[0] != '-' || a[1] != '-') continue;
|
|
|
|
// Remove --
|
|
auto flag = a.erase(0, 2);
|
|
|
|
// Ensure = is present
|
|
auto equalPos = flag.find('=');
|
|
if(equalPos == std::string::npos) continue;
|
|
|
|
// Get prefix and val and store
|
|
auto prefix = flag.substr(0, equalPos);
|
|
auto val = flag.substr(equalPos + 1);
|
|
if(val.size() == 0) continue;
|
|
flags[prefix] = val;
|
|
}
|
|
|
|
// Now validate flags
|
|
auto required = this->getRequiredFlags();
|
|
auto itReq = required.begin();
|
|
while(itReq != required.end()) {
|
|
auto match = flags.find(*itReq);
|
|
if(match == flags.end()) {
|
|
std::cout << "Missing required flag \"" + *itReq + "\"!" << std::endl;
|
|
return 1;
|
|
}
|
|
++itReq;
|
|
}
|
|
|
|
return this->start();
|
|
}
|
|
|
|
int main(const int32_t argc, const char *argv[]) {
|
|
memoryInit();
|
|
DAWN_TOOL_INSTANCE *self = new DAWN_TOOL_INSTANCE();
|
|
int32_t ret = self->exec(argc, argv);
|
|
delete self;
|
|
if(ret == 0) memoryDispose();
|
|
return ret;
|
|
} |