2017-08-01 12:30:58 +00:00
|
|
|
#ifndef COMMAND_LINE_ARGS_H
|
|
|
|
#define COMMAND_LINE_ARGS_H
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* Command-line parsing
|
|
|
|
******************************************************************************/
|
|
|
|
#include <map>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <string>
|
|
|
|
#include <cstring>
|
|
|
|
#include <sstream>
|
|
|
|
class b3CommandLineArgs
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
std::map<std::string, std::string> pairs;
|
|
|
|
|
|
|
|
public:
|
|
|
|
// Constructor
|
|
|
|
b3CommandLineArgs(int argc, char **argv)
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
addArgs(argc, argv);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
void addArgs(int argc, char **argv)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
for (int i = 1; i < argc; i++)
|
|
|
|
{
|
|
|
|
std::string arg = argv[i];
|
|
|
|
|
|
|
|
if ((arg.length() < 2) || (arg[0] != '-') || (arg[1] != '-'))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
std::string::size_type pos;
|
|
|
|
std::string key, val;
|
|
|
|
if ((pos = arg.find('=')) == std::string::npos)
|
|
|
|
{
|
|
|
|
key = std::string(arg, 2, arg.length() - 2);
|
|
|
|
val = "";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
key = std::string(arg, 2, pos - 2);
|
|
|
|
val = std::string(arg, pos + 1, arg.length() - 1);
|
|
|
|
}
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
//only add new keys, don't replace existing
|
2019-01-03 13:26:51 +00:00
|
|
|
if (pairs.find(key) == pairs.end())
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
pairs[key] = val;
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
}
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
bool CheckCmdLineFlag(const char *arg_name)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
std::map<std::string, std::string>::iterator itr;
|
2019-01-03 13:26:51 +00:00
|
|
|
if ((itr = pairs.find(arg_name)) != pairs.end())
|
|
|
|
{
|
2017-08-01 12:30:58 +00:00
|
|
|
return true;
|
2019-01-03 13:26:51 +00:00
|
|
|
}
|
2017-08-01 12:30:58 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool GetCmdLineArgument(const char *arg_name, T &val);
|
|
|
|
|
|
|
|
int ParsedArgc()
|
|
|
|
{
|
|
|
|
return pairs.size();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
inline bool b3CommandLineArgs::GetCmdLineArgument(const char *arg_name, T &val)
|
|
|
|
{
|
|
|
|
std::map<std::string, std::string>::iterator itr;
|
2019-01-03 13:26:51 +00:00
|
|
|
if ((itr = pairs.find(arg_name)) != pairs.end())
|
|
|
|
{
|
2017-08-01 12:30:58 +00:00
|
|
|
std::istringstream strstream(itr->second);
|
|
|
|
strstream >> val;
|
|
|
|
return true;
|
2019-01-03 13:26:51 +00:00
|
|
|
}
|
2017-08-01 12:30:58 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
2019-01-03 13:26:51 +00:00
|
|
|
inline bool b3CommandLineArgs::GetCmdLineArgument<char *>(const char *arg_name, char *&val)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
std::map<std::string, std::string>::iterator itr;
|
2019-01-03 13:26:51 +00:00
|
|
|
if ((itr = pairs.find(arg_name)) != pairs.end())
|
|
|
|
{
|
2017-08-01 12:30:58 +00:00
|
|
|
std::string s = itr->second;
|
2019-01-03 13:26:51 +00:00
|
|
|
val = (char *)malloc(sizeof(char) * (s.length() + 1));
|
2017-08-01 12:30:58 +00:00
|
|
|
std::strcpy(val, s.c_str());
|
|
|
|
return true;
|
2019-01-03 13:26:51 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
val = NULL;
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
#endif //COMMAND_LINE_ARGS_H
|