/** * @return whether commandline parsing was successful * * Parse command line arguments */ void SpringApp::ParseCmdLine(const std::string& binaryName) { cmdline->SetUsageDescription("Usage: " + binaryName + " [options] [path_to_script.txt or demo.sdf]"); cmdline->AddSwitch(0, "sync-version", "Display program sync version (for online gaming)"); cmdline->AddSwitch('f', "fullscreen", "Run in fullscreen mode"); cmdline->AddSwitch('w', "window", "Run in windowed mode"); cmdline->AddSwitch('b', "minimise", "Start in background (minimised)"); cmdline->AddSwitch(0, "nocolor", "Disables colorized stdout"); cmdline->AddSwitch('q', "quiet", "Ignore unrecognized arguments"); cmdline->AddString('s', "server", "Run as a server"); cmdline->AddSwitch('t', "textureatlas", "Dump each finalized textureatlas in textureatlasN.tga"); cmdline->AddInt( 0, "benchmark", "Enable benchmark mode (writes a benchmark.data file). The given number specifies the timespan to test."); cmdline->AddInt( 0, "benchmarkstart", "Benchmark start time in minutes."); cmdline->AddSwitch(0, "list-ai-interfaces", "Dump a list of available AI Interfaces to stdout"); cmdline->AddSwitch(0, "list-skirmish-ais", "Dump a list of available Skirmish AIs to stdout"); cmdline->AddSwitch(0, "list-config-vars", "Dump a list of config vars and meta data to stdout"); cmdline->AddSwitch(0, "list-def-tags", "Dump a list of all unitdef-, weapondef-, ... tags and meta data to stdout"); cmdline->AddSwitch(0, "list-ceg-classes", "Dump a list of available projectile classes to stdout"); cmdline->AddSwitch(0, "test-creg", "Test if all CREG classes are completed"); cmdline->AddSwitch(0, "safemode", "Turns off many things that are known to cause problems (i.e. on PC/Mac's with lower-end graphic cards)"); cmdline->AddString('C', "config", "Exclusive configuration file"); cmdline->AddSwitch('i', "isolation", "Limit the data-dir (games & maps) scanner to one directory"); cmdline->AddString(0, "isolation-dir", "Specify the isolation-mode data-dir (see --isolation)"); cmdline->AddString('d', "write-dir", "Specify where Spring writes to."); cmdline->AddString('g', "game", "Specify the game that will be instantly loaded"); cmdline->AddString('m', "map", "Specify the map that will be instantly loaded"); cmdline->AddString('n', "name", "Set your player name"); cmdline->AddSwitch(0, "oldmenu", "Start the old menu"); try { cmdline->Parse(); } catch (const CmdLineParams::unrecognized_option& err) { LOG_L(L_ERROR, "%s\n", err.what()); if (!cmdline->IsSet("quiet")) { cmdline->PrintUsage(); exit(EXIT_FAILURE); } } #ifndef WIN32 if (!cmdline->IsSet("nocolor") && (getenv("SPRING_NOCOLOR") == NULL)) { // don't colorize, if our output is piped to a diff tool or file if (isatty(fileno(stdout))) log_console_colorizedOutput(true); } #endif // mutually exclusive options that cause spring to quit immediately if (cmdline->IsSet("help")) { cmdline->PrintUsage(); exit(EXIT_SUCCESS); } if (cmdline->IsSet("version")) { std::cout << "Spring " << SpringVersion::GetFull() << std::endl; exit(EXIT_SUCCESS); } if (cmdline->IsSet("sync-version")) { // Note, the missing "Spring " is intentionally to make it compatible with `spring-dedicated --sync-version` std::cout << SpringVersion::GetSync() << std::endl; exit(EXIT_SUCCESS); } if (cmdline->IsSet("isolation")) { dataDirLocater.SetIsolationMode(true); } if (cmdline->IsSet("isolation-dir")) { dataDirLocater.SetIsolationMode(true); dataDirLocater.SetIsolationModeDir(cmdline->GetString("isolation-dir")); } if (cmdline->IsSet("write-dir")) { dataDirLocater.SetWriteDir(cmdline->GetString("write-dir")); } // Interface Documentations in JSON-Format if (cmdline->IsSet("list-config-vars")) { ConfigVariable::OutputMetaDataMap(); exit(EXIT_SUCCESS); } else if (cmdline->IsSet("list-def-tags")) { DefType::OutputTagMap(); exit(0); } else if (cmdline->IsSet("list-ceg-classes")) { const int res = CCustomExplosionGenerator::OutputProjectileClassInfo() ? EXIT_SUCCESS : EXIT_FAILURE; exit(res); } // Runtime Tests if (cmdline->IsSet("test-creg")) { const int res = creg::RuntimeTest() ? EXIT_SUCCESS : EXIT_FAILURE; exit(res); } const string configSource = (cmdline->IsSet("config") ? cmdline->GetString("config") : ""); const bool safemode = cmdline->IsSet("safemode"); // mutually exclusive options that cause spring to quit immediately if (cmdline->IsSet("list-ai-interfaces")) { ConsolePrintInitialize(configSource, safemode); IAILibraryManager::OutputAIInterfacesInfo(); exit(0); } else if (cmdline->IsSet("list-skirmish-ais")) { ConsolePrintInitialize(configSource, safemode); IAILibraryManager::OutputSkirmishAIInfo(); exit(0); } LOG("[%s] command-line args: \"%s\"", __FUNCTION__, cmdline->GetCmdLine().c_str()); FileSystemInitializer::PreInitializeConfigHandler(configSource, safemode); if (cmdline->IsSet("textureatlas")) { CTextureAtlas::SetDebug(true); } if (cmdline->IsSet("name")) { const string name = cmdline->GetString("name"); if (!name.empty()) { configHandler->SetString("name", StringReplace(name, " ", "_")); } } if (cmdline->IsSet("benchmark")) { CBenchmark::enabled = true; if (cmdline->IsSet("benchmarkstart")) { CBenchmark::startFrame = cmdline->GetInt("benchmarkstart") * 60 * GAME_SPEED; } CBenchmark::endFrame = CBenchmark::startFrame + cmdline->GetInt("benchmark") * 60 * GAME_SPEED; } }
void ParseCmdLine(int argc, char* argv[], std::string* script_txt) { #undef LOG_SECTION_CURRENT #define LOG_SECTION_CURRENT LOG_SECTION_DEFAULT std::string binaryname = argv[0]; CmdLineParams cmdline(argc, argv); cmdline.SetUsageDescription("Usage: " + binaryname + " [options] path_to_script.txt"); cmdline.AddSwitch(0, "sync-version", "Display program sync version (for online gaming)"); cmdline.AddString('C', "config", "Exclusive configuration file"); cmdline.AddSwitch(0, "list-config-vars", "Dump a list of config vars and meta data to stdout"); cmdline.AddSwitch('i', "isolation", "Limit the data-dir (games & maps) scanner to one directory"); cmdline.AddString(0, "isolation-dir", "Specify the isolation-mode data-dir (see --isolation)"); cmdline.AddSwitch(0, "nocolor", "Disables colorized stdout"); cmdline.AddSwitch('q', "quiet", "Ignore unrecognized arguments"); try { cmdline.Parse(); } catch (const CmdLineParams::unrecognized_option& err) { LOG_L(L_ERROR, "%s\n", err.what()); if (!cmdline.IsSet("quiet")) { cmdline.PrintUsage(); exit(EXIT_FAILURE); } } #ifndef WIN32 if (!cmdline.IsSet("nocolor") && (getenv("SPRING_NOCOLOR") == NULL)) { // don't colorize, if our output is piped to a diff tool or file if (isatty(fileno(stdout))) log_console_colorizedOutput(true); } #endif if (cmdline.IsSet("help")) { cmdline.PrintUsage(); exit(0); } if (cmdline.IsSet("version")) { LOG("%s", (SpringVersion::GetFull()).c_str()); exit(0); } if (cmdline.IsSet("sync-version")) { LOG("%s", (SpringVersion::GetSync()).c_str()); exit(0); } *script_txt = cmdline.GetInputFile(); if (script_txt->empty() && !cmdline.IsSet("list-config-vars")) { cmdline.PrintUsage(); exit(1); } if (cmdline.IsSet("isolation")) { dataDirLocater.SetIsolationMode(true); } if (cmdline.IsSet("isolation-dir")) { dataDirLocater.SetIsolationMode(true); dataDirLocater.SetIsolationModeDir(cmdline.GetString("isolation-dir")); } const std::string configSource = cmdline.IsSet("config") ? cmdline.GetString("config") : ""; if (cmdline.IsSet("list-config-vars")) { LOG_DISABLE(); FileSystemInitializer::PreInitializeConfigHandler(configSource); FileSystemInitializer::InitializeLogOutput(); LOG_ENABLE(); ConfigVariable::OutputMetaDataMap(); exit(0); } LOG("Run: %s", cmdline.GetCmdLine().c_str()); FileSystemInitializer::PreInitializeConfigHandler(configSource); #undef LOG_SECTION_CURRENT #define LOG_SECTION_CURRENT LOG_SECTION_DEDICATED_SERVER }