The Battle for Wesnoth  1.17.23+dev
commandline_options.hpp
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 #pragma once
17 
18 #include <optional>
19 
20 #include <boost/program_options/options_description.hpp>
21 #include <iosfwd> // for ostream
22 #include <string> // for string
23 #include <tuple>
24 #include <vector> // for vector
25 
26 class bad_commandline_resolution : public boost::program_options::error
27 {
28 public:
29  bad_commandline_resolution(const std::string& resolution);
30 };
31 
32 class bad_commandline_tuple : public boost::program_options::error
33 {
34 public:
35  bad_commandline_tuple(const std::string& str,
36  const std::string& expected_format);
37 };
38 
39 class config;
40 
42 {
43  /** To be used for printing help to the commandline. */
44  friend std::ostream& operator<<(std::ostream &os, const commandline_options& cmdline_opts);
45 
46 public:
47  commandline_options(const std::vector<std::string>& args);
48 
49  config to_config() const; /* Used by lua scrips. Not all of the options need to be exposed here, just those exposed to lua */
50 
51  /** True if the --validate or any of the --validate-* options are given. */
52  bool any_validation_option() const;
53 
54  /** Non-empty if --bunzip2 was given on the command line. Uncompresses a .bz2 file and exits. */
55  std::optional<std::string> bunzip2;
56  /** Non-empty if --bzip2 was given on the command line. Compresses a file to .bz2 and exits. */
57  std::optional<std::string> bzip2;
58  /** Non-empty if --campaign was given on the command line. ID of the campaign we want to start. */
59  std::optional<std::string> campaign;
60  /** Non-empty if --campaign-difficulty was given on the command line. Numerical difficulty of the campaign to be played. Dependent on --campaign. */
61  std::optional<int> campaign_difficulty;
62  /** Non-empty if --campaign-scenario was given on the command line. Chooses starting scenario in the campaign to be played. Dependent on --campaign. */
63  std::optional<std::string> campaign_scenario;
64  /** True if --skip-story was given on the command line. Skips [story] and [message]s through the end of the "start" event. Dependent on --campaign. */
66  /** True if --clock was given on the command line. Enables */
67  bool clock;
68  /** Non-empty if --core was given on the command line. Chooses the core to be loaded. */
69  std::optional<std::string> core_id;
70  /** True if --data-path was given on the command line. Prints path to data directory and exits. */
71  bool data_path;
72  /** Non-empty if --data-dir was given on the command line. Sets the config dir to the specified one. */
73  std::optional<std::string> data_dir;
74  /** True if --debug was given on the command line. Enables debug mode. */
75  bool debug;
76  /** True if --debug-lua was given in the commandline. Enables some Lua debugging mechanisms. */
77  bool debug_lua;
78  /** True if --strict-lua was given in the commandline. Disallows use of deprecated APIs. */
79  bool strict_lua;
80  /**
81  * True if --allow-insecure was given in the commandline.
82  * Allows sending a plaintext password over an unencrypted connection.
83  * Should only ever be used for local testing.
84  */
86 #ifdef DEBUG_WINDOW_LAYOUT_GRAPHS
87  /** Non-empty if --debug-dot-domain was given on the command line. */
88  std::optional<std::string> debug_dot_domain;
89  /** Non-empty if --debug-dot-level was given on the command line. */
90  std::optional<std::string> debug_dot_level;
91 #endif
92  /** Non-empty if --editor was given on the command line. Goes directly into editor. If string is longer than 0, it contains path to the file to edit. */
93  std::optional<std::string> editor;
94  /** True if --fps was given on the command line. Shows number of fps. */
95  bool fps;
96  /** True if --fullscreen was given on the command line. Starts Wesnoth in fullscreen mode. */
97  bool fullscreen;
98  /** Non-empty if --gunzip was given on the command line. Uncompresses a .gz file and exits. */
99  std::optional<std::string> gunzip;
100  /** Non-empty if --gzip was given on the command line. Compresses a file to .gz and exits. */
101  std::optional<std::string> gzip;
102  /** True if --help was given on the command line. Prints help and exits. */
103  bool help;
104  /** Non-empty if --language was given on the command line. Sets the language for this session. */
105  std::optional<std::string> language;
106  /**
107  * Contains parsed arguments of --log-* (e.g. --log-debug).
108  * Vector of pairs (severity, log domain).
109  */
110  std::optional<std::vector<std::pair<int, std::string>>> log;
111  /** Non-empty if --log-strict was given */
112  std::optional<int> log_strict_level;
113  /** Non-empty if --load was given on the command line. Savegame specified to load after start. */
114  std::optional<std::string> load;
115  /** Non-empty if --logdomains was given on the command line. Prints possible logdomains filtered by given string and exits. */
116  std::optional<std::string> logdomains;
117  /** True if --log-precise was given on the command line. Shows timestamps in log with more precision. */
119  /** True if --multiplayer was given on the command line. Goes directly into multiplayer mode. */
121  /** Non-empty if --ai-config was given on the command line. Vector of pairs (side number, value). Dependent on --multiplayer. */
122  std::optional<std::vector<std::pair<unsigned int, std::string>>> multiplayer_ai_config;
123  /** Non-empty if --algorithm was given on the command line. Vector of pairs (side number, value). Dependent on --multiplayer. */
124  std::optional<std::vector<std::pair<unsigned int, std::string>>> multiplayer_algorithm;
125  /** Non-empty if --controller was given on the command line. Vector of pairs (side number, controller). Dependent on --multiplayer. */
126  std::optional<std::vector<std::pair<unsigned int, std::string>>> multiplayer_controller;
127  /** Non-empty if --era was given on the command line. Dependent on --multiplayer. */
128  std::optional<std::string> multiplayer_era;
129  /** True if --exit-at-and was given on the command line. Dependent on --multiplayer. */
131  /** True if --ignore-map-settings was given at the command line. Do not use map settings. */
133  /** Non-empty if --label was given on the command line. Dependent on --multiplayer. */
134  std::optional<std::string> multiplayer_label;
135  /** Non-empty if --parm was given on the command line. Vector of pairs (side number, parm name, parm value). Dependent on --multiplayer. */
136  std::optional<std::vector<std::tuple<unsigned int, std::string, std::string>>> multiplayer_parm;
137  /** Repeats specified by --multiplayer-repeat option. Repeats a multiplayer game after it is finished. Dependent on --multiplayer. */
138  std::optional<unsigned int> multiplayer_repeat;
139  /** Non-empty if --scenario was given on the command line. Dependent on --multiplayer. */
140  std::optional<std::string> multiplayer_scenario;
141  /** Non-empty if --side was given on the command line. Vector of pairs (side number, faction id). Dependent on --multiplayer. */
142  std::optional<std::vector<std::pair<unsigned int, std::string>>> multiplayer_side;
143  /** Non-empty if --turns was given on the command line. Dependent on --multiplayer. */
144  std::optional<std::string> multiplayer_turns;
145  /** Max FPS specified by --max-fps option. */
146  std::optional<int> max_fps;
147  /** True if --noaddons was given on the command line. Disables the loading of all add-ons. */
148  bool noaddons;
149  /** True if --nocache was given on the command line. Disables cache usage. */
150  bool nocache;
151  /** True if --nodelay was given on the command line. */
152  bool nodelay;
153  /** True if --nogui was given on the command line. Disables GUI. */
154  bool nogui;
155  /** True if --nobanner was given on the command line. Disables startup banner. */
156  bool nobanner;
157  /** True if --nomusic was given on the command line. Disables music. */
158  bool nomusic;
159  /** True if --nosound was given on the command line. Disables sound. */
160  bool nosound;
161  /** True if --new-widgets was given on the command line. Hidden option to enable the new widget toolkit. */
163  /** True if --preprocess was given on the command line. Starts Wesnoth in preprocessor-only mode. */
165  /** Defines that were given to the --preprocess option. */
166  std::optional<std::vector<std::string>> preprocess_defines;
167  /** Non-empty if --preprocess-input-macros was given on the command line. Specifies a file that contains [preproc_define]s to be included before preprocessing. Dependent on --preprocess. */
168  std::optional<std::string> preprocess_input_macros;
169  /** Non-empty if --preprocess-output-macros was given on the command line. Outputs all preprocessed macros to the specified file. Dependent on --preprocess. */
170  std::optional<std::string> preprocess_output_macros;
171  /** Path to parse that was given to the --preprocess option. */
172  std::optional<std::string> preprocess_path;
173  /** Target (output) path that was given to the --preprocess option. */
174  std::optional<std::string> preprocess_target;
175  /** Pair of AxB values specified after --resolution. Changes Wesnoth resolution. */
176  std::optional<std::pair<int,int>> resolution;
177  /** RNG seed specified by --rng-seed option. Initializes RNG with given seed. */
178  std::optional<unsigned int> rng_seed;
179  /** Non-empty if --server was given on the command line. Connects Wesnoth to specified server. If no server was specified afterwards, contains an empty string. */
180  std::optional<std::string> server;
181  /** Non-empty if --username was given on the command line. Forces Wesnoth to use this network username. */
182  std::optional<std::string> username;
183  /** Non-empty if --password was given on the command line. Forces Wesnoth to use this network password. */
184  std::optional<std::string> password;
185  /** Image path to render. First parameter after --render-image */
186  std::optional<std::string> render_image;
187  /** Output file to put rendered image path in. Optional second parameter after --render-image */
188  std::optional<std::string> render_image_dst;
189  /** True if --screenshot was given on the command line. Starts Wesnoth in screenshot mode. */
191  /** Map file to make a screenshot of. First parameter given after --screenshot. */
192  std::optional<std::string> screenshot_map_file;
193  /** Output file to put screenshot in. Second parameter given after --screenshot. */
194  std::optional<std::string> screenshot_output_file;
195  /** File to load lua script from. */
196  std::optional<std::string> script_file;
197  /** File to load a lua plugin (similar to a script) from. Experimental / may replace script. */
198  std::optional<std::string> plugin_file;
199  /** Whether to load the "package" package for the scripting environment. (This allows to load arbitrary lua packages, and gives untrusted lua the same permissions as wesnoth executable) */
201  /** True if --strict-validation was given on the command line. Makes Wesnoth trust validation errors as fatal WML errors and create WML exception, if so. */
203  /** Non-empty if --test was given on the command line. Goes directly into test mode, into a scenario, if specified. */
204  std::optional<std::string> test;
205  /** Non-empty if --unit was given on the command line. Goes directly into unit test mode, into a scenario, if specified. */
206  std::vector<std::string> unit_test;
207  /** True if --unit is used and --showgui is not present. */
209  /** True if --noreplaycheck was given on the command line. Dependent on --unit. */
211  /** True if --mp-test was given on the command line. */
212  bool mptest;
213  /** True if --usercache-path was given on the command line. Prints path to cache directory and exits. */
215  /** Non-empty if --usercache-dir was given on the command line. Sets the cache dir to the specified one. */
216  std::optional<std::string> usercache_dir;
217  /** True if --userconfig-path was given on the command line. Prints path to user config directory and exits. */
219  /** Non-empty if --userconfig-dir was given on the command line. Sets the user config dir to the specified one. */
220  std::optional<std::string> userconfig_dir;
221  /** True if --userdata-path was given on the command line. Prints path to user data directory and exits. */
223  /** Non-empty if --userdata-dir was given on the command line. Sets the user data dir to the specified one. */
224  std::optional<std::string> userdata_dir;
225  /** True if --validcache was given on the command line. Makes Wesnoth assume the cache is valid. */
227  /** True if --validate-core was given on the command line. Makes Wesnoth validate the core WML. */
229  /** Non-empty if --validate-addon was given on the command line. Makes Wesnoth validate an addon's WML. */
230  std::optional<std::string> validate_addon;
231  /** Non-empty if --validate-schema was given on the command line. Makes Wesnoth validate a WML schema. */
232  std::optional<std::string> validate_schema;
233  /** Non-empty if --validate was given on the command line. Makes Wesnoth validate a WML file against a schema. */
234  std::optional<std::string> validate_wml;
235  /** Non-empty if --use-schema was given on the command line. Specifies the schema for use with --validate. */
236  std::optional<std::string> validate_with;
237  /** Output filename for WML diff or preprocessing */
238  std::optional<std::string> output_file;
240  /** Files for diffing or patching */
241  std::string diff_left, diff_right;
242  /** True if --version was given on the command line. Prints version and exits. */
243  bool version;
244  /** True if --simple-version was given on the command line. Prints version and nothing else then exits. */
246  /** True if --report was given on the command line. Prints a bug report-style info dump and exits. */
247  bool report;
248  /** True if --windowed was given on the command line. Starts Wesnoth in windowed mode. */
249  bool windowed;
250  /** True if --with-replay was given on the command line. Shows replay of the loaded file. */
252  /** Non-empty if --all-translations or --translations-over is given on the command line. */
253  std::optional<unsigned int> translation_percent;
254 private:
255  void parse_log_domains_(const std::string &domains_string, const int severity);
256  void parse_log_strictness (const std::string &severity);
257  void parse_resolution_ (const std::string &resolution_string);
258  /** A helper function splitting vector of strings of format unsigned int:string to vector of tuples (unsigned int,string) */
259  std::vector<std::pair<unsigned int,std::string>> parse_to_uint_string_tuples_(const std::vector<std::string> &strings, char separator = ':');
260  /** A helper function splitting vector of strings of format unsigned int:string:string to vector of tuples (unsigned int,string,string) */
261  std::vector<std::tuple<unsigned int,std::string,std::string>> parse_to_uint_string_string_tuples_(const std::vector<std::string> &strings, char separator = ':');
262  std::vector<std::string> args_;
263  std::string args0_;
264  boost::program_options::options_description all_;
265  boost::program_options::options_description visible_;
266  boost::program_options::options_description hidden_;
267 };
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< int > log_strict_level
Non-empty if –log-strict was given.
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.
bool help
True if –help was given on the command line.
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 > editor
Non-empty if –editor 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
severity
Definition: log.hpp:76
point resolution()
Definition: general.cpp:392