63 lines
1.4 KiB
C++
63 lines
1.4 KiB
C++
|
#include "cmdlineparser.h"
|
||
|
|
||
|
CmdLineParser::CmdLineParser()
|
||
|
{
|
||
|
|
||
|
}
|
||
|
|
||
|
void CmdLineParser::regOpt(OptDefine optdef)
|
||
|
{
|
||
|
optMap[optdef.shortName] = optdef;
|
||
|
}
|
||
|
|
||
|
string CmdLineParser::getOpt(string key)
|
||
|
{
|
||
|
if(!hasOptSet(key)){
|
||
|
return "";
|
||
|
}
|
||
|
return paraMap.at(key);
|
||
|
}
|
||
|
|
||
|
void CmdLineParser::parseArgs(int argc, char *argv[])
|
||
|
{
|
||
|
size_t s=optMap.size();
|
||
|
struct option *longOption = new option[s]();
|
||
|
int index=0;
|
||
|
string shortArgs;
|
||
|
for (auto i = optMap.begin(); i != optMap.end(); ++i){
|
||
|
struct option logopt={0};
|
||
|
OptDefine od = i->second;
|
||
|
logopt.name = od.name.c_str();
|
||
|
logopt.flag = NULL;
|
||
|
logopt.has_arg = od.needParam;
|
||
|
logopt.val = od.shortName;
|
||
|
if(logopt.val>=65 && logopt.val<=90){
|
||
|
// this is a short name
|
||
|
shortArgs.append(1,char(logopt.val));
|
||
|
if(logopt.has_arg != no_argument){
|
||
|
shortArgs.append(1,':');
|
||
|
}
|
||
|
}
|
||
|
index++;
|
||
|
}
|
||
|
int optIndex=0;
|
||
|
int opt;
|
||
|
while ((opt=getopt_long(argc, argv, shortArgs.c_str(), longOption, &optIndex))!=-1) {
|
||
|
OptDefine d = optMap.at(opt);
|
||
|
if(d.needParam == no_argument){
|
||
|
paraMap[d.name] = "true";
|
||
|
} else {
|
||
|
paraMap[d.name] = optarg;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
bool CmdLineParser::hasOptSet(string key)
|
||
|
{
|
||
|
|
||
|
if(paraMap.find(key) == paraMap.end()){
|
||
|
return false;
|
||
|
}
|
||
|
return true;
|
||
|
}
|