Skip to content
Snippets Groups Projects
Commit ec1d8703 authored by samuel.hayden's avatar samuel.hayden
Browse files

Added command line arguement parser.

Currently the --input and --bounds arguements are accepted. Unknown arguements are displayed as errors. If the --input arguement is present, then the script will continue to attempt running with default values where required
parent 3b15bce1
No related branches found
No related tags found
No related merge requests found
#include "ParseCommandLine.h"
AcceptedKeys ParseCommandLine::s_validKeys = ACCEPTED_KEYS;
void ParseCommandLine::dumpErrors()
{
for (int i = 0; i < _errors.size(); i++)
{
ERROR_MSG_SUP("ParseCommandLine", _errors[i], _errorMsgs[i]);
}
}
#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();
};
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment