Debian Codegraph @

src/Main/options.hxx

raw symbols SCIP
1 // options.hxx -- class to handle command line options
2 //
3 // Written by Curtis Olson, started April 1998.
4 //
5 // Copyright (C) 1998 Curtis L. Olson - http://www.flightgear.org/~curt
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 //
21 // $Id$
22
23
24 #ifndef _OPTIONS_HXX
25 #define _OPTIONS_HXX
26
27 #include <memory>
28 #include <string>
29
30 #include <simgear/misc/simgear_optional.hxx>
31 #include <simgear/misc/strutils.hxx>
32
33 // forward decls
34 class SGPath;
35
36 namespace flightgear
37 {
38
39 /**
40 * return the default platform dependant download directory.
41 * This must be a user-writeable location, the question is if it should
42 * be a user visible location. On Windows we default to a subdir of
43 * Documents (FlightGear), on Unixes we default to FG_HOME, which is
44 * typically invisible.
45 */
46 SGPath defaultDownloadDir();
47
48
49 /// option processing can have various result values
50 /// depending on what the user requested. Note processOptions only
51 /// returns a subset of these.
52 enum OptionResult
53 {
54 FG_OPTIONS_OK = 0,
55 FG_OPTIONS_HELP,
56 FG_OPTIONS_ERROR,
57 FG_OPTIONS_EXIT,
58 FG_OPTIONS_VERBOSE_HELP,
59 FG_OPTIONS_SHOW_AIRCRAFT,
60 FG_OPTIONS_SHOW_SOUND_DEVICES,
61 FG_OPTIONS_NO_DEFAULT_CONFIG
62 };
63
64 class Options
65 {
66 private:
67 Options();
68
69 public:
70 static Options* sharedInstance();
71
72 /**
73 * Delete the entire options object. Use with a degree of care, no code
74 * should ever be caching the Options pointer but this has not actually been
75 * checked across the whole code :)
76 */
77 static void reset();
78
79 /**
80 * @brief check if a path 'looks like' it contains FGData by checking for
81 * the presence of various standard files. Note this does not do any version
82 * checking.
83 */
84 static bool isFGData(const SGPath& p);
85
86 ~Options();
87
88 /**
89 * pass command line arguments, read default config files
90 */
91 OptionResult init(int argc, char* argv[], const SGPath& appDataPath);
92
93 /**
94 * parse a config file (eg, .fgfsrc)
95 */
96 void readConfig(const SGPath& path);
97
98 /**
99 * read the value for an option, if it has been set
100 */
101 std::string valueForOption(const std::string& key, const std::string& defValue = std::string()) const;
102
103 /**
104 * return all values for a multi-valued option
105 */
106 string_list valuesForOption(const std::string& key) const;
107
108 /**
109 * Check if a particular option has been set (so far).
110 * For boolean option please use isBoolOptionEnable or isBoolOptionDisable.
111 */
112 bool isOptionSet(const std::string& key) const;
113
114 /**
115 * Check if the user has specified a given boolean option.
116 * We need to return 3 states:
117 * * 1 - the user has explicitly enabled the option,
118 * * 0 - the user has explicitly disabled the option,
119 * * -1 - the user has not used the specified option at all.
120 *
121 * User provided options => Using the method => Result
122 * --enable-fullscreen => checkBoolOptionSet("fullscreen") => true
123 * --disable-fullscreen => checkBoolOptionSet("fullscreen") => false
124 * --fullscreen => checkBoolOptionSet("fullscreen") => true
125 * --fullscreen true => checkBoolOptionSet("fullscreen") => true
126 * --fullscreen false => checkBoolOptionSet("fullscreen") => false
127 * --fullscreen 1 => checkBoolOptionSet("fullscreen") => true
128 * --fullscreen 0 => checkBoolOptionSet("fullscreen") => false
129 * --fullscreen yes => checkBoolOptionSet("fullscreen") => true
130 * --fullscreen no => checkBoolOptionSet("fullscreen") => false
131 * {none of the above} => checkBoolOptionSet("fullscreen") => no value
132 */
133 simgear::optional<bool> checkBoolOptionSet(const std::string& key) const;
134
135 /**
136 * An overlay on checkBoolOptionSet, except that when the user has not used
137 * the option at all then false is returned.
138 * For non-boolean option please use isOptionSet.
139 */
140 bool isBoolOptionEnable(const std::string& key) const;
141
142 /**
143 * An overlay on checkBoolOptionSet, to check whether user used the option
144 * with explicitly disable it.
145 * For non-boolean option please use isOptionSet.
146 */
147 bool isBoolOptionDisable(const std::string& key) const;
148
149 /**
150 * set an option value, assuming it is not already set (or multiple values
151 * are permitted)
152 * This can be used to inject option values, eg based upon environment variables
153 */
154 int addOption(const std::string& key, const std::string& value);
155
156 /**
157 * set an option, overwriting any existing value which might be set
158 */
159 int setOption(const std::string& key, const std::string& value);
160
161 void clearOption(const std::string& key);
162
163 /**
164 * apply option values to the simulation state
165 * (set properties, etc).
166 */
167 OptionResult processOptions();
168
169 /**
170 * process command line options relating to scenery / aircraft / data paths
171 */
172 void initPaths();
173
174 /**
175 * init the aircraft options
176 */
177 OptionResult initAircraft();
178
179 /**
180 * should defualt configuration files be loaded and processed or not?
181 * There's many configuration files we have historically read by default
182 * on startup - fgfs.rc in various places and so on.
183 * --no-default-config allows this behaviour to be changed, so only
184 * expicitly listed files are read Expose
185 * the value of the option here.
186 */
187 bool shouldLoadDefaultConfig() const;
188
189 /**
190 * when using the built-in launcher, we disable the default config files.
191 * explicitly loaded confg files are still permitted.
192 */
193 void setShouldLoadDefaultConfig(bool load);
194
195 /**
196 * Check if the arguments array contains a particular string (with a '--' or
197 * '-' prefix).
198 * Used by early startup code before Options object is created.
199 * For boolean option please use checkForBoolArg or checkForArgEnable/checkForArgDisable.
200 */
201 static bool checkForArg(int argc, char* argv[], const char* arg);
202
203 /**
204 * Check if the user has specified a given boolean option.
205 * Used by early startup code before Options object is created.
206 * We need to return 3 states:
207 * * 1 - the user has explicitly enabled the option,
208 * * 0 - the user has explicitly disabled the option,
209 * * -1 - the user has not used the specified option at all.
210 *
211 * User provided options => Using the method => Result
212 * --enable-fullscreen => checkForBoolArg(argc, argv, "fullscreen") => true
213 * --disable-fullscreen => checkForBoolArg(argc, argv, "fullscreen") => false
214 * --fullscreen => checkForBoolArg(argc, argv, "fullscreen") => true
215 * --fullscreen true => checkForBoolArg(argc, argv, "fullscreen") => true
216 * --fullscreen false => checkForBoolArg(argc, argv, "fullscreen") => false
217 * --fullscreen 1 => checkForBoolArg(argc, argv, "fullscreen") => true
218 * --fullscreen 0 => checkForBoolArg(argc, argv, "fullscreen") => false
219 * --fullscreen yes => checkForBoolArg(argc, argv, "fullscreen") => true
220 * --fullscreen no => checkForBoolArg(argc, argv, "fullscreen") => false
221 * {none of the above} => checkForBoolArg(argc, argv, "fullscreen") => no value
222 */
223 static simgear::optional<bool> checkForBoolArg(int argc, char* argv[], const std::string& checkArg);
224
225 /**
226 * Return true when user explicitly enabled boolean option, otherwise false.
227 * Used by early startup code before Options object is created.
228 */
229 static bool checkForArgEnable(int argc, char* argv[], const std::string& checkArg);
230
231 /**
232 * Return true when user explicitly disabled boolean option by set false value.
233 * Used by early startup code before Options object is created.
234 */
235 static bool checkForArgDisable(int argc, char* argv[], const std::string& checkArg);
236
237 /**
238 * @brief check for any of the arguments which exit 'immediately' and print information to stdout,
239 * such as --help, --version, etc.
240 */
241 static bool checkForEarlyExitArg(int argc, char* argv[]);
242
243 /**
244 * @brief getArgValue - get the value of an argument if it exists, or
245 * an empty string otherwise
246 * @param argc
247 * @param argv
248 * @param checkArg : arg to look for, with '--' prefix
249 * @return value following '=' until the next white space
250 */
251 static std::string getArgValue(int argc, char* argv[], const char* checkArg);
252
253
254 /**
255 * @brief Default location to find FGData. This is based on compile-time configuration
256 * and platform conventions. For most deployments it's empty because we no longer
257 * bundle FGData with the simulator, but download it automatically.
258 *
259 * @return SGPath
260 */
261 SGPath platformDefaultRoot() const;
262
263 /**
264 * @brief Default location to download / update FGData. In older versions this
265 * was located inside the application (eg Contents/Resources on macOS). But
266 * now we download the data, it needs to be user-writeable.
267 *
268 * The value is computed based on actualDownloadDir at present
269 *
270 * @return SGPath
271 */
272 SGPath downloadedDataRoot() const;
273
274 /**
275 * @brief extractOptions - extract the currently set options as
276 * a string array. This can be used to examine what options were
277 * requested / set so far.
278 * @return
279 */
280 string_list extractOptions() const;
281
282 /**
283 @brief the actual download dir in use, which may be the default or a user-supplied value
284 */
285 SGPath actualDownloadDir() const;
286
287 /** @brief define custom location for downloads from external storage. */
288 void setCustomDownloadDir(const SGPath& p);
289
290 /**
291 * @brief the TerraSync dir we are actually using, based on defaults & options
292 */
293 SGPath actualTerrasyncDir() const;
294
295 /**
296 * Convert string to bool for boolean options. When param cannot be recognized as bool then
297 * the true is returned.
298 */
299 static bool paramToBool(const std::string& param);
300
301 private:
302 void showUsage() const;
303 void showVersion() const;
304 // Write info such as FG version, FG_ROOT, FG_HOME, scenery paths, aircraft
305 // paths, etc. to stdout in JSON format, using the UTF-8 encoding.
306 void printJSONReport() const;
307
308 // The 'fromConfigFile' parameter indicates whether the option comes from a
309 // config file or directly from the command line.
310 int parseOption(const std::string& s, const simgear::optional<std::string>& val, bool fromConfigFile);
311 /**
312 * @brief print various pieces of runtime information about the setup / system
313 */
314 void showInfo() const;
315
316 int parseConfigOption(const SGPath &path, bool fromConfigFile);
317
318 std::string getValueForBooleanOption(const std::string& str, const std::string& option, const simgear::optional<std::string>& value);
319
320 /**
321 * Since option values can be separated by a space, we check what is in
322 * the next parameter and return a string value if the current option
323 * requires a value and the value does not start with a "-" character.
324 */
325 static simgear::optional<std::string> getValueFromNextParam(int index, int argc, char** argv);
326
327 void processArgResult(int result);
328
329 /**
330 * Setup the root base, and check it's valid. If
331 * the root package was not found or is the incorrect version,
332 * returns FG_OPTIONS_ERROR. Argv/argv
333 * are passed since we might potentially show a GUI dialog at this point
334 * to help the user our (finding a base package), and hence need to init Qt.
335 */
336 OptionResult setupRoot(int argc, char** argv);
337
338
339 class OptionsPrivate;
340 std::unique_ptr<OptionsPrivate> p;
341 };
342
343 } // of namespace flightgear
344
345 void fgSetDefaults();
346
347 #endif /* _OPTIONS_HXX */