1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-09-15 15:02:34 +02:00

Catch all command line parsing errors, properly exit on --help

This commit is contained in:
Sven Stucki 2016-09-02 01:43:30 +02:00
parent 0847c85abd
commit f5d76fbbcd
2 changed files with 22 additions and 9 deletions

View File

@ -50,6 +50,7 @@ RWGame::RWGame(int argc, char* argv[])
bool fullscreen = false;
bool newgame = false;
bool test = false;
bool help = false;
std::string startSave;
std::string benchFile;
@ -64,18 +65,25 @@ RWGame::RWGame(int argc, char* argv[])
("newgame,n", "Directly start a new game")
("test,t", "Starts a new game in a test location")
("load,l", po::value<std::string>(), "Load save file")
("benchmark,b", po::value<std::string>(), "Run benchmark and store results in file")
("benchmark,b", po::value<std::string>(), "Run benchmark from file")
;
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
if( vm.count("help") )
try
{
// TODO: This is a hack
std::cout << desc << std::endl;
throw std::runtime_error("Terminate");
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
}
catch (po::error& ex)
{
help = true;
std::cout << "Error parsing arguments: " << ex.what() << std::endl;
}
if( help || vm.count("help") )
{
std::cout << desc;
throw std::invalid_argument("");
}
if( vm.count("width") )
{
@ -178,7 +186,7 @@ RWGame::RWGame(int argc, char* argv[])
{
loading->setNextState(new MenuState(this));
}
StateManager::get().enter(loading);
log.info("Game", "Started");

View File

@ -8,6 +8,11 @@ int main(int argc, char* argv[])
RWGame game(argc, argv);
return game.run();
} catch (std::invalid_argument& ex) {
// This exception is thrown when either an invalid command line option
// or a --help is found. The RWGame constructor prints a usage message
// in this case and then throws this exception.
return -2;
} catch (std::runtime_error& ex) {
// Catch runtime_error as these are fatal issues the user may want to
// know about like corrupted files or GL initialisation failure.