diff options
author | Andrei Karas <akaras@inbox.ru> | 2015-08-02 15:51:45 +0300 |
---|---|---|
committer | Andrei Karas <akaras@inbox.ru> | 2015-08-02 16:54:28 +0300 |
commit | 6a8bbbcf85f8f15000894cb106af7b87db26f485 (patch) | |
tree | 42608b40a15fc7d9a5d69d514b03eb4b754f7e7a | |
parent | d5ebca96984c0b1b63f0ec249fd22dc84acfa8a5 (diff) | |
download | manaverse-6a8bbbcf85f8f15000894cb106af7b87db26f485.tar.gz manaverse-6a8bbbcf85f8f15000894cb106af7b87db26f485.tar.bz2 manaverse-6a8bbbcf85f8f15000894cb106af7b87db26f485.tar.xz manaverse-6a8bbbcf85f8f15000894cb106af7b87db26f485.zip |
Move command line options parsing to separate file.
-rw-r--r-- | src/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/Makefile.am | 2 | ||||
-rw-r--r-- | src/commandline.cpp | 250 | ||||
-rw-r--r-- | src/commandline.h | 30 | ||||
-rw-r--r-- | src/main.cpp | 217 |
5 files changed, 285 insertions, 216 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d50299f2b..1a2a82f4b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -849,6 +849,8 @@ SET(SRCS chatlogger.h client.cpp client.h + commandline.cpp + commandline.h configmanager.cpp configmanager.h being/compounditem.h diff --git a/src/Makefile.am b/src/Makefile.am index 3e4495dc4..dd1a9a8d2 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -988,6 +988,8 @@ manaplus_SOURCES += gui/widgets/avatarlistbox.cpp \ chatlogger.h \ client.cpp \ client.h \ + commandline.cpp \ + commandline.h \ configmanager.cpp \ configmanager.h \ being/compounditem.h \ diff --git a/src/commandline.cpp b/src/commandline.cpp new file mode 100644 index 000000000..389d38434 --- /dev/null +++ b/src/commandline.cpp @@ -0,0 +1,250 @@ +/* + * The ManaPlus Client + * Copyright (C) 2004-2009 The Mana World Development Team + * Copyright (C) 2009-2010 The Mana Developers + * Copyright (C) 2011-2015 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "commandline.h" + +#include "main.h" +#include "settings.h" + +#include "utils/gettext.h" +#include "utils/paths.h" +#include "utils/stringutils.h" + +#include <getopt.h> +#include <iostream> +#include <unistd.h> + +#include "debug.h" + +static void printHelp() +{ + std::cout + // TRANSLATORS: command line help + << _("manaplus [options] [manaplus-file]") << std::endl << std::endl + // TRANSLATORS: command line help + << _("[manaplus-file] : The manaplus file is an XML file (.manaplus)") + << std::endl + // TRANSLATORS: command line help + << _(" used to set custom parameters") << std::endl + // TRANSLATORS: command line help + << _(" to the manaplus client.") + << std::endl << std::endl + // TRANSLATORS: command line help + << _("Options:") << std::endl + // TRANSLATORS: command line help + << _(" -l --log-file : Log file to use") << std::endl + // TRANSLATORS: command line help + << _(" -a --chat-log-dir : Chat log dir to use") << std::endl + // TRANSLATORS: command line help + << _(" -v --version : Display the version") << std::endl + // TRANSLATORS: command line help + << _(" -h --help : Display this help") << std::endl + // TRANSLATORS: command line help + << _(" -C --config-dir : Configuration directory to use") + << std::endl + // TRANSLATORS: command line help + << _(" -U --username : Login with this username") << std::endl + // TRANSLATORS: command line help + << _(" -P --password : Login with this password") << std::endl + // TRANSLATORS: command line help + << _(" -c --character : Login with this character") << std::endl + // TRANSLATORS: command line help + << _(" -s --server : Login server name or IP") << std::endl + // TRANSLATORS: command line help + << _(" -y --server-type : Login server type") << std::endl + // TRANSLATORS: command line help + << _(" -p --port : Login server port") << std::endl + // TRANSLATORS: command line help + << _(" -H --update-host : Use this update host") << std::endl + // TRANSLATORS: command line help + << _(" -D --default : Choose default character server and " + "character") << std::endl + // TRANSLATORS: command line help + << _(" -u --skip-update : Skip the update downloads") << std::endl + // TRANSLATORS: command line help + << _(" -d --data : Directory to load game " + "data from") << std::endl + // TRANSLATORS: command line help + << _(" -L --localdata-dir : Directory to use as local data" + " directory") << std::endl + // TRANSLATORS: command line help + << _(" --screenshot-dir : Directory to store screenshots") + << std::endl + // TRANSLATORS: command line help + << _(" --safemode : Start game in safe mode") << std::endl + // TRANSLATORS: command line help + << _(" --renderer : Set renderer type") << std::endl + // TRANSLATORS: command line help + << _(" -T --tests : Start testing drivers and " + "auto configuring") << std::endl +#ifdef USE_OPENGL + // TRANSLATORS: command line help + << _(" -O --no-opengl : Disable OpenGL for this session") + << std::endl +#endif + ; +} + +static void printVersion() +{ + std::cout << strprintf("ManaPlus client %s", FULL_VERSION) << std::endl; +} + +void parseOptions(const int argc, char *const argv[]) +{ + const char *const optstring = "hvud:U:P:Dc:p:y:l:L:C:s:t:T:a:r"; + + const struct option long_options[] = + { + { "config-dir", required_argument, nullptr, 'C' }, + { "data", required_argument, nullptr, 'd' }, + { "default", no_argument, nullptr, 'D' }, + { "password", required_argument, nullptr, 'P' }, + { "character", required_argument, nullptr, 'c' }, + { "help", no_argument, nullptr, 'h' }, + { "localdata-dir", required_argument, nullptr, 'L' }, + { "update-host", required_argument, nullptr, 'H' }, + { "port", required_argument, nullptr, 'p' }, + { "server", required_argument, nullptr, 's' }, + { "skip-update", no_argument, nullptr, 'u' }, + { "username", required_argument, nullptr, 'U' }, + { "no-opengl", no_argument, nullptr, 'O' }, + { "chat-log-dir", required_argument, nullptr, 'a' }, + { "version", no_argument, nullptr, 'v' }, + { "log-file", required_argument, nullptr, 'l' }, + { "screenshot-dir", required_argument, nullptr, 'i' }, + { "safemode", no_argument, nullptr, 'm' }, + { "tests", no_argument, nullptr, 'T' }, + { "test", required_argument, nullptr, 't' }, + { "renderer", required_argument, nullptr, 'r' }, + { "server-type", required_argument, nullptr, 'y' }, + { nullptr, 0, nullptr, 0 } + }; + + Options &options = settings.options; + + while (optind < argc) + { + const int result = getopt_long(argc, argv, + optstring, long_options, nullptr); + + if (result == -1) + break; + + switch (result) + { + case 'C': + options.configDir = optarg; + break; + case 'd': + options.dataPath = optarg; + break; + case 'D': + options.chooseDefault = true; + break; + case '?': // Unknown option + case ':': // Missing argument + case 'h': + options.printHelp = true; + break; + case 'H': + if (checkPath(optarg)) + options.updateHost = optarg; + else + options.updateHost.clear(); + break; + case 'c': + options.character = optarg; + break; + case 'P': + options.password = optarg; + break; + case 's': + options.serverName = optarg; + break; + case 'p': + options.serverPort = static_cast<uint16_t>(atoi(optarg)); + break; + case 'u': + options.skipUpdate = true; + break; + case 'U': + options.username = optarg; + break; + case 'v': + options.printVersion = true; + break; + case 'L': + options.localDataDir = optarg; + break; + case 'O': + options.noOpenGL = true; + break; + case 'l': + options.logFileName = std::string(optarg); + break; + case 'a': + options.chatLogDir = std::string(optarg); + break; + case 'i': + options.screenshotDir = optarg; + break; + case 'm': + options.safeMode = true; + break; + case 'T': + options.testMode = true; + options.test.clear(); + break; + case 't': + options.testMode = true; + options.test = std::string(optarg); + break; + case 'r': + options.renderer = static_cast<int>(atoi(optarg)); + break; + case 'y': + options.serverType = optarg; + break; + default: + break; + } + } + + // when there are still options left use the last + // one as branding file + if (optind < argc) + { + options.brandingPath = argv[optind]; + } + + if (settings.options.printHelp) + { + printHelp(); + _exit(0); + } + else if (settings.options.printVersion) + { + printVersion(); + _exit(0); + } +} diff --git a/src/commandline.h b/src/commandline.h new file mode 100644 index 000000000..1d95cdc53 --- /dev/null +++ b/src/commandline.h @@ -0,0 +1,30 @@ +/* + * The ManaPlus Client + * Copyright (C) 2004-2009 The Mana World Development Team + * Copyright (C) 2009-2010 The Mana Developers + * Copyright (C) 2011-2015 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef COMMANDLINE_H +#define COMMANDLINE_H + +#include "localconsts.h" + +void parseOptions(const int argc, char *const argv[]); + +#endif // COMMANDLINE_H diff --git a/src/main.cpp b/src/main.cpp index 907cc5c89..40d7bc676 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -24,14 +24,13 @@ #include "main.h" #include "client.h" +#include "commandline.h" #include "settings.h" #include "utils/delete2.h" -#include "utils/gettext.h" #ifdef ANDROID #include "utils/mkdir.h" #endif -#include "utils/paths.h" #include "utils/physfscheckutils.h" #include "utils/physfsrwops.h" #include "utils/process.h" @@ -75,209 +74,6 @@ char *selfName = nullptr; -static void printHelp() -{ - std::cout - // TRANSLATORS: command line help - << _("manaplus [options] [manaplus-file]") << std::endl << std::endl - // TRANSLATORS: command line help - << _("[manaplus-file] : The manaplus file is an XML file (.manaplus)") - << std::endl - // TRANSLATORS: command line help - << _(" used to set custom parameters") << std::endl - // TRANSLATORS: command line help - << _(" to the manaplus client.") - << std::endl << std::endl - // TRANSLATORS: command line help - << _("Options:") << std::endl - // TRANSLATORS: command line help - << _(" -l --log-file : Log file to use") << std::endl - // TRANSLATORS: command line help - << _(" -a --chat-log-dir : Chat log dir to use") << std::endl - // TRANSLATORS: command line help - << _(" -v --version : Display the version") << std::endl - // TRANSLATORS: command line help - << _(" -h --help : Display this help") << std::endl - // TRANSLATORS: command line help - << _(" -C --config-dir : Configuration directory to use") - << std::endl - // TRANSLATORS: command line help - << _(" -U --username : Login with this username") << std::endl - // TRANSLATORS: command line help - << _(" -P --password : Login with this password") << std::endl - // TRANSLATORS: command line help - << _(" -c --character : Login with this character") << std::endl - // TRANSLATORS: command line help - << _(" -s --server : Login server name or IP") << std::endl - // TRANSLATORS: command line help - << _(" -y --server-type : Login server type") << std::endl - // TRANSLATORS: command line help - << _(" -p --port : Login server port") << std::endl - // TRANSLATORS: command line help - << _(" -H --update-host : Use this update host") << std::endl - // TRANSLATORS: command line help - << _(" -D --default : Choose default character server and " - "character") << std::endl - // TRANSLATORS: command line help - << _(" -u --skip-update : Skip the update downloads") << std::endl - // TRANSLATORS: command line help - << _(" -d --data : Directory to load game " - "data from") << std::endl - // TRANSLATORS: command line help - << _(" -L --localdata-dir : Directory to use as local data" - " directory") << std::endl - // TRANSLATORS: command line help - << _(" --screenshot-dir : Directory to store screenshots") - << std::endl - // TRANSLATORS: command line help - << _(" --safemode : Start game in safe mode") << std::endl - // TRANSLATORS: command line help - << _(" --renderer : Set renderer type") << std::endl - // TRANSLATORS: command line help - << _(" -T --tests : Start testing drivers and " - "auto configuring") << std::endl -#ifdef USE_OPENGL - // TRANSLATORS: command line help - << _(" -O --no-opengl : Disable OpenGL for this session") - << std::endl -#endif - ; -} - -static void printVersion() -{ - std::cout << strprintf("ManaPlus client %s", FULL_VERSION) << std::endl; -} - -static void parseOptions(const int argc, char *const argv[]) -{ - const char *const optstring = "hvud:U:P:Dc:p:y:l:L:C:s:t:T:a:r"; - - const struct option long_options[] = - { - { "config-dir", required_argument, nullptr, 'C' }, - { "data", required_argument, nullptr, 'd' }, - { "default", no_argument, nullptr, 'D' }, - { "password", required_argument, nullptr, 'P' }, - { "character", required_argument, nullptr, 'c' }, - { "help", no_argument, nullptr, 'h' }, - { "localdata-dir", required_argument, nullptr, 'L' }, - { "update-host", required_argument, nullptr, 'H' }, - { "port", required_argument, nullptr, 'p' }, - { "server", required_argument, nullptr, 's' }, - { "skip-update", no_argument, nullptr, 'u' }, - { "username", required_argument, nullptr, 'U' }, - { "no-opengl", no_argument, nullptr, 'O' }, - { "chat-log-dir", required_argument, nullptr, 'a' }, - { "version", no_argument, nullptr, 'v' }, - { "log-file", required_argument, nullptr, 'l' }, - { "screenshot-dir", required_argument, nullptr, 'i' }, - { "safemode", no_argument, nullptr, 'm' }, - { "tests", no_argument, nullptr, 'T' }, - { "test", required_argument, nullptr, 't' }, - { "renderer", required_argument, nullptr, 'r' }, - { "server-type", required_argument, nullptr, 'y' }, - { nullptr, 0, nullptr, 0 } - }; - - Options &options = settings.options; - - while (optind < argc) - { - const int result = getopt_long(argc, argv, - optstring, long_options, nullptr); - - if (result == -1) - break; - - switch (result) - { - case 'C': - options.configDir = optarg; - break; - case 'd': - options.dataPath = optarg; - break; - case 'D': - options.chooseDefault = true; - break; - case '?': // Unknown option - case ':': // Missing argument - case 'h': - options.printHelp = true; - break; - case 'H': - if (checkPath(optarg)) - options.updateHost = optarg; - else - options.updateHost.clear(); - break; - case 'c': - options.character = optarg; - break; - case 'P': - options.password = optarg; - break; - case 's': - options.serverName = optarg; - break; - case 'p': - options.serverPort = static_cast<uint16_t>(atoi(optarg)); - break; - case 'u': - options.skipUpdate = true; - break; - case 'U': - options.username = optarg; - break; - case 'v': - options.printVersion = true; - break; - case 'L': - options.localDataDir = optarg; - break; - case 'O': - options.noOpenGL = true; - break; - case 'l': - options.logFileName = std::string(optarg); - break; - case 'a': - options.chatLogDir = std::string(optarg); - break; - case 'i': - options.screenshotDir = optarg; - break; - case 'm': - options.safeMode = true; - break; - case 'T': - options.testMode = true; - options.test.clear(); - break; - case 't': - options.testMode = true; - options.test = std::string(optarg); - break; - case 'r': - options.renderer = static_cast<int>(atoi(optarg)); - break; - case 'y': - options.serverType = optarg; - break; - default: - break; - } - } - - // when there are still options left use the last - // one as branding file - if (optind < argc) - { - options.brandingPath = argv[optind]; - } -} - #ifdef WIN32 extern "C" char const *_nl_locale_name_default(void); #endif @@ -296,17 +92,6 @@ int main(int argc, char *argv[]) parseOptions(argc, argv); - if (settings.options.printHelp) - { - printHelp(); - _exit(0); - } - else if (settings.options.printVersion) - { - printVersion(); - _exit(0); - } - std::ios::sync_with_stdio(false); #ifdef ANDROID |