ManaPlus
commandline.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2004-2009 The Mana World Development Team
4  * Copyright (C) 2009-2010 The Mana Developers
5  * Copyright (C) 2011-2019 The ManaPlus Developers
6  * Copyright (C) 2019-2021 Andrei Karas
7  *
8  * This file is part of The ManaPlus Client.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 #include "commandline.h"
25 
26 #include "main.h"
27 #include "settings.h"
28 
29 #include "fs/paths.h"
30 
31 #include "utils/cast.h"
32 #include "utils/gettext.h"
33 #include "utils/stringutils.h"
34 
35 #include <getopt.h>
36 #include <iostream>
37 #include <unistd.h>
38 
39 #include "debug.h"
40 
41 static void printHelp()
42 {
43  std::cout <<
44  // TRANSLATORS: command line help
45  _("manaplus [options] [manaplus-file]") <<
46  std::endl << std::endl <<
47  // TRANSLATORS: command line help
48  _("[manaplus-file] : The manaplus file is an XML file (.manaplus)") <<
49  std::endl <<
50  // TRANSLATORS: command line help
51  _(" used to set custom parameters") <<
52  std::endl <<
53  // TRANSLATORS: command line help
54  _(" to the manaplus client.") <<
55  std::endl << std::endl <<
56  // TRANSLATORS: command line help
57  _("Options:") <<
58  std::endl <<
59  // TRANSLATORS: command line help
60  _(" -l --log-file : Log file to use") << std::endl <<
61  // TRANSLATORS: command line help
62  _(" -a --chat-log-dir : Chat log dir to use") << std::endl <<
63  // TRANSLATORS: command line help
64  _(" -v --version : Display the version") << std::endl <<
65  // TRANSLATORS: command line help
66  _(" -h --help : Display this help") << std::endl <<
67  // TRANSLATORS: command line help
68  _(" -C --config-dir : Configuration directory to use") <<
69  std::endl <<
70  // TRANSLATORS: command line help
71  _(" -U --username : Login with this username") <<
72  std::endl <<
73  // TRANSLATORS: command line help
74  _(" -P --password : Login with this password") <<
75  std::endl <<
76  // TRANSLATORS: command line help
77  _(" -c --character : Login with this character") <<
78  std::endl <<
79  // TRANSLATORS: command line help
80  _(" -s --server : Login server name or IP") <<
81  std::endl <<
82  // TRANSLATORS: command line help
83  _(" -y --server-type : Login server type") <<
84  std::endl <<
85  // TRANSLATORS: command line help
86  _(" -p --port : Login server port") <<
87  std::endl <<
88  // TRANSLATORS: command line help
89  _(" -H --update-host : Use this update host") <<
90  std::endl <<
91  // TRANSLATORS: command line help
92  _(" -D --default : Choose default character server and "
93  "character") <<
94  std::endl <<
95  // TRANSLATORS: command line help
96  _(" -u --skip-update : Skip the update downloads") <<
97  std::endl <<
98  // TRANSLATORS: command line help
99  _(" -d --data : Directory to load game data from") <<
100  std::endl <<
101  // TRANSLATORS: command line help
102  _(" -L --localdata-dir : Directory to use as local "
103  "data directory") <<
104  std::endl <<
105  // TRANSLATORS: command line help
106  _(" --screenshot-dir : Directory to store screenshots") <<
107  std::endl <<
108  // TRANSLATORS: command line help
109  _(" --safemode : Start game in safe mode") <<
110  std::endl <<
111  // TRANSLATORS: command line help
112  _(" --renderer : Set renderer type") <<
113  std::endl <<
114  // TRANSLATORS: command line help
115  _(" -T --tests : Start testing drivers and "
116  "auto configuring") <<
117  std::endl <<
118  // TRANSLATORS: command line help
119  _(" -V --validate : Start validating client data") <<
120  std::endl
121 #ifdef USE_OPENGL
122  <<
123  // TRANSLATORS: command line help
124  _(" -O --no-opengl : Disable OpenGL for this session") <<
125  std::endl
126 #endif // USE_OPENGL
127  ;
128 }
129 
130 static void printVersion()
131 {
132  std::cout <<
133  strprintf("ManaPlus client %s", FULL_VERSION) <<
134  std::endl;
135 }
136 
137 void parseOptions(const int argc, char *const argv[])
138 {
139  const char *const optstring = "hvud:U:P:Dc:p:y:l:L:C:s:t:T:a:r:e:V";
140 
141  const option long_options[] =
142  {
143  { "config-dir", required_argument, nullptr, 'C' },
144  { "data", required_argument, nullptr, 'd' },
145  { "default", no_argument, nullptr, 'D' },
146  { "password", required_argument, nullptr, 'P' },
147  { "character", required_argument, nullptr, 'c' },
148  { "help", no_argument, nullptr, 'h' },
149  { "localdata-dir", required_argument, nullptr, 'L' },
150  { "update-host", required_argument, nullptr, 'H' },
151  { "port", required_argument, nullptr, 'p' },
152  { "server", required_argument, nullptr, 's' },
153  { "skip-update", no_argument, nullptr, 'u' },
154  { "username", required_argument, nullptr, 'U' },
155  { "no-opengl", no_argument, nullptr, 'O' },
156  { "chat-log-dir", required_argument, nullptr, 'a' },
157  { "version", no_argument, nullptr, 'v' },
158  { "log-file", required_argument, nullptr, 'l' },
159  { "screenshot-dir", required_argument, nullptr, 'i' },
160  { "safemode", no_argument, nullptr, 'm' },
161  { "tests", no_argument, nullptr, 'T' },
162  { "test", required_argument, nullptr, 't' },
163  { "renderer", required_argument, nullptr, 'r' },
164  { "server-type", required_argument, nullptr, 'y' },
165  { "enable-ipc", no_argument, nullptr, 'I' },
166  { "hide-cursor", no_argument, nullptr, 'q' },
167  { "error", no_argument, nullptr, 'e' },
168  { "validate", no_argument, nullptr, 'V' },
169  { nullptr, 0, nullptr, 0 }
170  };
171 
172  Options &options = settings.options;
173 
174  while (optind < argc)
175  {
176  const int result = getopt_long(argc,
177  argv,
178  optstring,
179  long_options,
180  nullptr);
181 
182  if (result == -1)
183  break;
184 
185  switch (result)
186  {
187  case 'C':
188  options.configDir = optarg;
189  break;
190  case 'd':
191  options.dataPath = optarg;
192  break;
193  case 'D':
194  options.chooseDefault = true;
195  break;
196  case '?': // Unknown option
197  case ':': // Missing argument
198  case 'h':
199  options.printHelp = true;
200  break;
201  case 'H':
202  if (checkPath(optarg))
203  options.updateHost = optarg;
204  else
205  options.updateHost.clear();
206  break;
207  case 'c':
208  options.character = optarg;
209  break;
210  case 'P':
211  options.password = optarg;
212  break;
213  case 's':
214  options.serverName = optarg;
215  break;
216  case 'p':
217  options.serverPort = CAST_U16(atoi(optarg));
218  break;
219  case 'u':
220  options.skipUpdate = true;
221  break;
222  case 'U':
223  options.username = optarg;
224  break;
225  case 'v':
226  options.printVersion = true;
227  break;
228  case 'L':
229  options.localDataDir = optarg;
230  break;
231  case 'O':
232  options.noOpenGL = true;
233  break;
234  case 'l':
235  options.logFileName = std::string(optarg);
236  break;
237  case 'a':
238  options.chatLogDir = std::string(optarg);
239  break;
240  case 'i':
241  options.screenshotDir = optarg;
242  break;
243  case 'm':
244  options.safeMode = true;
245  break;
246  case 'T':
247  options.testMode = true;
248  options.test.clear();
249  break;
250  case 't':
251  options.testMode = true;
252  options.test = std::string(optarg);
253  break;
254  case 'r':
255  options.renderer = CAST_S32(atoi(optarg));
256  break;
257  case 'y':
258  options.serverType = optarg;
259  break;
260  case 'I':
261  options.ipc = true;
262  break;
263  case 'q':
264  options.hideCursor = true;
265  break;
266  case 'e':
267  options.error = true;
268  break;
269  case 'V':
270  options.validate = true;
271  break;
272  default:
273  break;
274  }
275  }
276 
277  // when there are still options left use the last
278  // one as branding file
279  if (optind < argc)
280  {
281  options.brandingPath = argv[optind];
282  }
283 
285  {
286  printHelp();
287  _exit(0);
288  }
289  else if (settings.options.printVersion)
290  {
291  printVersion();
292  _exit(0);
293  }
294 }
#define CAST_U16
Definition: cast.h:29
#define CAST_S32
Definition: cast.h:30
Options options
Definition: settings.h:130
void parseOptions(const int argc, char *const argv[])
static void printHelp()
Definition: commandline.cpp:41
static void printVersion()
#define _(s)
Definition: gettext.h:35
#define FULL_VERSION
Definition: main.h:164
bool checkPath(const std::string &path)
Definition: paths.cpp:121
Settings settings
Definition: settings.cpp:32
std::string strprintf(const char *const format,...)
bool validate
Definition: options.h:103
bool printHelp
Definition: options.h:93
bool noOpenGL
Definition: options.h:97
std::string brandingPath
Definition: options.h:79
bool safeMode
Definition: options.h:98
std::string chatLogDir
Definition: options.h:84
std::string test
Definition: options.h:88
std::string screenshotDir
Definition: options.h:87
bool skipUpdate
Definition: options.h:95
bool chooseDefault
Definition: options.h:96
bool hideCursor
Definition: options.h:101
std::string updateHost
Definition: options.h:80
bool ipc
Definition: options.h:100
std::string configDir
Definition: options.h:85
uint16_t serverPort
Definition: options.h:92
std::string localDataDir
Definition: options.h:86
std::string character
Definition: options.h:78
bool error
Definition: options.h:102
std::string dataPath
Definition: options.h:81
bool printVersion
Definition: options.h:94
std::string logFileName
Definition: options.h:83
std::string password
Definition: options.h:77
int renderer
Definition: options.h:91
std::string serverType
Definition: options.h:90
bool testMode
Definition: options.h:99
std::string serverName
Definition: options.h:89
std::string username
Definition: options.h:76