The Battle for Wesnoth  1.17.10+dev
commandline_options.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2011 - 2022
3  by Lukasz Dobrogowski <lukasz.dobrogowski@gmail.com>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #include "commandline_options.hpp"
17 
18 #include "config.hpp"
19 #include "formatter.hpp"
20 #include "lexical_cast.hpp"
21 #include "log.hpp" // for logger, set_strict_severity, etc
22 #include "serialization/string_utils.hpp" // for split
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 
32 #include <array>
33 
34 namespace po = boost::program_options;
35 
36 class two_strings : public std::pair<std::string,std::string> {};
37 
38 static void validate(boost::any& v, const std::vector<std::string>& values,
39  two_strings*, int)
40 {
41  two_strings ret_val;
42  if(values.size() != 2) {
43  throw po::validation_error(po::validation_error::invalid_option_value);
44  }
45  ret_val.first = values[0];
46  ret_val.second = values[1];
47  v = ret_val;
48 }
49 
51  : error(formatter() << "Invalid resolution \"" << resolution
52  << "\" (WIDTHxHEIGHT expected)")
53 {
54 }
55 
57  const std::string& expected_format)
58  : error(formatter() << "Invalid value set \"" << str
59  << "\" (" << expected_format << " expected)")
60 {
61 }
62 
63 
64 #ifdef _WIN32
65 #define IMPLY_WCONSOLE " Implies --wconsole."
66 #else
67 #define IMPLY_WCONSOLE
68 #endif // _WIN32
69 
70 
71 commandline_options::commandline_options(const std::vector<std::string>& args)
72  : bunzip2()
73  , bzip2()
74  , campaign()
75  , campaign_difficulty()
76  , campaign_scenario()
77  , campaign_skip_story(false)
78  , clock(false)
79  , core_id()
80  , data_path(false)
81  , data_dir()
82  , debug(false)
83  , debug_lua(false)
84  , strict_lua(false)
85  , allow_insecure(false)
86 #ifdef DEBUG_WINDOW_LAYOUT_GRAPHS
87  , debug_dot_domain()
88  , debug_dot_level()
89 #endif
90  , editor()
91  , fps(false)
92  , fullscreen(false)
93  , gunzip()
94  , gzip()
95  , help()
96  , language()
97  , log()
98  , load()
99  , logdomains()
100  , log_precise_timestamps(false)
101  , multiplayer(false)
102  , multiplayer_ai_config()
103  , multiplayer_algorithm()
104  , multiplayer_controller()
105  , multiplayer_era()
106  , multiplayer_exit_at_end()
107  , multiplayer_ignore_map_settings()
108  , multiplayer_label()
109  , multiplayer_parm()
110  , multiplayer_repeat()
111  , multiplayer_scenario()
112  , multiplayer_side()
113  , multiplayer_turns()
114  , max_fps()
115  , noaddons(false)
116  , nocache(false)
117  , nodelay(false)
118  , nogui(false)
119  , nobanner(false)
120  , nomusic(false)
121  , nosound(false)
122  , new_widgets(false)
123  , preprocess(false)
124  , preprocess_defines()
125  , preprocess_input_macros()
126  , preprocess_output_macros()
127  , preprocess_path()
128  , preprocess_target()
129  , resolution()
130  , rng_seed()
131  , server()
132  , username()
133  , password()
134  , render_image()
135  , render_image_dst()
136  , screenshot(false)
137  , screenshot_map_file()
138  , screenshot_output_file()
139  , script_file()
140  , plugin_file()
141  , script_unsafe_mode(false)
142  , strict_validation(false)
143  , test()
144  , unit_test()
145  , headless_unit_test(false)
146  , noreplaycheck(false)
147  , mptest(false)
148  , usercache_path(false)
149  , usercache_dir()
150  , userconfig_path(false)
151  , userconfig_dir()
152  , userdata_path(false)
153  , userdata_dir()
154  , validcache(false)
155  , validate_core(false)
156  , validate_addon()
157  , validate_schema()
158  , validate_wml()
159  , validate_with()
160  , do_diff()
161  , do_patch()
162  , diff_left()
163  , diff_right()
164  , version(false)
165  , simple_version(false)
166  , report(false)
167  , windowed(false)
168  , with_replay(false)
169  , translation_percent()
170  , args_(args.begin() + 1, args.end())
171  , args0_(*args.begin())
172  , all_()
173  , visible_()
174  , hidden_()
175 {
176  // When adding items don't forget to update doc/man/wesnoth.6
177  // Options are sorted alphabetically by --long-option.
178  po::options_description general_opts("General options");
179  general_opts.add_options()
180  ("all-translations", "Show all translations, even incomplete ones.")
181  ("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.")
182  ("bzip2", po::value<std::string>(), "compresses a file (<arg>) in bzip2 format, stores it as <arg>.bz2 and removes <arg>.")
183  ("clock", "Adds the option to show a clock for testing the drawing timer.")
184  ("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.")
185  ("config-path", "prints the path of the userdata directory and exits. DEPRECATED: use userdata-path instead.")
186  ("core", po::value<std::string>(), "overrides the loaded core with the one whose id is specified.")
187  ("data-dir", po::value<std::string>(), "overrides the data directory with the one specified.")
188  ("data-path", "prints the path of the data directory and exits." IMPLY_WCONSOLE)
189  ("debug,d", "enables additional command mode options in-game.")
190  ("debug-lua", "enables some Lua debugging mechanisms")
191  ("strict-lua", "disallow deprecated Lua API calls")
192  ("allow-insecure", "Allows sending a plaintext password over an unencrypted connection. Should only ever be used for local testing.")
193 #ifdef DEBUG_WINDOW_LAYOUT_GRAPHS
194  ("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).")
195  ("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.")
196 #endif
197  ("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>.")
198  ("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.")
199  ("gzip", po::value<std::string>(), "compresses a file (<arg>) in gzip format, stores it as <arg>.gz and removes <arg>.")
200  ("help,h", "prints this message and exits." IMPLY_WCONSOLE)
201  ("language,L", po::value<std::string>(), "uses language <arg> (symbol) this session. Example: --language ang_GB@latin")
202  ("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.")
203  ("noaddons", "disables the loading of all add-ons.")
204  ("nocache", "disables caching of game data.")
205  ("nodelay", "runs the game without any delays.")
206  ("nomusic", "runs the game without music.")
207  ("nosound", "runs the game without sounds and music.")
208  ("password", po::value<std::string>(), "uses <password> when connecting to a server, ignoring other preferences.")
209  ("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.")
210  ("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." IMPLY_WCONSOLE)
211  ("report,R", "initializes game directories, prints build information suitable for use in bug reports, and exits." IMPLY_WCONSOLE)
212  ("rng-seed", po::value<unsigned int>(), "seeds the random number generator with number <arg>. Example: --rng-seed 0")
213  ("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." IMPLY_WCONSOLE)
214  ("script", po::value<std::string>(), "(experimental) file containing a Lua script to control the client")
215  ("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.")
216  ("strict-validation", "makes validation errors fatal")
217  ("translations-over", po::value<unsigned int>(), "Specify the standard for determining whether a translation is complete.")
218  ("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.")
219  ("usercache-dir", po::value<std::string>(), "sets the path of the cache 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/.cache/wesnoth on X11 and to the userdata-dir on other systems.")
220  ("usercache-path", "prints the path of the cache directory and exits.")
221  ("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.")
222  ("userconfig-path", "prints the path of the user config directory and exits.")
223  ("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.")
224  ("userdata-path", "prints the path of the userdata directory and exits." IMPLY_WCONSOLE)
225  ("username", po::value<std::string>(), "uses <username> when connecting to a server, ignoring other preferences.")
226  ("validcache", "assumes that the cache is valid. (dangerous)")
227  ("version,v", "prints the game's version number and exits.")
228  ("simple-version", "prints the game's version number and nothing else.")
229  ("with-replay", "replays the file loaded with the --load option.")
230 #ifdef _WIN32
231  ("wconsole", "attaches a console window on startup (Windows only). Implied by any option that prints something and exits.")
232  ("wnoconsole", "don't attach a console window on startup (Windows only). Overrides options that imply --wconsole.")
233  ("wnoredirect", "disables standard redirection of logging to a file (Windows only), allowing the output to be piped to another command")
234 #endif // _WIN32
235  ;
236 
237  po::options_description campaign_opts("Campaign options");
238  campaign_opts.add_options()
239  ("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.")
240  ("campaign-difficulty", po::value<int>(), "The difficulty of the specified campaign (1 to max). If none specified, the campaign difficulty selection widget will appear.")
241  ("campaign-scenario", po::value<std::string>(),"The id of the scenario from the specified campaign. The default is the first scenario.")
242  ("campaign-skip-story", "Skip [story] tags of the specified campaign.")
243  ;
244 
245  po::options_description display_opts("Display options");
246  display_opts.add_options()
247  ("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.")
248  ("fullscreen,f", "runs the game in full screen mode.")
249  ("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.")
250  ("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.")
251  ("resolution,r", po::value<std::string>(), "sets the screen resolution. <arg> should have format XxY. Example: --resolution 800x600")
252  ("windowed,w", "runs the game in windowed mode.")
253  ;
254 
255  po::options_description logging_opts("Logging options");
256  logging_opts.add_options()
257  ("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." IMPLY_WCONSOLE)
258  ("log-error", po::value<std::string>(), "sets the severity level of the specified log domain(s) to 'error'. <arg> should be given as a comma-separated list of domains, wildcards are allowed. Example: --log-error=network,gui/*,engine/enemies")
259  ("log-warning", po::value<std::string>(), "sets the severity level of the specified log domain(s) to 'warning'. Similar to --log-error.")
260  ("log-info", po::value<std::string>(), "sets the severity level of the specified log domain(s) to 'info'. Similar to --log-error.")
261  ("log-debug", po::value<std::string>(), "sets the severity level of the specified log domain(s) to 'debug'. Similar to --log-error.")
262  ("log-none", po::value<std::string>(), "sets the severity level of the specified log domain(s) to 'none'. Similar to --log-error.")
263  ("log-precise", "shows the timestamps in log output with more precision.")
264  ("log-to-file", "log output is written to a file rather than to standard error.")
265  ;
266 
267  po::options_description multiplayer_opts("Multiplayer options");
268  multiplayer_opts.add_options()
269  ("multiplayer,m", "Starts a multiplayer game. There are additional options that can be used as explained below:")
270  ("ai-config", po::value<std::vector<std::string>>()->composing(), "selects a configuration file to load for this side. <arg> should have format side:value")
271  ("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")
272  ("controller", po::value<std::vector<std::string>>()->composing(), "selects the controller for this side. <arg> should have format side:value")
273  ("era", po::value<std::string>(), "selects the era to be played in by its id.")
274  ("exit-at-end", "exit Wesnoth at the end of the scenario.")
275  ("ignore-map-settings", "do not use map settings.")
276  ("label", po::value<std::string>(), "sets the label for AIs.") // TODO: is the description precise? this option was undocumented before.
277  ("multiplayer-repeat", po::value<unsigned int>(), "repeats a multiplayer game after it is finished <arg> times.")
278  ("nogui", "runs the game without the GUI.")
279  ("parm", po::value<std::vector<std::string>>()->composing(), "sets additional parameters for this side. <arg> should have format side:name:value.")
280  ("scenario", po::value<std::string>(), "selects a multiplayer scenario. The default scenario is \"multiplayer_The_Freelands\".")
281  ("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.")
282  ("turns", po::value<std::string>(), "sets the number of turns. By default no turn limit is set.")
283  ;
284 
285  po::options_description testing_opts("Testing options");
286  testing_opts.add_options()
287  ("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.")
288  ("unit,u", po::value<std::vector<std::string>>(), "runs a unit test scenario. The GUI is not shown and the exit code of the program reflects the victory / defeat conditions of the scenario.\n\t0 - PASS\n\t1 - FAIL\n\t3 - FAIL (INVALID REPLAY)\n\t4 - FAIL (ERRORED REPLAY)\n\t5 - FAIL (BROKE STRICT)\n\t6 - FAIL (WML EXCEPTION)\n\tMultiple tests can be run by giving this option multiple times, in this case the test run will stop immediately after any test which doesn't PASS and the return code will be the status of the test that caused the stop.")
289  ("showgui", "don't run headlessly (for debugging a failing test)")
290  ("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.")
291  ("nobanner", "suppress startup banner.")
292  ("noreplaycheck", "don't try to validate replay of unit test.")
293  ("mp-test", "load the test mp scenarios.")
294  ;
295 
296  po::options_description parsing_opts("WML parsing options");
297  parsing_opts.add_options()
298  ("use-schema,S", po::value<std::string>(), "specify a schema to validate WML against (defaults to the core schema)")
299  ("validate,V", po::value<std::string>(), "validate a specified WML file against a schema" IMPLY_WCONSOLE)
300  ("validate-addon", po::value<std::string>(), "validate the specified addon's WML against the schema. Requires the user to play the campaign (in the GUI) to trigger the validation.")
301  ("validate-core", "validate the core WML against the schema")
302  ("validate-schema", po::value<std::string>(), "validate a specified WML schema" IMPLY_WCONSOLE)
303  ("diff,D", po::value<two_strings>()->multitoken(), "diff two preprocessed WML documents" IMPLY_WCONSOLE)
304  ("output,o", po::value<std::string>(), "output to specified file")
305  ("patch,P", po::value<two_strings>()->multitoken(), "apply a patch to a preprocessed WML document" IMPLY_WCONSOLE)
306  ("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." IMPLY_WCONSOLE)
307  ("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")
308  ("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.")
309  ("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.")
310  ;
311 
312  //hidden_.add_options()
313  // ("example-hidden-option", "")
314  // ;
315  visible_.add(general_opts).add(campaign_opts).add(display_opts).add(logging_opts).add(multiplayer_opts).add(testing_opts).add(parsing_opts);
316 
317  all_.add(visible_).add(hidden_);
318 
319  po::positional_options_description positional;
320  positional.add("data-dir",1);
321 
322  po::variables_map vm;
323  const int parsing_style = po::command_line_style::default_style ^ po::command_line_style::allow_guessing;
324  po::store(po::command_line_parser(args_).options(all_).positional(positional).style(parsing_style).run(),vm);
325 
326  if(vm.count("ai-config"))
327  multiplayer_ai_config = parse_to_uint_string_tuples_(vm["ai-config"].as<std::vector<std::string>>());
328  if(vm.count("algorithm"))
329  multiplayer_algorithm = parse_to_uint_string_tuples_(vm["algorithm"].as<std::vector<std::string>>());
330  if(vm.count("bunzip2"))
331  bunzip2 = vm["bunzip2"].as<std::string>();
332  if(vm.count("bzip2"))
333  bzip2 = vm["bzip2"].as<std::string>();
334  if(vm.count("campaign"))
335  campaign = vm["campaign"].as<std::string>();
336  if(vm.count("campaign-difficulty"))
337  campaign_difficulty = vm["campaign-difficulty"].as<int>();
338  if(vm.count("campaign-scenario"))
339  campaign_scenario = vm["campaign-scenario"].as<std::string>();
340  if(vm.count("campaign-skip-story"))
341  campaign_skip_story = true;
342  if(vm.count("clock"))
343  clock = true;
344  if(vm.count("core"))
345  core_id = vm["core"].as<std::string>();
346  if(vm.count("config-dir"))
347  userdata_dir = vm["config-dir"].as<std::string>(); //TODO: complain and remove
348  if(vm.count("config-path"))
349  userdata_path = true; //TODO: complain and remove
350  if(vm.count("controller"))
351  multiplayer_controller = parse_to_uint_string_tuples_(vm["controller"].as<std::vector<std::string>>());
352  if(vm.count("data-dir"))
353  data_dir = vm["data-dir"].as<std::string>();
354  if(vm.count("data-path"))
355  data_path = true;
356  if(vm.count("debug"))
357  debug = true;
358  if(vm.count("debug-lua"))
359  debug_lua = true;
360  if(vm.count("strict-lua"))
361  strict_lua = true;
362  if(vm.count("allow-insecure"))
363  allow_insecure = true;
364 #ifdef DEBUG_WINDOW_LAYOUT_GRAPHS
365  if(vm.count("debug-dot-domain")) {
366  debug_dot_domain = vm["debug-dot-domain"].as<std::string>();
367  }
368  if(vm.count("debug-dot-level")) {
369  debug_dot_level = vm["debug-dot-level"].as<std::string>();
370  }
371 #endif
372  if(vm.count("editor"))
373  editor = vm["editor"].as<std::string>();
374  if(vm.count("era"))
375  multiplayer_era = vm["era"].as<std::string>();
376  if(vm.count("exit-at-end"))
378  if(vm.count("fps"))
379  fps = true;
380  if(vm.count("fullscreen"))
381  fullscreen = true;
382  if(vm.count("gunzip"))
383  gunzip = vm["gunzip"].as<std::string>();
384  if(vm.count("gzip"))
385  gzip = vm["gzip"].as<std::string>();
386  if(vm.count("help"))
387  help = true;
388  if(vm.count("ignore-map-settings"))
390  if(vm.count("label"))
391  multiplayer_label = vm["label"].as<std::string>();
392  if(vm.count("language"))
393  language = vm["language"].as<std::string>();
394  if(vm.count("load"))
395  load = vm["load"].as<std::string>();
396  if(vm.count("log-error"))
397  parse_log_domains_(vm["log-error"].as<std::string>(),lg::err().get_severity());
398  if(vm.count("log-warning"))
399  parse_log_domains_(vm["log-warning"].as<std::string>(),lg::warn().get_severity());
400  if(vm.count("log-info"))
401  parse_log_domains_(vm["log-info"].as<std::string>(),lg::info().get_severity());
402  if(vm.count("log-debug"))
403  parse_log_domains_(vm["log-debug"].as<std::string>(),lg::debug().get_severity());
404  if(vm.count("log-none"))
405  parse_log_domains_(vm["log-none"].as<std::string>(),-1);
406  if(vm.count("logdomains"))
407  logdomains = vm["logdomains"].as<std::string>();
408  if(vm.count("log-precise"))
409  log_precise_timestamps = true;
410  if(vm.count("log-strict"))
411  parse_log_strictness(vm["log-strict"].as<std::string>());
412  if(vm.count("max-fps"))
413  max_fps = vm["max-fps"].as<int>();
414  if(vm.count("mp-test"))
415  mptest = true;
416  if(vm.count("multiplayer"))
417  multiplayer = true;
418  if(vm.count("multiplayer-repeat"))
419  multiplayer_repeat = vm["multiplayer-repeat"].as<unsigned int>();
420  if(vm.count("new-widgets"))
421  new_widgets = true;
422  if(vm.count("noaddons"))
423  noaddons = true;
424  if(vm.count("nocache"))
425  nocache = true;
426  if(vm.count("nodelay"))
427  nodelay = true;
428  if(vm.count("nomusic"))
429  nomusic = true;
430  if(vm.count("noreplaycheck"))
431  noreplaycheck = true;
432  if(vm.count("nosound"))
433  nosound = true;
434  if(vm.count("nogui"))
435  nogui = true;
436  if(vm.count("nobanner"))
437  nobanner = true;
438  if(vm.count("parm"))
439  multiplayer_parm = parse_to_uint_string_string_tuples_(vm["parm"].as<std::vector<std::string>>());
440  if(vm.count("preprocess"))
441  {
442  preprocess = true;
443  preprocess_path = vm["preprocess"].as<two_strings>().first;
444  preprocess_target = vm["preprocess"].as<two_strings>().second;
445  }
446  if(vm.count("diff"))
447  {
448  do_diff = true;
449  diff_left = vm["diff"].as<two_strings>().first;
450  diff_right = vm["diff"].as<two_strings>().second;
451  }
452  if(vm.count("patch"))
453  {
454  do_patch = true;
455  diff_left = vm["patch"].as<two_strings>().first;
456  diff_right = vm["patch"].as<two_strings>().second;
457  }
458  if(vm.count("output"))
459  {
460  output_file = vm["output"].as<std::string>();
461  }
462  if(vm.count("preprocess-defines"))
463  preprocess_defines = utils::split(vm["preprocess-defines"].as<std::string>(), ',');
464  if(vm.count("preprocess-input-macros"))
465  preprocess_input_macros = vm["preprocess-input-macros"].as<std::string>();
466  if(vm.count("preprocess-output-macros"))
467  preprocess_output_macros = vm["preprocess-output-macros"].as<std::string>();
468  if(vm.count("resolution"))
469  parse_resolution_(vm["resolution"].as<std::string>());
470  if(vm.count("rng-seed"))
471  rng_seed = vm["rng-seed"].as<unsigned int>();
472  if(vm.count("scenario"))
473  multiplayer_scenario = vm["scenario"].as<std::string>();
474  if(vm.count("render-image"))
475  {
476  render_image = vm["render-image"].as<two_strings>().first;
477  render_image_dst = vm["render-image"].as<two_strings>().second;
478  }
479  if(vm.count("screenshot"))
480  {
481  screenshot = true;
482  screenshot_map_file = vm["screenshot"].as<two_strings>().first;
483  screenshot_output_file = vm["screenshot"].as<two_strings>().second;
484  }
485  if(vm.count("script"))
486  script_file = vm["script"].as<std::string>();
487  if(vm.count("unsafe-scripts"))
488  script_unsafe_mode = true;
489  if(vm.count("plugin"))
490  plugin_file = vm["plugin"].as<std::string>();
491  if(vm.count("server"))
492  server = vm["server"].as<std::string>();
493  if(vm.count("username"))
494  username = vm["username"].as<std::string>();
495  if(vm.count("password"))
496  password = vm["password"].as<std::string>();
497  if(vm.count("report"))
498  report = true;
499  if(vm.count("side"))
500  multiplayer_side = parse_to_uint_string_tuples_(vm["side"].as<std::vector<std::string>>());
501  if(vm.count("test"))
502  test = vm["test"].as<std::string>();
503  if(vm.count("unit"))
504  {
505  unit_test = vm["unit"].as<std::vector<std::string>>();
506  headless_unit_test = true;
507  }
508  if(vm.count("showgui"))
509  headless_unit_test = false;
510  if(vm.count("noreplaycheck"))
511  noreplaycheck = true;
512  if(vm.count("turns"))
513  multiplayer_turns = vm["turns"].as<std::string>();
514  if(vm.count("strict-validation"))
515  strict_validation = true;
516  if(vm.count("usercache-dir"))
517  usercache_dir = vm["usercache-dir"].as<std::string>();
518  if(vm.count("usercache-path"))
519  usercache_path = true;
520  if(vm.count("userconfig-dir"))
521  userconfig_dir = vm["userconfig-dir"].as<std::string>();
522  if(vm.count("userconfig-path"))
523  userconfig_path = true;
524  if(vm.count("userdata-dir"))
525  userdata_dir = vm["userdata-dir"].as<std::string>();
526  if(vm.count("userdata-path"))
527  userdata_path = true;
528  if(vm.count("validcache"))
529  validcache = true;
530  // If you add a new validate-* option, remember the any_validation_option() function
531  if(vm.count("validate"))
532  validate_wml = vm["validate"].as<std::string>();
533  if(vm.count("validate-core"))
534  validate_core = true;
535  if(vm.count("validate-addon"))
536  validate_addon = vm["validate-addon"].as<std::string>();
537  if(vm.count("validate-schema"))
538  validate_schema = vm["validate-schema"].as<std::string>();
539  // If you add a new validate-* option, remember the any_validation_option() function
540  if(vm.count("use-schema"))
541  validate_with = vm["use-schema"].as<std::string>();
542  if(vm.count("version"))
543  version = true;
544  if(vm.count("simple-version"))
545  {
546  simple_version = true;
547  nobanner = true;
548  }
549  if(vm.count("windowed"))
550  windowed = true;
551  if(vm.count("with-replay"))
552  with_replay = true;
553  if(vm.count("all-translations"))
555  else if(vm.count("translations-over"))
556  translation_percent = std::clamp<unsigned int>(vm["translations-over"].as<unsigned int>(), 0, 100);
557 }
558 
559 void commandline_options::parse_log_domains_(const std::string &domains_string, const int severity)
560 {
561  if(std::vector<std::string> domains = utils::split(domains_string, ','); !domains.empty()) {
562  if(!log) {
563  log.emplace();
564  }
565  for(auto&& domain : domains) {
566  log->emplace_back(severity, std::move(domain));
567  }
568  }
569 }
570 
572  static const std::array<const lg::logger*, 4> loggers {{&lg::err(), &lg::warn(), &lg::info(), &lg::debug()}};
573  for(const lg::logger * l : loggers ) {
574  if(severity == l->get_name()) {
576  return;
577  }
578  }
579  PLAIN_LOG << "Unrecognized argument to --log-strict : " << severity << " . \nDisabling strict mode logging.";
581 }
582 
583 void commandline_options::parse_resolution_ (const std::string& resolution_string)
584 {
585  const std::vector<std::string> tokens = utils::split(resolution_string, 'x');
586  if(tokens.size() != 2) {
587  throw bad_commandline_resolution(resolution_string);
588  }
589 
590  int xres, yres;
591 
592  try {
593  xres = std::stoi(tokens[0]);
594  yres = std::stoi(tokens[1]);
595  } catch(const std::invalid_argument &) {
596  throw bad_commandline_resolution(resolution_string);
597  }
598 
599  resolution = std::pair(xres, yres);
600 }
601 
602 std::vector<std::pair<unsigned int,std::string>> commandline_options::parse_to_uint_string_tuples_(const std::vector<std::string> &strings, char separator)
603 {
604  std::vector<std::pair<unsigned int,std::string>> vec;
605 
606  using namespace std::literals;
607  const std::string expected_format
608  = "UINT"s + separator + "STRING";
609 
610  for(const std::string &s : strings) {
611  std::vector<std::string> tokens = utils::split(s, separator);
612  if(tokens.size() != 2) {
613  throw bad_commandline_tuple(s, expected_format);
614  }
615 
616  unsigned int temp;
617  try {
618  temp = lexical_cast<unsigned int>(tokens[0]);
619  } catch (const bad_lexical_cast &) {
620  throw bad_commandline_tuple(s, expected_format);
621  }
622 
623  vec.emplace_back(temp, std::move(tokens[1]));
624  }
625  return vec;
626 }
627 
628 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)
629 {
630  std::vector<std::tuple<unsigned int,std::string,std::string>> vec;
631 
632  using namespace std::literals;
633  const std::string expected_format
634  = "UINT"s + separator + "STRING" + separator + "STRING";
635 
636  for(const std::string &s : strings) {
637  const std::vector<std::string> tokens = utils::split(s, separator);
638  if(tokens.size() != 3) {
639  throw bad_commandline_tuple(s, expected_format);
640  }
641 
642  unsigned int temp;
643  try {
644  temp = lexical_cast<unsigned int>(tokens[0]);
645  } catch (const bad_lexical_cast &) {
646  throw bad_commandline_tuple(s, expected_format);
647  }
648 
649  vec.emplace_back(temp, tokens[1], tokens[2]);
650  }
651  return vec;
652 }
653 
654 std::ostream& operator<<(std::ostream &os, const commandline_options& cmdline_opts)
655 {
656  os << "Usage: " << cmdline_opts.args0_ << " [<options>] [<data-directory>]\n";
657  os << cmdline_opts.visible_;
658  return os;
659 }
660 
662  config ret;
663  if(server) {
664  ret["server"] = *server;
665  }
666  if(username) {
667  ret["username"] = *username;
668  }
669  if(password) {
670  ret["password"] = *password;
671  }
672  return ret;
673 }
674 
676 {
678 }
bool log_precise_timestamps
True if –log-precise was given on the command line.
bool new_widgets
Do we wish to use the new library or not.
Definition: settings.cpp:23
std::vector< std::string > args_
std::optional< std::string > preprocess_path
Path to parse that was given to the –preprocess option.
std::optional< std::string > output_file
Output filename for WML diff or preprocessing.
std::optional< std::string > core_id
Non-empty if –core was given on the command line.
#define PLAIN_LOG
Definition: log.hpp:258
bool any_validation_option() const
True if the –validate or any of the –validate-* options are given.
std::optional< std::string > preprocess_output_macros
Non-empty if –preprocess-output-macros was given on the command line.
bool mptest
True if –mp-test was given on the command line.
static domain_map * domains
Definition: log.cpp:165
boost::program_options::options_description all_
bool nogui
True if –nogui was given on the command line.
bool script_unsafe_mode
Whether to load the "package" package for the scripting environment.
std::optional< std::string > test
Non-empty if –test was given on the command line.
std::optional< std::vector< std::pair< unsigned int, std::string > > > multiplayer_ai_config
Non-empty if –ai-config was given on the command line.
std::vector< game_tip > load(const config &cfg)
Loads the tips from a config.
Definition: tips.cpp:36
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...
std::optional< std::string > password
Non-empty if –password was given on the command line.
boost::program_options::options_description hidden_
bool noaddons
True if –noaddons was given on the command line.
std::optional< unsigned int > translation_percent
Non-empty if –all-translations or –translations-over is given on the command line.
New lexcical_cast header.
logger & info()
Definition: log.cpp:182
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.
bool multiplayer_ignore_map_settings
True if –ignore-map-settings was given at the command line.
std::optional< std::string > preprocess_target
Target (output) path that was given to the –preprocess option.
std::optional< std::string > preprocess_input_macros
Non-empty if –preprocess-input-macros was given on the command line.
std::optional< std::string > multiplayer_label
Non-empty if –label was given on the command line.
std::optional< std::vector< std::tuple< unsigned int, std::string, std::string > > > multiplayer_parm
Non-empty if –parm was given on the command line.
std::optional< std::string > userconfig_dir
Non-empty if –userconfig-dir was given on the command line.
bool fps
True if –fps was given on the command line.
std::vector< std::string > unit_test
Non-empty if –unit was given on the command line.
bool noreplaycheck
True if –noreplaycheck was given on the command line.
std::optional< std::string > load
Non-empty if –load was given on the command line.
std::optional< int > max_fps
Max FPS specified by –max-fps option.
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::string diff_left
Files for diffing or patching.
#define IMPLY_WCONSOLE
bool preprocess
True if –preprocess was given on the command line.
bool with_replay
True if –with-replay was given on the command line.
bool headless_unit_test
True if –unit is used and –showgui is not present.
const config & options()
Definition: game.cpp:556
std::optional< std::string > render_image_dst
Output file to put rendered image path in.
bool report
True if –report was given on the command line.
std::optional< std::string > gzip
Non-empty if –gzip was given on the command line.
std::ostringstream wrapper.
Definition: formatter.hpp:39
bool simple_version
True if –simple-version was given on the command line.
bool nodelay
True if –nodelay was given on the command line.
std::optional< unsigned int > rng_seed
RNG seed specified by –rng-seed option.
std::optional< std::vector< std::pair< int, std::string > > > log
Contains parsed arguments of –log-* (e.g.
std::optional< std::string > validate_addon
Non-empty if –validate-addon was given on the command line.
bool fullscreen()
Definition: general.cpp:437
std::optional< int > campaign_difficulty
Non-empty if –campaign-difficulty was given on the command line.
bool windowed
True if –windowed was given on the command line.
std::optional< std::string > bunzip2
Non-empty if –bunzip2 was given on the command line.
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...
bool multiplayer
True if –multiplayer was given on the command line.
std::optional< std::vector< std::pair< unsigned int, std::string > > > multiplayer_algorithm
Non-empty if –algorithm was given on the command line.
Manage the empty-palette in the editor.
Definition: action.cpp:30
logger & debug()
Definition: log.cpp:188
bool clock
True if –clock was given on the command line.
bool userconfig_path
True if –userconfig-path was given on the command line.
severity
Definition: log.hpp:73
std::optional< std::string > bzip2
Non-empty if –bzip2 was given on the command line.
bool allow_insecure
Definition: game_config.cpp:96
bool nobanner
True if –nobanner was given on the command line.
std::optional< std::string > logdomains
Non-empty if –logdomains was given on the command line.
std::optional< std::string > render_image
Image path to render.
bool nomusic
True if –nomusic was given on the command line.
std::optional< std::string > multiplayer_era
Non-empty if –era was given on the command line.
std::optional< std::string > campaign
Non-empty if –campaign was given on the command line.
bool debug
True if –debug was given on the command line.
bool debug_lua
True if –debug-lua was given in the commandline.
bool nocache
True if –nocache was given on the command line.
logger & err()
Definition: log.cpp:170
bool allow_insecure
True if –allow-insecure was given in the commandline.
void set_strict_severity(int severity)
Definition: log.cpp:253
bool usercache_path
True if –usercache-path was given on the command line.
bool strict_lua
True if –strict-lua was given in the commandline.
bool new_widgets
True if –new-widgets was given on the command line.
std::optional< std::string > screenshot_output_file
Output file to put screenshot in.
std::optional< std::string > gunzip
Non-empty if –gunzip was given on the command line.
std::optional< std::vector< std::pair< unsigned int, std::string > > > multiplayer_side
Non-empty if –side was given on the command line.
static map_location::DIRECTION s
std::string password(const std::string &server, const std::string &login)
std::optional< std::string > userdata_dir
Non-empty if –userdata-dir was given on the command line.
std::string language()
Definition: general.cpp:538
bad_commandline_resolution(const std::string &resolution)
static void validate(boost::any &v, const std::vector< std::string > &values, two_strings *, int)
std::optional< std::string > script_file
File to load lua script from.
#define debug(x)
std::optional< std::string > username
Non-empty if –username was given on the command line.
std::optional< std::string > data_dir
Non-empty if –data-dir was given on the command line.
std::optional< std::string > multiplayer_scenario
Non-empty if –scenario was given on the command line.
bool campaign_skip_story
True if –skip-story was given on the command line.
std::string core_id()
Definition: general.cpp:325
void parse_log_strictness(const std::string &severity)
std::optional< std::string > server
Non-empty if –server was given on the command line.
bool data_path
True if –data-path was given on the command line.
bool screenshot
True if –screenshot was given on the command line.
logger & warn()
Definition: log.cpp:176
std::vector< std::string > split(const config_attribute_value &val)
bool version
True if –version was given on the command line.
bool validate_core
True if –validate-core was given on the command line.
std::optional< std::string > screenshot_map_file
Map file to make a screenshot of.
Standard logging facilities (interface).
std::optional< std::string > validate_with
Non-empty if –use-schema was given on the command line.
std::optional< std::string > validate_wml
Non-empty if –validate was given on the command line.
bool fullscreen
True if –fullscreen was given on the command line.
bool validcache
True if –validcache was given on the command line.
bool strict_validation
True if –strict-validation was given on the command line.
std::optional< std::string > multiplayer_turns
Non-empty if –turns was given on the command line.
point resolution()
Definition: general.cpp:395
std::optional< std::vector< std::string > > preprocess_defines
Defines that were given to the –preprocess option.
Definition: help.cpp:57
std::optional< unsigned int > multiplayer_repeat
Repeats specified by –multiplayer-repeat option.
bool nosound
True if –nosound was given on the command line.
std::optional< std::vector< std::pair< unsigned int, std::string > > > multiplayer_controller
Non-empty if –controller was given on the command line.
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.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:60
std::optional< std::string > campaign_scenario
Non-empty if –campaign-scenario was given on the command line.
std::optional< std::string > validate_schema
Non-empty if –validate-schema was given on the command line.
int get_severity() const
Returns following values depending on the logger: error: 0 warn: 1 info: 2 debug: 3 See also the lg::...
Definition: log.hpp:178
std::optional< std::pair< int, int > > resolution
Pair of AxB values specified after –resolution.
Thrown when a lexical_cast fails.
std::optional< std::string > plugin_file
File to load a lua plugin (similar to a script) from.
std::optional< std::string > usercache_dir
Non-empty if –usercache-dir was given on the command line.
std::optional< std::string > language
Non-empty if –language was given on the command line.