#pragma once #include <unordered_map> #include <unordered_set> #include <iostream> #include <string> #include "ErrorCodes.h" typedef std::unordered_map<std::string, std::string> KeyValueMap; typedef std::unordered_set<std::string> AcceptedKeys; #define ACCEPTED_KEYS { "--input", "--bounds" } class ParseCommandLine { private: std::string _launchPath; KeyValueMap _cmdLineMap; std::vector<ErrorCodes> _errors; std::vector<std::string> _errorMsgs; private: static AcceptedKeys s_validKeys; inline bool validKey(const std::string& key) { return s_validKeys.count(key); } public: ParseCommandLine(int argc, char** argv) { _launchPath = argv[0]; for (int i = 1; i < argc; i += 2) { std::string key = argv[i]; if (validKey(key)) _cmdLineMap.emplace(key.substr(2), argv[i + 1]); else { _errors.emplace_back(ErrorCodes::InvalidArgument); _errorMsgs.emplace_back(key); } } } inline const std::string& getValue(const std::string& key) const { return _cmdLineMap.at(key); } inline const bool hasKey(const std::string& key) const { if (_cmdLineMap.find(key) == _cmdLineMap.end()) return false; return true; } const bool hasErrors() const { return _errors.size() > 0; } void dumpErrors(); };