The Battle for Wesnoth  1.15.1+dev
commandline_options.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2011 - 2018 by Lukasz Dobrogowski <lukasz.dobrogowski@gmail.com>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any lfooater version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #include "commandline_options.hpp"
16 
17 #include "config.hpp"
18 #include "formatter.hpp"
19 #include "lexical_cast.hpp"
20 #include "log.hpp" // for logger, set_strict_severity, etc
21 #include "serialization/string_utils.hpp" // for split
22 #include "utils/general.hpp" // for clamp
23 
24 #include <boost/any.hpp> // for any
25 #include <boost/program_options/cmdline.hpp>
26 #include <boost/program_options/errors.hpp> // for validation_error, etc
27 #include <boost/program_options/parsers.hpp>
28 #include <boost/program_options/positional_options.hpp>
29 #include <boost/program_options/value_semantic.hpp> // for value, etc
30 #include <boost/program_options/variables_map.hpp> // for variables_map, etc
31 #include <iostream> // for operator<<, basic_ostream, etc
32 
33 namespace po = boost::program_options;
34 
35 class two_strings : public std::pair<std::string,std::string> {};
36 
37 static void validate(boost::any& v, const std::vector<std::string>& values,
38  two_strings*, int)
39 {
40  two_strings ret_val;
41  if (values.size() != 2) {
42  throw po::validation_error(po::validation_error::invalid_option_value);
43  }
44  ret_val.first = values.at(0);
45  ret_val.second = values.at(1);
46  v = ret_val;
47 }
48 
50  : error(formatter() << "Invalid resolution \"" << resolution
51  << "\" (WIDTHxHEIGHT expected)")
52 {
53 }
54 
56  const std::string& expected_format)
57  : error(formatter() << "Invalid value set \"" << str
58  << "\" (" << expected_format << " expected)")
59 {
60 }
61 
62 commandline_options::commandline_options (const std::vector<std::string>& args) :
63  bunzip2(),
64  bzip2(),
65  campaign(),
66  campaign_difficulty(),
67  campaign_scenario(),
68  campaign_skip_story(false),
69  clock(false),
70  core_id(),
71  data_path(false),
72  data_dir(),
73  debug(false),
74  debug_lua(false),
75 #ifdef DEBUG_WINDOW_LAYOUT_GRAPHS
76  debug_dot_domain(),
77  debug_dot_level(),
78 #endif
79  editor(),
80  fps(false),
81  fullscreen(false),
82  gunzip(),
83  gzip(),
84  help(),
85  language(),
86  log(),
87  load(),
88  logdomains(),
89  log_precise_timestamps(false),
90  multiplayer(false),
91  multiplayer_ai_config(),
92  multiplayer_algorithm(),
93  multiplayer_controller(),
94  multiplayer_era(),
95  multiplayer_exit_at_end(),
96  multiplayer_ignore_map_settings(),
97  multiplayer_label(),
98  multiplayer_parm(),
99  multiplayer_repeat(),
100  multiplayer_scenario(),
101  multiplayer_side(),
102  multiplayer_turns(),
103  max_fps(),
104  noaddons(false),
105  nocache(false),
106  nodelay(false),
107  nogui(false),
108  nomusic(false),
109  nosound(false),
110  new_widgets(false),
111  preprocess(false),
112  preprocess_defines(),
113  preprocess_input_macros(),
114  preprocess_output_macros(),
115  preprocess_path(),
116  preprocess_target(),
117  resolution(),
118  rng_seed(),
119  server(),
120  username(),
121  password(),
122  render_image(),
123  render_image_dst(),
124  screenshot(false),
125  screenshot_map_file(),
126  screenshot_output_file(),
127  script_file(),
128  plugin_file(),
129  script_unsafe_mode(false),
130  strict_validation(false),
131  test(),
132  unit_test(),
133  headless_unit_test(false),
134  timeout(),
135  noreplaycheck(false),
136  mptest(false),
137  userconfig_path(false),
138  userconfig_dir(),
139  userdata_path(false),
140  userdata_dir(),
141  validcache(false),
142  validate_core(false),
143  validate_addon(),
144  validate_schema(),
145  validate_wml(),
146  validate_with(),
147  version(false),
148  report(false),
149  windowed(false),
150  with_replay(false),
151  translation_percent(),
152  args_(args.begin() + 1 , args.end()),
153  args0_(*args.begin()),
154  all_(),
155  visible_(),
156  hidden_()
157 {
158  // When adding items don't forget to update doc/man/wesnoth.6
159  // Options are sorted alphabetically by --long-option.
160  po::options_description general_opts("General options");
161  general_opts.add_options()
162  ("all-translations", "Show all translations, even incomplete ones.")
163  ("bunzip2", po::value<std::string>(), "decompresses a file (<arg>.bz2) in bzip2 format and stores it without the .bz2 suffix. <arg>.bz2 will be removed.")
164  ("bzip2", po::value<std::string>(), "compresses a file (<arg>) in bzip2 format, stores it as <arg>.bz2 and removes <arg>.")
165  ("clock", "Adds the option to show a clock for testing the drawing timer.")
166  ("config-dir", po::value<std::string>(), "sets the path of the userdata directory to $HOME/<arg> or My Documents\\My Games\\<arg> for Windows. You can specify also an absolute path outside the $HOME or My Documents\\My Games directory. DEPRECATED: use userdata-dir instead.")
167  ("config-path", "prints the path of the userdata directory and exits. DEPRECATED: use userdata-path instead.")
168  ("core", po::value<std::string>(), "overrides the loaded core with the one whose id is specified.")
169  ("data-dir", po::value<std::string>(), "overrides the data directory with the one specified.")
170  ("data-path", "prints the path of the data directory and exits.")
171  ("debug,d", "enables additional command mode options in-game.")
172  ("debug-lua", "enables some Lua debugging mechanisms")
173 #ifdef DEBUG_WINDOW_LAYOUT_GRAPHS
174  ("debug-dot-level", po::value<std::string>(), "sets the level of the debug dot files. <arg> should be a comma separated list of levels. These files are used for debugging the widgets especially the for the layout engine. When enabled the engine will produce dot files which can be converted to images with the dot tool. Available levels: size (generate the size info of the widget), state (generate the state info of the widget).")
175  ("debug-dot-domain", po::value<std::string>(), "sets the domain of the debug dot files. <arg> should be a comma separated list of domains. See --debug-dot-level for more info. Available domains: show (generate the data when the dialog is about to be shown), layout (generate the data during the layout phase - might result in multiple files). The data can also be generated when the F12 is pressed in a dialog.")
176 #endif
177  ("editor,e", po::value<std::string>()->implicit_value(std::string()), "starts the in-game map editor directly. If file <arg> is specified, equivalent to -e --load <arg>.")
178  ("gunzip", po::value<std::string>(), "decompresses a file (<arg>.gz) in gzip format and stores it without the .gz suffix. <arg>.gz will be removed.")
179  ("gzip", po::value<std::string>(), "compresses a file (<arg>) in gzip format, stores it as <arg>.gz and removes <arg>.")
180  ("help,h", "prints this message and exits.")
181  ("language,L", po::value<std::string>(), "uses language <arg> (symbol) this session. Example: --language ang_GB@latin")
182  ("load,l", po::value<std::string>(), "loads the save <arg> from the standard save game directory. When launching the map editor via -e, the map <arg> is loaded, relative to the current directory. If it is a directory, the editor will start with a load map dialog opened there.")
183  ("noaddons", "disables the loading of all add-ons.")
184  ("nocache", "disables caching of game data.")
185  ("nodelay", "runs the game without any delays.")
186  ("nomusic", "runs the game without music.")
187  ("nosound", "runs the game without sounds and music.")
188  ("password", po::value<std::string>(), "uses <password> when connecting to a server, ignoring other preferences.")
189  ("plugin", po::value<std::string>(), "(experimental) load a script which defines a wesnoth plugin. similar to --script below, but Lua file should return a function which will be run as a coroutine and periodically woken up with updates.")
190  ("render-image", po::value<two_strings>()->multitoken(), "takes two arguments: <image> <output>. Like screenshot, but instead of a map, takes a valid Wesnoth 'image path string' with image path functions, and writes it to a .png file."
191 #ifdef _WIN32
192  " Implies --wconsole."
193 #endif // _WIN32
194  )
195  ("report,R", "initializes game directories, prints build information suitable for use in bug reports, and exits."
196 #ifdef _WIN32
197  " Implies --wconsole."
198 #endif // _WIN32
199  )
200  ("rng-seed", po::value<unsigned int>(), "seeds the random number generator with number <arg>. Example: --rng-seed 0")
201  ("screenshot", po::value<two_strings>()->multitoken(), "takes two arguments: <map> <output>. Saves a screenshot of <map> to <output> without initializing a screen. Editor must be compiled in for this to work."
202 #ifdef _WIN32
203  " Implies --wconsole."
204 #endif // _WIN32
205  )
206  ("script", po::value<std::string>(), "(experimental) file containing a Lua script to control the client")
207  ("server,s", po::value<std::string>()->implicit_value(std::string()), "connects to the host <arg> if specified or to the first host in your preferences.")
208  ("strict-validation", "makes validation errors fatal")
209  ("translations-over", po::value<unsigned int>(), "Specify the standard for determining whether a translation is complete.")
210  ("unsafe-scripts", "makes the \'package\' package available to Lua scripts, so that they can load arbitrary packages. Do not do this with untrusted scripts! This action gives ua the same permissions as the Wesnoth executable.")
211  ("userconfig-dir", po::value<std::string>(), "sets the path of the user config directory to $HOME/<arg> or My Documents\\My Games\\<arg> for Windows. You can specify also an absolute path outside the $HOME or My Documents\\My Games directory. Defaults to $HOME/.config/wesnoth on X11 and to the userdata-dir on other systems.")
212  ("userconfig-path", "prints the path of the user config directory and exits.")
213  ("userdata-dir", po::value<std::string>(), "sets the path of the userdata directory to $HOME/<arg> or My Documents\\My Games\\<arg> for Windows. You can specify also an absolute path outside the $HOME or My Documents\\My Games directory.")
214  ("userdata-path", "prints the path of the userdata directory and exits.")
215  ("username", po::value<std::string>(), "uses <username> when connecting to a server, ignoring other preferences.")
216  ("validcache", "assumes that the cache is valid. (dangerous)")
217  ("version,v", "prints the game's version number and exits.")
218  ("with-replay", "replays the file loaded with the --load option.")
219 #ifdef _WIN32
220  ("wconsole", "attaches a console window on startup (Windows only). Implied by any option that prints something and exits.")
221 #endif // _WIN32
222  ;
223 
224  po::options_description campaign_opts("Campaign options");
225  campaign_opts.add_options()
226  ("campaign,c", po::value<std::string>()->implicit_value(std::string()), "goes directly to the campaign with id <arg>. A selection menu will appear if no id was specified.")
227  ("campaign-difficulty", po::value<int>(), "The difficulty of the specified campaign (1 to max). If none specified, the campaign difficulty selection widget will appear.")
228  ("campaign-scenario", po::value<std::string>(),"The id of the scenario from the specified campaign. The default is the first scenario.")
229  ("campaign-skip-story", "Skip [story] tags of the specified campaign.")
230  ;
231 
232  po::options_description display_opts("Display options");
233  display_opts.add_options()
234  ("fps", "displays the number of frames per second the game is currently running at, in a corner of the screen. Min/avg/max don't take the FPS limiter into account, act does.")
235  ("fullscreen,f", "runs the game in full screen mode.")
236  ("max-fps", po::value<int>(), "the maximum fps the game tries to run at. Values should be between 1 and 1000, the default is the display's refresh rate.")
237  ("new-widgets", "there is a new WIP widget toolkit this switch enables the new toolkit (VERY EXPERIMENTAL don't file bug reports since most are known). Parts of the library are deemed stable and will work without this switch.")
238  ("resolution,r", po::value<std::string>(), "sets the screen resolution. <arg> should have format XxY. Example: --resolution 800x600")
239  ("windowed,w", "runs the game in windowed mode.")
240  ;
241 
242  po::options_description logging_opts("Logging options");
243  logging_opts.add_options()
244  ("logdomains", po::value<std::string>()->implicit_value(std::string()), "lists defined log domains (only the ones containing <arg> filter if such is provided) and exits.")
245  ("log-error", po::value<std::string>(), "sets the severity level of the specified log domain(s) to 'error'. <arg> should be given as comma separated list of domains, wildcards are allowed. Example: --log-error=network,gui/*,engine/enemies")
246  ("log-warning", po::value<std::string>(), "sets the severity level of the specified log domain(s) to 'warning'. Similar to --log-error.")
247  ("log-info", po::value<std::string>(), "sets the severity level of the specified log domain(s) to 'info'. Similar to --log-error.")
248  ("log-debug", po::value<std::string>(), "sets the severity level of the specified log domain(s) to 'debug'. Similar to --log-error.")
249  ("log-none", po::value<std::string>(), "sets the severity level of the specified log domain(s) to 'none'. Similar to --log-error.")
250  ("log-precise", "shows the timestamps in the logfile with more precision.")
251  ;
252 
253  po::options_description multiplayer_opts("Multiplayer options");
254  multiplayer_opts.add_options()
255  ("multiplayer,m", "Starts a multiplayer game. There are additional options that can be used as explained below:")
256  ("ai-config", po::value<std::vector<std::string>>()->composing(), "selects a configuration file to load for this side. <arg> should have format side:value")
257  ("algorithm", po::value<std::vector<std::string>>()->composing(), "selects a non-standard algorithm to be used by the AI controller for this side. <arg> should have format side:value")
258  ("controller", po::value<std::vector<std::string>>()->composing(), "selects the controller for this side. <arg> should have format side:value")
259  ("era", po::value<std::string>(), "selects the era to be played in by its id.")
260  ("exit-at-end", "exit Wesnoth at the end of the scenario.")
261  ("ignore-map-settings", "do not use map settings.")
262  ("label", po::value<std::string>(), "sets the label for AIs.") //TODO is the description precise? this option was undocumented before.
263  ("multiplayer-repeat", po::value<unsigned int>(), "repeats a multiplayer game after it is finished <arg> times.")
264  ("nogui", "runs the game without the GUI.")
265  ("parm", po::value<std::vector<std::string>>()->composing(), "sets additional parameters for this side. <arg> should have format side:name:value.")
266  ("scenario", po::value<std::string>(), "selects a multiplayer scenario. The default scenario is \"multiplayer_The_Freelands\".")
267  ("side", po::value<std::vector<std::string>>()->composing(), "selects a faction of the current era for this side by id. <arg> should have format side:value.")
268  ("turns", po::value<std::string>(), "sets the number of turns. By default no turn limit is set.")
269  ;
270 
271  po::options_description testing_opts("Testing options");
272  testing_opts.add_options()
273  ("test,t", po::value<std::string>()->implicit_value(std::string()), "runs the game in a small test scenario. If specified, scenario <arg> will be used instead.")
274  ("unit,u", po::value<std::string>()->implicit_value(std::string()), "runs a unit test scenario. Works like test, except that the exit code of the program reflects the victory / defeat conditions of the scenario.\n\t0 - PASS\n\t1 - FAIL\n\t2 - FAIL (TIMEOUT)\n\t3 - FAIL (INVALID REPLAY)\n\t4 - FAIL (ERRORED REPLAY)")
275  ("showgui", "don't run headlessly (for debugging a failing test)")
276  ("timeout", po::value<unsigned int>(), "sets a timeout (milliseconds) for the unit test. (DEPRECATED)")
277  ("log-strict", po::value<std::string>(), "sets the strict level of the logger. any messages sent to log domains of this level or more severe will cause the unit test to fail regardless of the victory result.")
278  ("noreplaycheck", "don't try to validate replay of unit test.")
279  ("mp-test", "load the test mp scenarios.")
280  ("use-schema,S", po::value<std::string>(), "specify a schema to validate WML against (defaults to the core schema)")
281  ("validate,V", po::value<std::string>(), "validate a specified WML file against a schema")
282  ("validate-addon", po::value<std::string>(), "validate the specified addon's WML against the schema")
283  ("validate-core", "validate the core WML against the schema")
284  ("validate-schema", po::value<std::string>(), "validate a specified WML schema")
285  ;
286 
287  po::options_description preprocessor_opts("Preprocessor mode options");
288  preprocessor_opts.add_options()
289  ("preprocess,p", po::value<two_strings>()->multitoken(), "requires two arguments: <file/folder> <target directory>. Preprocesses a specified file/folder. The preprocessed file(s) will be written in the specified target directory: a plain cfg file and a processed cfg file.")
290  ("preprocess-defines", po::value<std::string>(), "comma separated list of defines to be used by '--preprocess' command. If 'SKIP_CORE' is in the define list the data/core won't be preprocessed. Example: --preprocess-defines=FOO,BAR")
291  ("preprocess-input-macros", po::value<std::string>(), "used only by the '--preprocess' command. Specifies source file <arg> that contains [preproc_define]s to be included before preprocessing.")
292  ("preprocess-output-macros", po::value<std::string>()->implicit_value(std::string()), "used only by the '--preprocess' command. Will output all preprocessed macros in the target file <arg>. If the file is not specified the output will be file '_MACROS_.cfg' in the target directory of preprocess's command.")
293  ;
294 
295  po::options_description proxy_opts("Proxy options");
296  proxy_opts.add_options()
297  ("proxy", "enables usage of proxy for network connections.")
298  ("proxy-address", po::value<std::string>(), "specifies address of the proxy.")
299  ("proxy-port", po::value<std::string>(), "specifies port of the proxy.")
300  ("proxy-user", po::value<std::string>(), "specifies username to log in to the proxy.")
301  ("proxy-password", po::value<std::string>(), "specifies password to log in to the proxy.")
302  ;
303 
304  //hidden_.add_options()
305  // ("example-hidden-option", "")
306  // ;
307  visible_.add(general_opts).add(campaign_opts).add(display_opts).add(logging_opts).add(multiplayer_opts).add(testing_opts).add(preprocessor_opts).add(proxy_opts);
308 
309  all_.add(visible_).add(hidden_);
310 
311  po::positional_options_description positional;
312  positional.add("data-dir",1);
313 
314  po::variables_map vm;
315  const int parsing_style = po::command_line_style::default_style ^ po::command_line_style::allow_guessing;
316  po::store(po::command_line_parser(args_).options(all_).positional(positional).style(parsing_style).run(),vm);
317 
318  if (vm.count("ai-config"))
319  multiplayer_ai_config = parse_to_uint_string_tuples_(vm["ai-config"].as<std::vector<std::string>>());
320  if (vm.count("algorithm"))
321  multiplayer_algorithm = parse_to_uint_string_tuples_(vm["algorithm"].as<std::vector<std::string>>());
322  if (vm.count("bunzip2"))
323  bunzip2 = vm["bunzip2"].as<std::string>();
324  if (vm.count("bzip2"))
325  bzip2 = vm["bzip2"].as<std::string>();
326  if (vm.count("campaign"))
327  campaign = vm["campaign"].as<std::string>();
328  if (vm.count("campaign-difficulty"))
329  campaign_difficulty = vm["campaign-difficulty"].as<int>();
330  if (vm.count("campaign-scenario"))
331  campaign_scenario = vm["campaign-scenario"].as<std::string>();
332  if (vm.count("campaign-skip-story"))
333  campaign_skip_story = true;
334  if (vm.count("clock"))
335  clock = true;
336  if (vm.count("core"))
337  core_id = vm["core"].as<std::string>();
338  if (vm.count("config-dir"))
339  userdata_dir = vm["config-dir"].as<std::string>(); //TODO: complain and remove
340  if (vm.count("config-path"))
341  userdata_path = true; //TODO: complain and remove
342  if (vm.count("controller"))
343  multiplayer_controller = parse_to_uint_string_tuples_(vm["controller"].as<std::vector<std::string>>());
344  if (vm.count("data-dir"))
345  data_dir = vm["data-dir"].as<std::string>();
346  if (vm.count("data-path"))
347  data_path = true;
348  if (vm.count("debug"))
349  debug = true;
350  if (vm.count("debug-lua"))
351  debug_lua = true;
352 #ifdef DEBUG_WINDOW_LAYOUT_GRAPHS
353  if (vm.count("debug-dot-domain")) {
354  debug_dot_domain = vm["debug-dot-domain"].as<std::string>();
355  }
356  if (vm.count("debug-dot-level")) {
357  debug_dot_level = vm["debug-dot-level"].as<std::string>();
358  }
359 #endif
360  if (vm.count("editor"))
361  editor = vm["editor"].as<std::string>();
362  if (vm.count("era"))
363  multiplayer_era = vm["era"].as<std::string>();
364  if (vm.count("exit-at-end"))
366  if (vm.count("fps"))
367  fps = true;
368  if (vm.count("fullscreen"))
369  fullscreen = true;
370  if (vm.count("gunzip"))
371  gunzip = vm["gunzip"].as<std::string>();
372  if (vm.count("gzip"))
373  gzip = vm["gzip"].as<std::string>();
374  if (vm.count("help"))
375  help = true;
376  if (vm.count("ignore-map-settings"))
378  if (vm.count("label"))
379  multiplayer_label = vm["label"].as<std::string>();
380  if (vm.count("language"))
381  language = vm["language"].as<std::string>();
382  if (vm.count("load"))
383  load = vm["load"].as<std::string>();
384  if (vm.count("log-error"))
385  parse_log_domains_(vm["log-error"].as<std::string>(),lg::err().get_severity());
386  if (vm.count("log-warning"))
387  parse_log_domains_(vm["log-warning"].as<std::string>(),lg::warn().get_severity());
388  if (vm.count("log-info"))
389  parse_log_domains_(vm["log-info"].as<std::string>(),lg::info().get_severity());
390  if (vm.count("log-debug"))
391  parse_log_domains_(vm["log-debug"].as<std::string>(),lg::debug().get_severity());
392  if (vm.count("log-none"))
393  parse_log_domains_(vm["log-none"].as<std::string>(),-1);
394  if (vm.count("logdomains"))
395  logdomains = vm["logdomains"].as<std::string>();
396  if (vm.count("log-precise"))
397  log_precise_timestamps = true;
398  if (vm.count("log-strict"))
399  parse_log_strictness(vm["log-strict"].as<std::string>());
400  if (vm.count("max-fps"))
401  max_fps = vm["max-fps"].as<int>();
402  if (vm.count("mp-test"))
403  mptest = true;
404  if (vm.count("multiplayer"))
405  multiplayer = true;
406  if (vm.count("multiplayer-repeat"))
407  multiplayer_repeat = vm["multiplayer-repeat"].as<unsigned int>();
408  if (vm.count("new-widgets"))
409  new_widgets = true;
410  if (vm.count("noaddons"))
411  noaddons = true;
412  if (vm.count("nocache"))
413  nocache = true;
414  if (vm.count("nodelay"))
415  nodelay = true;
416  if (vm.count("nomusic"))
417  nomusic = true;
418  if (vm.count("noreplaycheck"))
419  noreplaycheck = true;
420  if (vm.count("nosound"))
421  nosound = true;
422  if (vm.count("nogui"))
423  nogui = true;
424  if (vm.count("parm"))
425  multiplayer_parm = parse_to_uint_string_string_tuples_(vm["parm"].as<std::vector<std::string>>());
426  if (vm.count("preprocess"))
427  {
428  preprocess = true;
429  preprocess_path = vm["preprocess"].as<two_strings>().first;
430  preprocess_target = vm["preprocess"].as<two_strings>().second;
431  }
432  if (vm.count("preprocess-defines"))
433  preprocess_defines = utils::split(vm["preprocess-defines"].as<std::string>(), ',');
434  if (vm.count("preprocess-input-macros"))
435  preprocess_input_macros = vm["preprocess-input-macros"].as<std::string>();
436  if (vm.count("preprocess-output-macros"))
437  preprocess_output_macros = vm["preprocess-output-macros"].as<std::string>();
438  if (vm.count("resolution"))
439  parse_resolution_(vm["resolution"].as<std::string>());
440  if (vm.count("rng-seed"))
441  rng_seed = vm["rng-seed"].as<unsigned int>();
442  if (vm.count("scenario"))
443  multiplayer_scenario = vm["scenario"].as<std::string>();
444  if (vm.count("render-image"))
445  {
446  render_image = vm["render-image"].as<two_strings>().first;
447  render_image_dst = vm["render-image"].as<two_strings>().second;
448  }
449  if (vm.count("screenshot"))
450  {
451  screenshot = true;
452  screenshot_map_file = vm["screenshot"].as<two_strings>().first;
453  screenshot_output_file = vm["screenshot"].as<two_strings>().second;
454  }
455  if (vm.count("script"))
456  script_file = vm["script"].as<std::string>();
457  if (vm.count("unsafe-scripts"))
458  script_unsafe_mode = true;
459  if (vm.count("plugin"))
460  plugin_file = vm["plugin"].as<std::string>();
461  if (vm.count("server"))
462  server = vm["server"].as<std::string>();
463  if (vm.count("username"))
464  username = vm["username"].as<std::string>();
465  if (vm.count("password"))
466  password = vm["password"].as<std::string>();
467  if (vm.count("report"))
468  report = true;
469  if (vm.count("side"))
470  multiplayer_side = parse_to_uint_string_tuples_(vm["side"].as<std::vector<std::string>>());
471  if (vm.count("test"))
472  test = vm["test"].as<std::string>();
473  if (vm.count("unit"))
474  {
475  unit_test = vm["unit"].as<std::string>();
476  headless_unit_test = true;
477  }
478  if (vm.count("showgui"))
479  headless_unit_test = false;
480  if (vm.count("timeout"))
481  timeout = vm["timeout"].as<unsigned int>();
482  if (vm.count("noreplaycheck"))
483  noreplaycheck = true;
484  if (vm.count("turns"))
485  multiplayer_turns = vm["turns"].as<std::string>();
486  if (vm.count("strict-validation"))
487  strict_validation = true;
488  if (vm.count("userconfig-dir"))
489  userconfig_dir = vm["userconfig-dir"].as<std::string>();
490  if (vm.count("userconfig-path"))
491  userconfig_path = true;
492  if (vm.count("userdata-dir"))
493  userdata_dir = vm["userdata-dir"].as<std::string>();
494  if (vm.count("userdata-path"))
495  userdata_path = true;
496  if (vm.count("validcache"))
497  validcache = true;
498  if (vm.count("validate"))
499  validate_wml = vm["validate"].as<std::string>();
500  if (vm.count("validate-core"))
501  validate_core = true;
502  if (vm.count("validate-addon"))
503  validate_addon = vm["validate-addon"].as<std::string>();
504  if (vm.count("validate-schema"))
505  validate_schema = vm["validate-schema"].as<std::string>();
506  if (vm.count("use-schema"))
507  validate_with = vm["use-schema"].as<std::string>();;
508  if (vm.count("version"))
509  version = true;
510  if (vm.count("windowed"))
511  windowed = true;
512  if (vm.count("with-replay"))
513  with_replay = true;
514  if(vm.count("all-translations"))
516  else if(vm.count("translations-over"))
517  translation_percent = utils::clamp<unsigned int>(vm["translations-over"].as<unsigned int>(), 0, 100);
518 }
519 
520 void commandline_options::parse_log_domains_(const std::string &domains_string, const int severity)
521 {
522  const std::vector<std::string> domains = utils::split(domains_string, ',');
523  for (const std::string& domain : domains)
524  {
525  if (!log)
526  log = std::vector<std::pair<int, std::string>>();
527  log->emplace_back(severity, domain);
528  }
529 }
530 
531 void commandline_options::parse_log_strictness (const std::string & severity ) {
532  static lg::logger const *loggers[] { &lg::err(), &lg::warn(), &lg::info(), &lg::debug() };
533  for (const lg::logger * l : loggers ) {
534  if (severity == l->get_name()) {
536  return ;
537  }
538  }
539  std::cerr << "Unrecognized argument to --log-strict : " << severity << " . \nDisabling strict mode logging." << std::endl;
541 }
542 
543 void commandline_options::parse_resolution_ ( const std::string& resolution_string )
544 {
545  const std::vector<std::string> tokens = utils::split(resolution_string, 'x');
546  if (tokens.size() != 2) {
547  throw bad_commandline_resolution(resolution_string);
548  }
549 
550  int xres, yres;
551 
552  try {
553  xres = std::stoi(tokens[0]);
554  yres = std::stoi(tokens[1]);
555  } catch(const std::invalid_argument &) {
556  throw bad_commandline_resolution(resolution_string);
557  }
558 
559  resolution = std::make_pair(xres, yres);
560 }
561 
562 std::vector<std::pair<unsigned int,std::string>> commandline_options::parse_to_uint_string_tuples_(const std::vector<std::string> &strings, char separator)
563 {
564  std::vector<std::pair<unsigned int,std::string>> vec;
565  const std::string& expected_format
566  = std::string() + "UINT" + separator + "STRING";
567 
568  for (const std::string &s : strings)
569  {
570  const std::vector<std::string> tokens = utils::split(s, separator);
571  if(tokens.size() != 2) {
572  throw bad_commandline_tuple(s, expected_format);
573  }
574 
575  unsigned int temp;
576  try {
577  temp = lexical_cast<unsigned int>(tokens[0]);
578  } catch (const bad_lexical_cast &) {
579  throw bad_commandline_tuple(s, expected_format);
580  }
581 
582  vec.emplace_back(temp, tokens[1]);
583  }
584  return vec;
585 }
586 
587 std::vector<std::tuple<unsigned int,std::string,std::string>> commandline_options::parse_to_uint_string_string_tuples_(const std::vector<std::string> &strings, char separator)
588 {
589  std::vector<std::tuple<unsigned int,std::string,std::string>> vec;
590  const std::string& expected_format
591  = std::string() + "UINT" + separator + "STRING" + separator + "STRING";
592 
593  for (const std::string &s : strings)
594  {
595  const std::vector<std::string> tokens = utils::split(s, separator);
596  if(tokens.size() != 3) {
597  throw bad_commandline_tuple(s, expected_format);
598  }
599 
600  unsigned int temp;
601  try {
602  temp = lexical_cast<unsigned int>(tokens[0]);
603  } catch (const bad_lexical_cast &) {
604  throw bad_commandline_tuple(s, expected_format);
605  }
606 
607  vec.emplace_back(temp, tokens[1], tokens[2]);
608  }
609  return vec;
610 }
611 
612 std::ostream& operator<<(std::ostream &os, const commandline_options& cmdline_opts)
613 {
614  os << "Usage: " << cmdline_opts.args0_ << " [<options>] [<data-directory>]\n";
615  os << cmdline_opts.visible_;
616  return os;
617 }
618 
620  config ret;
621  if (server) {
622  ret["server"] = *server;
623  }
624  if (username) {
625  ret["username"] = *username;
626  }
627  if (password) {
628  ret["password"] = *password;
629  }
630  return ret;
631 }
boost::optional< std::string > core_id
Non-empty if –core was given on the command line. Chooses the core to be loaded. ...
bool log_precise_timestamps
True if –log-precise was given on the command line. Shows timestamps in log with more precision...
boost::optional< std::string > script_file
File to load lua script from.
bool new_widgets
Do we wish to use the new library or not.
Definition: settings.cpp:21
std::vector< std::string > args_
bool mptest
True if –mp-test was given on the command line.
static domain_map * domains
Definition: log.cpp:73
boost::program_options::options_description all_
bool nogui
True if –nogui was given on the command line. Disables GUI.
boost::optional< std::string > multiplayer_turns
Non-empty if –turns was given on the command line. Dependent on –multiplayer.
bool script_unsafe_mode
Whether to load the "package" package for the scripting environment. (This allows to load arbitrary l...
boost::optional< std::string > bzip2
Non-empty if –bzip2 was given on the command line. Compresses a file to .bz2 and exits...
std::vector< game_tip > load(const config &cfg)
Loads the tips from a config.
Definition: tips.cpp:35
commandline_options(const std::vector< std::string > &args)
std::vector< std::tuple< unsigned int, std::string, std::string > > parse_to_uint_string_string_tuples_(const std::vector< std::string > &strings, char separator=':')
A helper function splitting vector of strings of format unsigned int:string:string to vector of tuple...
boost::program_options::options_description hidden_
static l_noret error(LoadState *S, const char *why)
Definition: lundump.cpp:39
bool noaddons
True if –noaddons was given on the command line. Disables the loading of all add-ons.
New lexcical_cast header.
logger & info()
Definition: log.cpp:90
void parse_log_domains_(const std::string &domains_string, const int severity)
bool userdata_path
True if –userdata-path was given on the command line. Prints path to user data directory and exits...
boost::optional< std::string > multiplayer_scenario
Non-empty if –scenario was given on the command line. Dependent on –multiplayer.
bool multiplayer_ignore_map_settings
True if –ignore-map-settings was given at the command line. Do not use map settings.
boost::optional< std::string > username
Non-empty if –username was given on the command line. Forces Wesnoth to use this network username...
boost::optional< std::string > gunzip
Non-empty if –gunzip was given on the command line. Uncompresses a .gz file and exits.
boost::optional< std::string > campaign_scenario
Non-empty if –campaign-scenario was given on the command line. Chooses starting scenario in the camp...
boost::optional< int > max_fps
Max FPS specified by –max-fps option.
boost::optional< unsigned int > rng_seed
RNG seed specified by –rng-seed option. Initializes RNG with given seed.
bool fps
True if –fps was given on the command line. Shows number of fps.
boost::optional< std::string > data_dir
Non-empty if –data-dir was given on the command line. Sets the config dir to the specified one...
bool noreplaycheck
True if –noreplaycheck was given on the command line. Dependent on –unit.
boost::optional< std::string > multiplayer_era
Non-empty if –era was given on the command line. Dependent on –multiplayer.
void parse_resolution_(const std::string &resolution_string)
To lexical_cast(From value)
Lexical cast converts one type to another.
Definitions for the interface to Wesnoth Markup Language (WML).
bad_commandline_tuple(const std::string &str, const std::string &expected_format)
std::vector< std::string > split(const std::string &val, const char c, const int flags)
Splits a (comma-)separated string into a vector of pieces.
boost::optional< std::vector< std::pair< unsigned int, std::string > > > multiplayer_side
Non-empty if –side was given on the command line. Vector of pairs (side number, faction id)...
boost::optional< std::string > plugin_file
File to load a lua plugin (similar to a script) from. Experimental / may replace script.
boost::optional< std::string > multiplayer_label
Non-empty if –label was given on the command line. Dependent on –multiplayer.
boost::optional< unsigned int > timeout
Non-empty if –timeout was given on the command line. Dependent on –unit.
boost::optional< std::string > load
Non-empty if –load was given on the command line. Savegame specified to load after start...
bool preprocess
True if –preprocess was given on the command line. Starts Wesnoth in preprocessor-only mode...
bool with_replay
True if –with-replay was given on the command line. Shows replay of the loaded file.
bool headless_unit_test
True if –unit is used and –showgui is not present.
const config & options()
Definition: game.cpp:582
boost::optional< std::string > validate_schema
Non-empty if –validate-schema was given on the command line. Makes Wesnoth validate a WML schema...
boost::optional< std::string > userconfig_dir
Non-empty if –userconfig-dir was given on the command line. Sets the user config dir to the specifie...
bool report
True if –report was given on the command line. Prints a bug report-style info dump and exits...
boost::optional< std::vector< std::pair< unsigned int, std::string > > > multiplayer_ai_config
Non-empty if –ai-config was given on the command line. Vector of pairs (side number, value). Dependent on –multiplayer.
std::ostringstream wrapper.
Definition: formatter.hpp:38
bool nodelay
True if –nodelay was given on the command line.
bool fullscreen()
Definition: general.cpp:394
boost::optional< unsigned int > translation_percent
Non-empty if –all-translations or –translations-over is given on the command line.
boost::optional< std::string > logdomains
Non-empty if –logdomains was given on the command line. Prints possible logdomains filtered by given...
bool windowed
True if –windowed was given on the command line. Starts Wesnoth in windowed mode.
std::vector< std::pair< unsigned int, std::string > > parse_to_uint_string_tuples_(const std::vector< std::string > &strings, char separator=':')
A helper function splitting vector of strings of format unsigned int:string to vector of tuples (unsi...
boost::optional< std::string > userdata_dir
Non-empty if –userdata-dir was given on the command line. Sets the user data dir to the specified on...
boost::optional< std::string > gzip
Non-empty if –gzip was given on the command line. Compresses a file to .gz and exits.
boost::optional< std::string > preprocess_output_macros
Non-empty if –preprocess-output-macros was given on the command line. Outputs all preprocessed macro...
boost::optional< std::string > test
Non-empty if –test was given on the command line. Goes directly into test mode, into a scenario...
bool multiplayer
True if –multiplayer was given on the command line. Goes directly into multiplayer mode...
Manage the empty-palette in the editor.
Definition: action.cpp:29
logger & debug()
Definition: log.cpp:96
bool clock
True if –clock was given on the command line. Enables.
bool userconfig_path
True if –userconfig-path was given on the command line. Prints path to user config directory and exi...
boost::optional< std::vector< std::pair< unsigned int, std::string > > > multiplayer_algorithm
Non-empty if –algorithm was given on the command line. Vector of pairs (side number, value). Dependent on –multiplayer.
boost::optional< std::vector< std::pair< int, std::string > > > log
Contains parsed arguments of –log-* (e.g.
boost::optional< std::string > render_image
Image path to render. First parameter after –render-image.
boost::optional< int > campaign_difficulty
Non-empty if –campaign-difficulty was given on the command line. Numerical difficulty of the campaig...
bool nomusic
True if –nomusic was given on the command line. Disables music.
boost::optional< std::string > validate_wml
Non-empty if –validate was given on the command line. Makes Wesnoth validate a WML file against a sc...
boost::optional< std::string > password
Non-empty if –password was given on the command line. Forces Wesnoth to use this network password...
boost::optional< std::string > preprocess_target
Target (output) path that was given to the –preprocess option.
bool debug
True if –debug was given on the command line. Enables debug mode.
bool debug_lua
True if –debug-lua was given in the commandline. Enables some Lua debugging mechanisms.
bool nocache
True if –nocache was given on the command line. Disables cache usage.
logger & err()
Definition: log.cpp:78
boost::optional< std::string > screenshot_map_file
Map file to make a screenshot of. First parameter given after –screenshot.
boost::optional< std::string > preprocess_path
Path to parse that was given to the –preprocess option.
void set_strict_severity(int severity)
Definition: log.cpp:161
bool new_widgets
True if –new-widgets was given on the command line. Hidden option to enable the new widget toolkit...
static map_location::DIRECTION s
boost::optional< std::string > bunzip2
Non-empty if –bunzip2 was given on the command line. Uncompresses a .bz2 file and exits...
std::string password(const std::string &server, const std::string &login)
std::string language()
Definition: general.cpp:475
bad_commandline_resolution(const std::string &resolution)
boost::optional< std::vector< std::tuple< unsigned int, std::string, std::string > > > multiplayer_parm
Non-empty if –parm was given on the command line. Vector of pairs (side number, parm name...
static void validate(boost::any &v, const std::vector< std::string > &values, two_strings *, int)
#define debug(x)
bool campaign_skip_story
True if –skip-story was given on the command line. Skips [story] and [message]s through the end of t...
std::string core_id()
Definition: general.cpp:313
boost::optional< std::string > render_image_dst
Output file to put rendered image path in. Optional second parameter after –render-image.
void parse_log_strictness(const std::string &severity)
bool data_path
True if –data-path was given on the command line. Prints path to data directory and exits...
bool screenshot
True if –screenshot was given on the command line. Starts Wesnoth in screenshot mode.
boost::optional< std::string > unit_test
Non-empty if –unit was given on the command line. Goes directly into unit test mode, into a scenario, if specified.
logger & warn()
Definition: log.cpp:84
boost::optional< unsigned int > multiplayer_repeat
Repeats specified by –multiplayer-repeat option. Repeats a multiplayer game after it is finished...
bool version
True if –version was given on the command line. Prints version and exits.
bool validate_core
True if –validate-core was given on the command line. Makes Wesnoth validate the core WML...
Standard logging facilities (interface).
boost::optional< std::string > screenshot_output_file
Output file to put screenshot in. Second parameter given after –screenshot.
bool fullscreen
True if –fullscreen was given on the command line. Starts Wesnoth in fullscreen mode.
bool validcache
True if –validcache was given on the command line. Makes Wesnoth assume the cache is valid...
bool strict_validation
True if –strict-validation was given on the command line. Makes Wesnoth trust validation errors as f...
boost::optional< std::vector< std::string > > preprocess_defines
Defines that were given to the –preprocess option.
point resolution()
Definition: general.cpp:373
boost::optional< std::string > language
Non-empty if –language was given on the command line. Sets the language for this session...
Definition: help.cpp:56
bool nosound
True if –nosound was given on the command line. Disables sound.
boost::optional< std::string > preprocess_input_macros
Non-empty if –preprocess-input-macros was given on the command line. Specifies a file that contains ...
boost::optional< std::pair< int, int > > resolution
Pair of AxB values specified after –resolution. Changes Wesnoth resolution.
boost::program_options::options_description visible_
friend std::ostream & operator<<(std::ostream &os, const commandline_options &cmdline_opts)
To be used for printing help to the commandline.
bool multiplayer_exit_at_end
True if –exit-at-and was given on the command line. Dependent on –multiplayer.
boost::optional< std::string > campaign
Non-empty if –campaign was given on the command line. ID of the campaign we want to start...
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:92
int get_severity() const
Definition: log.hpp:128
Thrown when a lexical_cast fails.
boost::optional< std::vector< std::pair< unsigned int, std::string > > > multiplayer_controller
Non-empty if –controller was given on the command line. Vector of pairs (side number, controller). Dependent on –multiplayer.
boost::optional< std::string > validate_addon
Non-empty if –validate-addon was given on the command line. Makes Wesnoth validate an addon&#39;s WML...
boost::optional< std::string > server
Non-empty if –server was given on the command line. Connects Wesnoth to specified server...
boost::optional< std::string > validate_with
Non-empty if –use-schema was given on the command line. Specifies the schema for use with –validate...