58 lines
1.6 KiB
C++
58 lines
1.6 KiB
C++
#include <string>
|
|
#include <memory>
|
|
#include <map>
|
|
#include <sharedipc.h>
|
|
|
|
class ArgumentParserPrivate {
|
|
public:
|
|
using Ptr = std::shared_ptr<ArgumentParserPrivate>;
|
|
|
|
void parse(int argc, char* argv[]) {
|
|
for (int i = 1; i < argc; i += 2) {
|
|
if (i + 1 >= argc) {
|
|
// throw std::runtime_error("Missing value for argument: " + std::string(argv[i]));
|
|
}
|
|
args_[argv[i]] = argv[i + 1];
|
|
}
|
|
|
|
// Проверяем обязательные аргументы
|
|
if (!has("-masterMemory") || !has("-slaveMemory")) {
|
|
// throw std::runtime_error("Both -masterMemory and -slaveMemory must be specified");
|
|
}
|
|
}
|
|
|
|
std::string masterMemory() const { return get("-masterMemory"); }
|
|
std::string slaveMemory() const { return get("-slaveMemory"); }
|
|
|
|
std::string get(const std::string& key, const std::string& defaultValue = "") const {
|
|
auto it = args_.find(key);
|
|
return it != args_.end() ? it->second : defaultValue;
|
|
}
|
|
|
|
bool has(const std::string& key) const {
|
|
return args_.find(key) != args_.end();
|
|
}
|
|
|
|
private:
|
|
std::map<std::string, std::string> args_;
|
|
};
|
|
|
|
ArgumentParser::ArgumentParser()
|
|
{
|
|
_model = new ArgumentParserPrivate();
|
|
}
|
|
|
|
ArgumentParser::~ArgumentParser()
|
|
{
|
|
if (_model) {
|
|
_model = new ArgumentParserPrivate();
|
|
}
|
|
}
|
|
|
|
void ArgumentParser::parse(int argc, char* argv[]) {
|
|
_model->parse(argc,argv);
|
|
}
|
|
|
|
std::string ArgumentParser::masterMemory() const { return _model->get("-masterMemory"); }
|
|
std::string ArgumentParser::slaveMemory() const { return _model->get("-slaveMemory"); }
|