mirror of
https://github.com/modelec/cpp-lib.git
synced 2026-01-19 08:37:22 +01:00
initial commit
This commit is contained in:
68
components/CLParser/src/CLParser.cpp
Normal file
68
components/CLParser/src/CLParser.cpp
Normal file
@@ -0,0 +1,68 @@
|
||||
#include <Modelec/CLParser.h>
|
||||
|
||||
CLParser::CLParser(int argc, char **argv) : _argc(argc) {
|
||||
this->_argv = new std::string[argc];
|
||||
for (int i = 0; i < argc; i++) {
|
||||
this->_argv[i] = argv[i];
|
||||
}
|
||||
|
||||
for (int i = 1; i < argc; i++) {
|
||||
if (this->_argv[i].rfind("--", 0) == 0) {
|
||||
std::string option = this->_argv[i].substr(2);
|
||||
if (i + 1 < argc && this->_argv[i + 1].rfind("--", 0) != 0) {
|
||||
this->_options[option] = this->_argv[i + 1];
|
||||
} else {
|
||||
this->_options[option] = "";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::optional<std::string> CLParser::getOption(const std::string &option) const {
|
||||
if (!this->hasOption(option)) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
return this->_options.at(option);
|
||||
}
|
||||
|
||||
bool CLParser::hasOption(const std::string &option) const {
|
||||
return this->_options.find(option) != this->_options.end();
|
||||
}
|
||||
|
||||
std::string CLParser::getOption(const std::string &option, const std::string &defaultValue) const {
|
||||
if (!this->hasOption(option)) {
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
return this->_options.at(option);
|
||||
}
|
||||
|
||||
bool CLParser::hasPositionalArgument(int index) const {
|
||||
return index < this->_argc;
|
||||
}
|
||||
|
||||
std::string CLParser::getPositionalArgument(int index) const {
|
||||
if (!this->hasPositionalArgument(index)) {
|
||||
return "";
|
||||
}
|
||||
|
||||
return this->_argv[index];
|
||||
}
|
||||
|
||||
int CLParser::positionalArgumentsCount() const {
|
||||
return this->_argc;
|
||||
}
|
||||
|
||||
CLParser::~CLParser() {
|
||||
delete[] this->_argv;
|
||||
}
|
||||
|
||||
CLParser::CLParser(const CLParser &other) : _argc(other._argc) {
|
||||
this->_argv = new std::string[other._argc];
|
||||
for (int i = 0; i < other._argc; i++) {
|
||||
this->_argv[i] = other._argv[i];
|
||||
}
|
||||
|
||||
this->_options = other._options;
|
||||
}
|
||||
Reference in New Issue
Block a user