in reInvent18_Developer_Workshop/filter2D/src/host/cmdlineparser.cpp [170:261]
int CmdLineParser::parse(int argc, char* argv[]) {
int i = 0;
int ctOptions = 0;
while(i < argc) {
string key, val;
bool iskey = false;
string token = string(argv[i]);
bool isNextTokenKey = false;
if(i + 1 < argc) {
string peeknext = string(argv[i+1]);
if(starts_with(peeknext, "-") || starts_with(peeknext, "--")) {
string fullkey;
isNextTokenKey = token_to_fullkeyname(peeknext, fullkey);
}
}
//full-key
if(starts_with(token, string("--"))) {
if(m_mapKeySwitch.find(token) == m_mapKeySwitch.end()) {
LogError("Unrecognized key passed %s", token.c_str());
printHelp();
return -1;
}
key = token;
iskey = true;
}
//shortcut
else if(starts_with(token, "-")) {
if(m_mapShortcutKeys.find(token) == m_mapShortcutKeys.end()) {
LogError("Unrecognized shortcut key passed %s", token.c_str());
printHelp();
return -1;
}
key = m_mapShortcutKeys[token];
iskey = true;
}
//default key, the value for default key is the last argument
else if(isNextTokenKey == false && m_strDefaultKey.length() > 0 && i == argc - 2) {
if(m_mapKeySwitch.find(m_strDefaultKey) == m_mapKeySwitch.end()) {
LogError("Unrecognized default key %s", m_strDefaultKey.c_str());
printHelp();
return -1;
}
LogInfo("Using default key: %s", m_strDefaultKey.c_str());
key = m_strDefaultKey;
iskey = true;
}
//if iskey and needs param then read it
if(iskey) {
ctOptions++;
if(key == "--help") {
printHelp();
return 1;
}
//fetch value
CmdSwitch* pcmd = m_mapKeySwitch[key];
//read next
if(pcmd->istoggle) {
pcmd->value = string("true");
pcmd->isvalid = true;
}
else {
i++;
pcmd->value = string(argv[i]);
pcmd->isvalid = true;
}
}
//next token
i++;
}
//capture real app name
if(argc > 0) {
m_appname = string(argv[0]);
}
return ctOptions;
}