second round of refactoring; moving configuration handling out of main function

actorid
Marc Zinnschlag 15 years ago
parent 04037fb01a
commit b7db6dcda3

@ -4,71 +4,69 @@
#include <fstream> #include <fstream>
#include <boost/program_options.hpp> #include <boost/program_options.hpp>
#include <boost/filesystem.hpp>
#include "engine.hpp" #include "engine.hpp"
using namespace std; using namespace std;
void maintest (boost::filesystem::path dataDir, const std::string& cellName, /// Parse command line options and openmw.cfg file (if one exists). Results are directly
std::string master) /// written to \a engine.
{ /// \return Run OpenMW?
assert (!dataDir.empty());
OMW::Engine engine;
engine.setDataDir (dataDir);
engine.setCell (cellName);
engine.addMaster (master);
engine.go(); bool parseOptions (int argc, char**argv, OMW::Engine& engine)
}
int main(int argc, char**argv)
{ {
try
{
boost::program_options::options_description desc ( boost::program_options::options_description desc (
"Syntax: openmw <options>\nAllowed options"); "Syntax: openmw <options>\nAllowed options");
desc.add_options() desc.add_options()
("help", "print help message") ("help", "print help message")
("data", boost::program_options::value<std::string>()->default_value ("data"), ("data", boost::program_options::value<std::string>()->default_value ("data"),
"set data directory") "set data directory")
("start", boost::program_options::value<std::string>()->default_value ("Beshara"), ("start", boost::program_options::value<std::string>()->default_value ("Beshara"),
"set initial cell (only interior cells supported at the moment") "set initial cell (only interior cells supported at the moment")
("master", boost::program_options::value<std::string>()->default_value ("Morrowind"), ("master", boost::program_options::value<std::string>()->default_value ("Morrowind"),
"master file") "master file")
; ;
boost::program_options::variables_map variables; boost::program_options::variables_map variables;
std::ifstream configFile ("openmw.cfg"); std::ifstream configFile ("openmw.cfg");
boost::program_options::store ( boost::program_options::store (
boost::program_options::parse_command_line (argc, argv, desc), variables); boost::program_options::parse_command_line (argc, argv, desc), variables);
boost::program_options::notify (variables); boost::program_options::notify (variables);
if (configFile.is_open()) if (configFile.is_open())
boost::program_options::store ( boost::program_options::store (
boost::program_options::parse_config_file (configFile, desc), variables); boost::program_options::parse_config_file (configFile, desc), variables);
if (variables.count ("help")) if (variables.count ("help"))
{ {
std::cout << desc << std::endl; std::cout << desc << std::endl;
return false;
} }
else
{ engine.setDataDir (variables["data"].as<std::string>());
maintest (variables["data"].as<std::string>(), variables["start"].as<std::string>(), engine.setCell (variables["start"].as<std::string>());
variables["master"].as<std::string>()); engine.addMaster (variables["master"].as<std::string>());
} }
}
catch(exception &e) int main(int argc, char**argv)
{
try
{ {
cout << "\nERROR: " << e.what() << endl; OMW::Engine engine;
return 1;
if (parseOptions (argc, argv, engine))
{
engine.go();
}
}
catch(exception &e)
{
cout << "\nERROR: " << e.what() << endl;
return 1;
} }
return 0;
return 0;
} }

Loading…
Cancel
Save