Parse args
This commit is contained in:
4
Makefile
4
Makefile
@@ -1,6 +1,6 @@
|
|||||||
CXX ?= g++
|
CXX ?= g++
|
||||||
|
|
||||||
VERSION := 0.0.1
|
VERSION := 0.0.2
|
||||||
|
|
||||||
TARGET_NOVERSION := liboptions.so
|
TARGET_NOVERSION := liboptions.so
|
||||||
TARGET := $(TARGET_NOVERSION).$(VERSION)
|
TARGET := $(TARGET_NOVERSION).$(VERSION)
|
||||||
@@ -37,9 +37,11 @@ clean:
|
|||||||
install:
|
install:
|
||||||
cp $(TARGET) /usr/lib/$(TARGET)
|
cp $(TARGET) /usr/lib/$(TARGET)
|
||||||
cp options.h /usr/include/options.h
|
cp options.h /usr/include/options.h
|
||||||
|
cp options_headeronly.h /usr/include/options_headeronly.h
|
||||||
ln -s /usr/lib/$(TARGET) /usr/lib/$(TARGET_NOVERSION)
|
ln -s /usr/lib/$(TARGET) /usr/lib/$(TARGET_NOVERSION)
|
||||||
|
|
||||||
uninstall:
|
uninstall:
|
||||||
unlink /usr/lib/$(TARGET_NOVERSION)
|
unlink /usr/lib/$(TARGET_NOVERSION)
|
||||||
rm /usr/lib/$(TARGET)
|
rm /usr/lib/$(TARGET)
|
||||||
rm /usr/include/options.h
|
rm /usr/include/options.h
|
||||||
|
rm /usr/include/options_headeronly.h
|
||||||
22
options.cpp
22
options.cpp
@@ -61,3 +61,25 @@ void OptionsBase::read(const std::string& filename) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void OptionsBase::parse(const int argc, const char ** argv) {
|
||||||
|
std::unordered_map<std::string, std::string> kv;
|
||||||
|
|
||||||
|
std::string key, value, line;
|
||||||
|
|
||||||
|
for(int i = 0; i < argc; i++) {
|
||||||
|
line = std::string(argv[i]);
|
||||||
|
if (line.size() < 1 || line[0] == '#') continue; // skip comment lines TODO make better
|
||||||
|
auto pos = line.find('=');
|
||||||
|
if (pos == std::string::npos) continue; // skip invalid lines
|
||||||
|
std::string key = line.substr(0, pos);
|
||||||
|
std::string value = line.substr(pos + 1);
|
||||||
|
kv[key] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto* opt : getOptions()) {
|
||||||
|
if (kv.count(opt->key)) {
|
||||||
|
opt->setFromString(kv[opt->key]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -31,6 +31,7 @@ namespace OptionLib {
|
|||||||
const std::vector<__OptionBase*>& getOptions() const;
|
const std::vector<__OptionBase*>& getOptions() const;
|
||||||
void store(const std::string& filename, std::string comment="");
|
void store(const std::string& filename, std::string comment="");
|
||||||
void read(const std::string& filename);
|
void read(const std::string& filename);
|
||||||
|
void parse(const int argc, const char ** argv);
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
|
|||||||
@@ -86,6 +86,28 @@ namespace OptionLib {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void parse(const int argc, const char ** argv) {
|
||||||
|
std::unordered_map<std::string, std::string> kv;
|
||||||
|
|
||||||
|
std::string key, value, line;
|
||||||
|
|
||||||
|
for(int i = 0; i < argc; i++) {
|
||||||
|
line = std::string(argv[i]);
|
||||||
|
if (line.size() < 1 || line[0] == '#') continue; // skip comment lines TODO make better
|
||||||
|
auto pos = line.find('=');
|
||||||
|
if (pos == std::string::npos) continue; // skip invalid lines
|
||||||
|
std::string key = line.substr(0, pos);
|
||||||
|
std::string value = line.substr(pos + 1);
|
||||||
|
kv[key] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto* opt : getOptions()) {
|
||||||
|
if (kv.count(opt->key)) {
|
||||||
|
opt->setFromString(kv[opt->key]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
|
|||||||
Reference in New Issue
Block a user