61 lines
1.4 KiB
C++
61 lines
1.4 KiB
C++
//
|
|
// Created by versustune on 22.02.20.
|
|
//
|
|
#include "Arguments.h"
|
|
#include <cstring>
|
|
#include <utility>
|
|
|
|
/**
|
|
* PUBLIC ONES
|
|
*/
|
|
Arguments::Arguments(int argc, std::vector<std::string> args) {
|
|
count = argc;
|
|
Arguments::args = std::move(args);
|
|
}
|
|
|
|
Arguments::~Arguments() {
|
|
|
|
}
|
|
|
|
void Arguments::parseArgs() {
|
|
for (int i = 0; i < count; i++) {
|
|
//always skip the first :)
|
|
if (i == 0)
|
|
continue;
|
|
std::string string = std::string(args[i]);
|
|
//if argument doesnt start with -- it's invalid!
|
|
if (string.find("--", 0) != 0)
|
|
continue;
|
|
string = string.replace(0, 2, "");
|
|
auto pos = string.find('=', 0);
|
|
if (pos != 0 && pos < string.size()) {
|
|
std::string token = string.substr(0, pos);
|
|
parsedArgs[token] = string.substr(pos + 1, string.back());
|
|
} else {
|
|
parsedArgs[string] = "true";
|
|
}
|
|
}
|
|
}
|
|
|
|
std::string Arguments::getArg(const std::string &key) {
|
|
if (parsedArgs.find(key) == parsedArgs.end()) {
|
|
return "";
|
|
}
|
|
return parsedArgs[key];
|
|
}
|
|
|
|
std::string Arguments::getArgOrDefault(const std::string &key, std::string fallback) {
|
|
if (parsedArgs.find(key) == parsedArgs.end()) {
|
|
return fallback;
|
|
}
|
|
return parsedArgs[key];
|
|
}
|
|
|
|
const std::unordered_map<std::string, std::string> &Arguments::getParsedArgs() {
|
|
return parsedArgs;
|
|
}
|
|
|
|
/**
|
|
* PROTECTED ONES
|
|
*/
|