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