Index   Main   Namespaces   Classes   Hierarchy   Annotated   Files   Compound   Global   Pages  

FuegoTestMain.cpp

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


Sun Mar 13 2011 Doxygen 1.7.1