From 46e35a1ce408f8484e2025f7f6f52cb34f6e3c3f Mon Sep 17 00:00:00 2001 From: Vulpovile Date: Sun, 29 Mar 2026 21:45:52 -0700 Subject: [PATCH] Parse args --- Makefile | 6 ++++-- options.cpp | 22 ++++++++++++++++++++++ options.h | 1 + options_headeronly.h | 22 ++++++++++++++++++++++ 4 files changed, 49 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index f2eadf4..fdb7267 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ CXX ?= g++ -VERSION := 0.0.1 +VERSION := 0.0.2 TARGET_NOVERSION := liboptions.so TARGET := $(TARGET_NOVERSION).$(VERSION) @@ -37,9 +37,11 @@ clean: install: cp $(TARGET) /usr/lib/$(TARGET) 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) uninstall: unlink /usr/lib/$(TARGET_NOVERSION) rm /usr/lib/$(TARGET) - rm /usr/include/options.h \ No newline at end of file + rm /usr/include/options.h + rm /usr/include/options_headeronly.h \ No newline at end of file diff --git a/options.cpp b/options.cpp index 9d4e25f..148b48f 100644 --- a/options.cpp +++ b/options.cpp @@ -55,6 +55,28 @@ void OptionsBase::read(const std::string& filename) { kv[key] = value; } + for (auto* opt : getOptions()) { + if (kv.count(opt->key)) { + opt->setFromString(kv[opt->key]); + } + } +} + +void OptionsBase::parse(const int argc, const char ** argv) { + std::unordered_map 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]); diff --git a/options.h b/options.h index 0b347d4..4eb1827 100644 --- a/options.h +++ b/options.h @@ -31,6 +31,7 @@ namespace OptionLib { const std::vector<__OptionBase*>& getOptions() const; void store(const std::string& filename, std::string comment=""); void read(const std::string& filename); + void parse(const int argc, const char ** argv); }; template diff --git a/options_headeronly.h b/options_headeronly.h index 88ee271..cc56b7a 100644 --- a/options_headeronly.h +++ b/options_headeronly.h @@ -85,6 +85,28 @@ namespace OptionLib { } } } + + void parse(const int argc, const char ** argv) { + std::unordered_map 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]); + } + } + } };