00001 //---------------------------------------------------------------------------- 00002 /** @file FuegoTestMain.cpp 00003 Main function for FuegoTest. 00004 */ 00005 //---------------------------------------------------------------------------- 00006 00007 #include "SgSystem.h" 00008 00009 #include <iostream> 00010 #include "FuegoTestEngine.h" 00011 #include "GoInit.h" 00012 #include "SgDebug.h" 00013 #include "SgException.h" 00014 #include "SgInit.h" 00015 00016 #include <boost/utility.hpp> 00017 #include <boost/program_options/options_description.hpp> 00018 #include <boost/program_options/cmdline.hpp> 00019 #include <boost/program_options/variables_map.hpp> 00020 #include <boost/program_options/parsers.hpp> 00021 00022 using namespace std; 00023 namespace po = boost::program_options; 00024 00025 //---------------------------------------------------------------------------- 00026 00027 namespace { 00028 00029 /** @name Settings from command line options */ 00030 // @{ 00031 00032 bool g_quiet; 00033 00034 string g_config; 00035 00036 /** Player string as in FuegoTestEngine::SetPlayer */ 00037 string g_player; 00038 00039 const char* g_programPath; 00040 00041 // @} // @name 00042 00043 void MainLoop() 00044 { 00045 GtpInputStream gtpIn(cin); 00046 GtpOutputStream gtpOut(cout); 00047 FuegoTestEngine engine(gtpIn, gtpOut, 0, g_programPath, g_player); 00048 GoGtpAssertionHandler assertionHandler(engine); 00049 if (g_config != "") 00050 engine.ExecuteFile(g_config); 00051 engine.MainLoop(); 00052 } 00053 00054 void Help(po::options_description& desc) 00055 { 00056 cout << "Options:\n" << desc << '\n'; 00057 exit(1); 00058 } 00059 00060 void ParseOptions(int argc, char** argv) 00061 { 00062 int srand; 00063 po::options_description options_desc; 00064 options_desc.add_options() 00065 ("config", 00066 po::value<std::string>(&g_config)->default_value(""), 00067 "execuate GTP commands from file before starting main command loop") 00068 ("help", "displays this help and exit") 00069 ("player", 00070 po::value<std::string>(&g_player)->default_value(""), 00071 "player (average|ladder|liberty|maxeye|minlib|no-search|random|safe") 00072 ("quiet", "don't print debug messages") 00073 ("srand", 00074 po::value<int>(&srand)->default_value(0), 00075 "set random seed (-1:none, 0:time(0))"); 00076 po::variables_map vm; 00077 try { 00078 po::store(po::parse_command_line(argc, argv, options_desc), vm); 00079 po::notify(vm); 00080 } 00081 catch(...) { 00082 Help(options_desc); 00083 } 00084 if (vm.count("help")) 00085 Help(options_desc); 00086 if (vm.count("quiet")) 00087 g_quiet = true; 00088 if (vm.count("srand")) 00089 SgRandom::SetSeed(srand); 00090 } 00091 00092 } // namespace 00093 00094 //---------------------------------------------------------------------------- 00095 00096 int main(int argc, char** argv) 00097 { 00098 if (argc > 0 && argv != 0) 00099 { 00100 g_programPath = argv[0]; 00101 try 00102 { 00103 ParseOptions(argc, argv); 00104 } 00105 catch (const SgException& e) 00106 { 00107 SgDebug() << e.what() << "\n"; 00108 return 1; 00109 } 00110 } 00111 if (g_quiet) 00112 SgDebugToNull(); 00113 try 00114 { 00115 SgInit(); 00116 GoInit(); 00117 MainLoop(); 00118 GoFini(); 00119 SgFini(); 00120 } 00121 catch (const GtpFailure& e) 00122 { 00123 SgDebug() << e.Response() << '\n'; 00124 return 1; 00125 } 00126 catch (const std::exception& e) 00127 { 00128 SgDebug() << e.what() << '\n'; 00129 return 1; 00130 } 00131 return 0; 00132 } 00133 00134 //---------------------------------------------------------------------------- 00135