FlightGear next
options.hxx
Go to the documentation of this file.
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 <optional>
29#include <string>
30
31#include <simgear/misc/strutils.hxx>
32
33// forward decls
34class SGPath;
35
36namespace flightgear
37{
38
46 SGPath defaultDownloadDir();
47
48
63
64 class Options
65 {
66 private:
67 Options();
68
69 public:
70 static Options* sharedInstance();
71
77 static void reset();
78
79 ~Options();
80
84 OptionResult init(int argc, char* argv[], const SGPath& appDataPath);
85
89 void readConfig(const SGPath& path);
90
94 std::string valueForOption(const std::string& key, const std::string& defValue = std::string()) const;
95
99 string_list valuesForOption(const std::string& key) const;
100
105 bool isOptionSet(const std::string& key) const;
106
126 std::optional<bool> checkBoolOptionSet(const std::string& key) const;
127
133 bool isBoolOptionEnable(const std::string& key) const;
134
140 bool isBoolOptionDisable(const std::string& key) const;
141
147 int addOption(const std::string& key, const std::string& value);
148
152 int setOption(const std::string& key, const std::string& value);
153
154 void clearOption(const std::string& key);
155
161
165 void initPaths();
166
171
180 bool shouldLoadDefaultConfig() const;
181
186 void setShouldLoadDefaultConfig(bool load);
187
194 static bool checkForArg(int argc, char* argv[], const char* arg);
195
216 static std::optional<bool> checkForBoolArg(int argc, char* argv[], const std::string& checkArg);
217
222 static bool checkForArgEnable(int argc, char* argv[], const std::string& checkArg);
223
228 static bool checkForArgDisable(int argc, char* argv[], const std::string& checkArg);
229
238 static std::string getArgValue(int argc, char* argv[], const char* checkArg);
239
240
241 SGPath platformDefaultRoot() const;
242
250
254 SGPath actualDownloadDir();
255
260 static bool paramToBool(const std::string& param);
261
262 private:
263 void showUsage() const;
264 void showVersion() const;
265
269 void showInfo() const;
270
271 // Write info such as FG version, FG_ROOT, FG_HOME, scenery paths, aircraft
272 // paths, etc. to stdout in JSON format, using the UTF-8 encoding.
273 void printJSONReport() const;
274
275 // The 'fromConfigFile' parameter indicates whether the option comes from a
276 // config file or directly from the command line.
277 int parseOption(const std::string& s, const std::optional<std::string>& val, bool fromConfigFile);
278
279 int parseConfigOption(const SGPath& path, bool fromConfigFile);
280
281 std::string getValueForBooleanOption(const std::string& str, const std::string& option, const std::optional<std::string>& value);
282
288 static std::optional<std::string> getValueFromNextParam(int index, int argc, char** argv);
289
290 void processArgResult(int result);
291
299 OptionResult setupRoot(int argc, char** argv);
300
301
302 class OptionsPrivate;
303 std::unique_ptr<OptionsPrivate> p;
304 };
305
306} // of namespace flightgear
307
308void fgSetDefaults();
309
310#endif /* _OPTIONS_HXX */
static std::optional< bool > checkForBoolArg(int argc, char *argv[], const std::string &checkArg)
Check if the user has specified a given boolean option.
Definition options.cxx:3628
void setShouldLoadDefaultConfig(bool load)
when using the built-in launcher, we disable the default config files.
Definition options.cxx:3598
int addOption(const std::string &key, const std::string &value)
set an option value, assuming it is not already set (or multiple values are permitted) This can be us...
Definition options.cxx:2859
static void reset()
Delete the entire options object.
Definition options.cxx:2354
OptionResult init(int argc, char *argv[], const SGPath &appDataPath)
pass command line arguments, read default config files
Definition options.cxx:2381
int setOption(const std::string &key, const std::string &value)
set an option, overwriting any existing value which might be set
Definition options.cxx:2896
SGPath platformDefaultRoot() const
Definition options.cxx:3483
OptionResult processOptions()
apply option values to the simulation state (set properties, etc).
Definition options.cxx:3025
string_list extractOptions() const
extractOptions - extract the currently set options as a string array.
Definition options.cxx:3490
static bool checkForArgEnable(int argc, char *argv[], const std::string &checkArg)
Return true when user explicitly enabled boolean option, otherwise false.
Definition options.cxx:3681
void initPaths()
process command line options relating to scenery / aircraft / data paths
Definition options.cxx:2569
string_list valuesForOption(const std::string &key) const
return all values for a multi-valued option
Definition options.cxx:2944
static std::string getArgValue(int argc, char *argv[], const char *checkArg)
getArgValue - get the value of an argument if it exists, or an empty string otherwise
Definition options.cxx:3693
bool isBoolOptionDisable(const std::string &key) const
An overlay on checkBoolOptionSet, to check whether user used the option with explicitly disable it.
Definition options.cxx:2995
void readConfig(const SGPath &path)
parse a config file (eg, .fgfsrc)
Definition options.cxx:2691
static bool paramToBool(const std::string &param)
Convert string to bool for boolean options.
Definition options.cxx:2738
bool isBoolOptionEnable(const std::string &key) const
An overlay on checkBoolOptionSet, except that when the user has not used the option at all then false...
Definition options.cxx:2989
static bool checkForArg(int argc, char *argv[], const char *arg)
Check if the arguments array contains a particular string (with a '–' or '-' prefix).
Definition options.cxx:3603
SGPath actualDownloadDir()
the actual download dir in use, which may be the default or a user-supplied value
Definition options.cxx:3010
OptionResult initAircraft()
init the aircraft options
Definition options.cxx:2583
std::string valueForOption(const std::string &key, const std::string &defValue=std::string()) const
read the value for an option, if it has been set
Definition options.cxx:2934
static bool checkForArgDisable(int argc, char *argv[], const std::string &checkArg)
Return true when user explicitly disabled boolean option by set false value.
Definition options.cxx:3687
bool shouldLoadDefaultConfig() const
should defualt configuration files be loaded and processed or not?
Definition options.cxx:3593
std::optional< bool > checkBoolOptionSet(const std::string &key) const
Check if the user has specified a given boolean option.
Definition options.cxx:2961
bool isOptionSet(const std::string &key) const
Check if a particular option has been set (so far).
Definition options.cxx:2924
static Options * sharedInstance()
Definition options.cxx:2345
void clearOption(const std::string &key)
Definition options.cxx:2916
std::vector< std::string > string_list
Definition globals.hxx:36
FlightPlan.hxx - defines a full flight-plan object, including departure, cruise, arrival information ...
Definition Addon.cxx:53
SGPath defaultDownloadDir()
return the default platform dependant download directory.
Definition options.cxx:3001
OptionResult
option processing can have various result values depending on what the user requested.
Definition options.hxx:52
@ FG_OPTIONS_VERBOSE_HELP
Definition options.hxx:57
@ FG_OPTIONS_SHOW_AIRCRAFT
Definition options.hxx:58
@ FG_OPTIONS_OK
Definition options.hxx:53
@ FG_OPTIONS_HELP
Definition options.hxx:54
@ FG_OPTIONS_SHOW_SOUND_DEVICES
Definition options.hxx:59
@ FG_OPTIONS_EXIT
Definition options.hxx:56
@ FG_OPTIONS_ERROR
Definition options.hxx:55
@ FG_OPTIONS_NO_DEFAULT_CONFIG
Definition options.hxx:61
@ FG_OPTIONS_SHOW_INFO
Definition options.hxx:60
void fgSetDefaults()
Set a few fail-safe default property values.
Definition options.cxx:127