Skip to content

Commit b4fd07d

Browse files
committed
Merge pull request OpenXcom#527 from cfailde/options
Command line parsing of options improved
2 parents 8fa1b45 + 3db41ce commit b4fd07d

1 file changed

Lines changed: 27 additions & 9 deletions

File tree

src/Engine/Options.cpp

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ std::vector<std::string> _dataList;
4343
std::string _userFolder = "";
4444
std::string _configFolder = "";
4545
std::vector<std::string> _userList;
46-
std::map<std::string, std::string> _options;
46+
std::map<std::string, std::string> _options, _commandLineOptions;
4747
std::vector<std::string> _rulesets;
4848
std::vector<std::string> _purchaseexclusions;
4949

@@ -267,12 +267,7 @@ void loadArgs(int argc, char** args)
267267
std::transform(argname.begin(), argname.end(), argname.begin(), ::tolower);
268268
if (argc > i + 1)
269269
{
270-
std::map<std::string, std::string>::iterator it = _options.find(argname);
271-
if (it != _options.end())
272-
{
273-
it->second = args[i+1];
274-
}
275-
else if (argname == "data")
270+
if (argname == "data")
276271
{
277272
_dataFolder = CrossPlatform::endPath(args[i+1]);
278273
}
@@ -282,8 +277,25 @@ void loadArgs(int argc, char** args)
282277
}
283278
else
284279
{
285-
Log(LOG_WARNING) << "Unknown option: " << argname;
286-
}
280+
// case insensitive lookup of the argument
281+
bool found = false;
282+
for(std::map<std::string, std::string>::iterator it = _options.begin(); it != _options.end(); ++it)
283+
{
284+
std::string option = it->first;
285+
std::transform(option.begin(), option.end(), option.begin(), ::tolower);
286+
if (option == argname)
287+
{
288+
//save this command line option for now, we will apply it later
289+
_commandLineOptions[it->first]= args[i+1];
290+
found = true;
291+
break;
292+
}
293+
}
294+
if(!found)
295+
{
296+
Log(LOG_WARNING) << "Unknown option: " << argname;
297+
}
298+
}
287299
}
288300
else
289301
{
@@ -379,6 +391,12 @@ bool init(int argc, char** args)
379391
Log(LOG_INFO) << "User folder is: " << _userFolder;
380392
Log(LOG_INFO) << "Config folder is: " << _configFolder;
381393
Log(LOG_INFO) << "Options loaded successfully.";
394+
395+
// now apply options set on the command line, overriding defaults and those loaded from config file
396+
for(std::map<std::string, std::string>::const_iterator it = _commandLineOptions.begin(); it != _commandLineOptions.end(); ++it)
397+
{
398+
_options[it->first] = it->second;
399+
}
382400
return true;
383401
}
384402

0 commit comments

Comments
 (0)