JAPAn
Just Another Parity Analyzer
|
Command-line and configuration file options processor. More...
#include <QwOptions.h>
Public Member Functions | |
virtual | ~QwOptions () |
Default destructor. | |
po::options_description_easy_init | AddDefaultOptions () |
Add a default option. | |
po::options_description_easy_init | AddOptions (const std::string &blockname="Specialized options") |
Add an option to a named block or create new block. | |
void | Usage () |
Print usage information. | |
void | Version () |
Print version string. | |
void | SetCommandLine (int argc, char *argv[], bool default_config_file=true) |
Set the command line arguments. | |
void | SetConfigFile (const std::string &configfile) |
Set a configuration file. | |
void | AddConfigFile (const std::string &configfile) |
Add a configuration file. | |
void | AddConfigFile (std::vector< std::string > configfiles) |
Add some configuration files. | |
void | ListConfigFiles () |
List the configuration files. | |
void | Parse (bool force=false) |
Parse all sources of options. | |
bool | HasValue (const std::string &key) |
Has this key been defined. | |
template<class T> | |
T | GetValue (const std::string &key) |
Get a templated value. | |
template<class T> | |
std::vector< T > | GetValueVector (const std::string &key) |
Get a list of templated values. | |
std::pair< int, int > | GetIntValuePair (const std::string &key) |
Get a pair of integer values. | |
int | GetIntValuePairFirst (const std::string &key) |
Get the first of a pair of integer values. | |
int | GetIntValuePairLast (const std::string &key) |
Get the last of a pair of integer values. | |
int | GetArgc () |
Get the number of command line arguments. | |
char ** | GetArgv () |
Get the vector of command line arguments. | |
void | Clear () |
Clear the parsed variables. | |
Static Public Member Functions | |
static QwOptions & | Instance () |
Get instance. | |
static void | DefineOptions (QwOptions &options) |
Define the options. | |
Private Member Functions | |
QwOptions () | |
Private default constructor. | |
QwOptions (QwOptions const &) | |
Private copy constructor, not implemented. | |
QwOptions & | operator= (QwOptions const &) |
Private assignment operator, not implemented. | |
po::options_description * | CombineOptions () |
Combine the various option description in one. | |
void | ParseCommandLine () |
Parse the command line arguments. | |
void | ParseConfigFile () |
Parse the configuration file. | |
void | ParseEnvironment () |
Parse the environment variables. | |
Private Attributes | |
std::vector< std::string > | fConfigFiles |
Configuration file. | |
int | fArgc |
Command line arguments. | |
char ** | fArgv |
std::vector< po::options_description * > | fOptionBlock |
std::vector< std::string > | fOptionBlockName |
po::variables_map | fVariablesMap |
bool | fParsed |
Static Private Attributes | |
static QwOptions * | fInstance = 0 |
Command-line and configuration file options processor.
QwOptions provides a unified interface for handling command-line arguments, configuration files, and environment variables using Boost.Program_options. It supports typed options with defaults, positional arguments, and hierarchical configuration loading. The global gQwOptions singleton is available throughout the framework.
On instantiation of the global gQwOptions object the argc and argv are passed. The filename is set to a default value, but on instantiation a check is done for the option –config filename. After this no parsing is done without the user requesting a value.
To use this class in your own programs, just include the header file. This will make the global object gQwOptions available. Set the command line and config file with the methods SetCommandLine() and SetConfigFile() in the main routine. In other classes you can add subsystem-specific config files with the AddConfigFile() method.
Define your own options by calling the AddOptions() method. Look in the documentation of boost::program_options for the allowed syntax if you need more than just '–key value' pairs. Positional arguments are supported (untested), default values can and should preferably be given, and multiple values can be put into a vector corresponding to a single key (untested).
For boolean-valued options you might want to use zero_tokens() to allow the option to be specified as –bool instead of –bool yes.
Keep in mind, though, that this then ignores the value after the option completely, and it will not allow you to unset settings in the default configuration file. A better approach is to use the implicit_value(b) semantic that will still accept the value after the option. However, this was only introduced in boost 1.35.0. To avoid the need to test for this, use the syntax default_bool_value(b), with b the default value. If the flag is specified, the option will be set to true regardless of the specified default value.
It is easiest if you define your options in a static function DefineOptions() and then call that function in the QwOptionsTracking.h or QwOptionsParity.h header files. This ensures that only a single call to gQwOptions.DefineOptions() will load all the options and provide the information on a –help call.
To use the options, check first for the existence with HasValue(key), then get their value using GetValue<type>(key). Flags defined without type are treated as 'existence-only' keys, so use HasValue(key) on them instead of GetValue<bool>(key). If you define an option as bool, you should still use the regular GetValue<bool>(key) to retrieve the value.
To get the results of an integer range argument (strictly speaking a string argument), you can use GetIntValuePair() which returns a pair<int,int>, or the more specific GetIntValuePairFirst() and GetIntValuePairLast().
The default allowed options are:
Definition at line 141 of file QwOptions.h.
|
private |
Private default constructor.
The default constructor sets up the options description object with some options that should always be there. The other options can be setup wherever this object is accessible.
Definition at line 49 of file QwOptions.cc.
References AddDefaultOptions(), fArgc, fArgv, fConfigFiles, and fParsed.
Referenced by DefineOptions(), Instance(), operator=(), and QwOptions().
|
private |
Private copy constructor, not implemented.
References QwOptions().
|
virtual |
Default destructor.
Destructor where we clean up locally allocated memory
Definition at line 91 of file QwOptions.cc.
References fArgv, and fOptionBlock.
void QwOptions::AddConfigFile | ( | const std::string & | configfile | ) |
Add a configuration file.
Add the named configuration file to the list.
configfile | Name of the config file, without path |
Definition at line 161 of file QwOptions.cc.
References QwLog::endl(), fConfigFiles, fParsed, and QwMessage.
Referenced by AddConfigFile(), ParseCommandLine(), ParseConfigFile(), and SetCommandLine().
|
inline |
Add some configuration files.
Definition at line 204 of file QwOptions.h.
References AddConfigFile().
|
inline |
Add a default option.
Definition at line 165 of file QwOptions.h.
References AddOptions().
Referenced by QwEventBuffer::DefineOptions(), and QwOptions().
|
inline |
Add an option to a named block or create new block.
Definition at line 170 of file QwOptions.h.
References Clear(), fOptionBlock, and fOptionBlockName.
Referenced by AddDefaultOptions(), QwBlinder::DefineOptions(), QwCorrelator::DefineOptions(), QwDatabase::DefineOptions(), QwDataHandlerArray::DefineOptions(), QwEPICSEvent::DefineOptions(), QwEventBuffer::DefineOptions(), QwEventRing::DefineOptions(), QwHelicity::DefineOptions(), QwHelicityCorrelatedFeedback::DefineOptions(), QwHelicityPattern::DefineOptions(), QwHistogramHelper::DefineOptions(), QwLog::DefineOptions(), QwRootFile::DefineOptions(), QwSubsystemArray::DefineOptions(), and VQwDetectorArray::DefineOptions().
|
inline |
Clear the parsed variables.
Definition at line 277 of file QwOptions.h.
References fParsed, and fVariablesMap.
Referenced by AddOptions().
|
private |
Combine the various option description in one.
Combine the options of the various option descriptions in one object for parsing at once. This avoids having to try/catch every single option description
Definition at line 183 of file QwOptions.cc.
References fOptionBlock, and fOptionBlockName.
Referenced by ParseCommandLine(), ParseConfigFile(), and ParseEnvironment().
|
static |
Define the options.
Define standard options on the specified options object
options | Options object |
Definition at line 67 of file QwOptions.cc.
References QwDatabase::DefineOptions(), QwEPICSEvent::DefineOptions(), QwEventBuffer::DefineOptions(), QwHistogramHelper::DefineOptions(), QwLog::DefineOptions(), QwRootFile::DefineOptions(), QwSubsystemArray::DefineOptions(), and QwOptions().
Referenced by DefineOptionsParity().
|
inline |
Get the number of command line arguments.
Definition at line 272 of file QwOptions.h.
References fArgc.
|
inline |
Get the vector of command line arguments.
Definition at line 274 of file QwOptions.h.
References fArgv.
std::pair< int, int > QwOptions::GetIntValuePair | ( | const std::string & | key | ) |
Get a pair of integer values.
Get a pair of integers specified as a colon-separated range This function uses the utility function QwParameterFile::ParseIntRange.
key | Option key |
Definition at line 357 of file QwOptions.cc.
References fParsed, fVariablesMap, Parse(), and QwParameterFile::ParseIntRange().
Referenced by GetIntValuePairFirst(), GetIntValuePairLast(), and QwEventBuffer::ProcessOptions().
|
inline |
Get the first of a pair of integer values.
Definition at line 262 of file QwOptions.h.
References GetIntValuePair().
|
inline |
Get the last of a pair of integer values.
Definition at line 267 of file QwOptions.h.
References GetIntValuePair().
|
inline |
Get a templated value.
Definition at line 236 of file QwOptions.h.
References QwLog::endl(), fParsed, fVariablesMap, Parse(), QwError, and QwVerbose.
Referenced by QwSubsystemArray::LoadAllEventRanges(), QwBlinder::ProcessOptions(), QwDatabase::ProcessOptions(), QwDataHandlerArray::ProcessOptions(), QwEPICSEvent::ProcessOptions(), QwEventBuffer::ProcessOptions(), QwHelicity::ProcessOptions(), QwHelicityCorrelatedFeedback::ProcessOptions(), QwHelicityPattern::ProcessOptions(), QwHistogramHelper::ProcessOptions(), QwLog::ProcessOptions(), QwRootFile::ProcessOptions(), VQwDetectorArray::ProcessOptions(), and QwSubsystemArray::ProcessOptionsToplevel().
|
inline |
Get a list of templated values.
Definition at line 249 of file QwOptions.h.
References fParsed, fVariablesMap, and Parse().
Referenced by QwDataHandlerArray::ProcessOptions(), QwLog::ProcessOptions(), QwRootFile::ProcessOptions(), and QwSubsystemArray::ProcessOptionsToplevel().
|
inline |
Has this key been defined.
Definition at line 229 of file QwOptions.h.
References fParsed, fVariablesMap, and Parse().
Referenced by QwDatabase::ProcessOptions(), QwDataHandlerArray::ProcessOptions(), QwEventBuffer::ProcessOptions(), QwHelicity::ProcessOptions(), QwHistogramHelper::ProcessOptions(), and QwLog::ProcessOptions().
|
inlinestatic |
Get instance.
Definition at line 155 of file QwOptions.h.
References fInstance, and QwOptions().
|
inline |
List the configuration files.
Definition at line 210 of file QwOptions.h.
References QwLog::endl(), fConfigFiles, and QwMessage.
Private assignment operator, not implemented.
References QwOptions().
|
inline |
Parse all sources of options.
Definition at line 218 of file QwOptions.h.
References fParsed, ParseCommandLine(), ParseConfigFile(), and ParseEnvironment().
Referenced by GetIntValuePair(), GetValue(), GetValueVector(), and HasValue().
|
private |
Parse the command line arguments.
Parse the command line arguments for options and warn when encountering an unknown option, then notify the variables map. Print usage instructions when no options are given, or when explicitly asked for.
Definition at line 201 of file QwOptions.cc.
References AddConfigFile(), CombineOptions(), QwLog::endl(), fArgc, fArgv, fVariablesMap, QwWarning, SetConfigFile(), Usage(), and Version().
Referenced by Parse().
|
private |
Parse the configuration file.
Parse the configuration file for options and warn when encountering an unknown option, then notify the variables map.
Definition at line 278 of file QwOptions.cc.
References AddConfigFile(), CombineOptions(), QwLog::endl(), fConfigFiles, fVariablesMap, QwWarning, and QwParameterFile::rdbuf().
Referenced by Parse().
|
private |
Parse the environment variables.
Parse the environment variables for options and notify the variables map.
Definition at line 242 of file QwOptions.cc.
References CombineOptions(), QwLog::endl(), fVariablesMap, and QwWarning.
Referenced by Parse().
void QwOptions::SetCommandLine | ( | int | argc, |
char * | argv[], | ||
bool | default_config_file = true ) |
Set the command line arguments.
Make a local copy of the command line arguments so they are available for later parsing.
argc | Number of arguments |
argv[] | Array of arguments |
default_config_file | Flag to enable default config file named by executable |
Definition at line 112 of file QwOptions.cc.
References AddConfigFile(), QwLog::endl(), fArgc, fArgv, fParsed, and QwDebug.
void QwOptions::SetConfigFile | ( | const std::string & | configfile | ) |
Set a configuration file.
Set the named configuration file as the first (and initially only) one in the list.
configfile | Name of the config file, without path |
Definition at line 147 of file QwOptions.cc.
References QwLog::endl(), fConfigFiles, fParsed, and QwWarning.
Referenced by ParseCommandLine().
void QwOptions::Usage | ( | ) |
Print usage information.
Print usage information
Definition at line 312 of file QwOptions.cc.
References QwLog::endl(), fOptionBlock, and QwMessage.
Referenced by ParseCommandLine().
void QwOptions::Version | ( | ) |
Print version string.
Print version string
Definition at line 325 of file QwOptions.cc.
References QwLog::endl(), fArgv, gGitInfo, and QwMessage.
Referenced by ParseCommandLine().
|
private |
Command line arguments.
Because argc and argv are only available in the main routine, we need to store a copy of them for later parsing. The copy is static to have them available in local copies of the options object.
Definition at line 305 of file QwOptions.h.
Referenced by GetArgc(), ParseCommandLine(), QwOptions(), and SetCommandLine().
|
private |
Definition at line 306 of file QwOptions.h.
Referenced by GetArgv(), ParseCommandLine(), QwOptions(), SetCommandLine(), Version(), and ~QwOptions().
|
private |
Configuration file.
Definition at line 297 of file QwOptions.h.
Referenced by AddConfigFile(), ListConfigFiles(), ParseConfigFile(), QwOptions(), and SetConfigFile().
|
staticprivate |
Definition at line 144 of file QwOptions.h.
Referenced by Instance().
|
private |
Definition at line 312 of file QwOptions.h.
Referenced by AddOptions(), CombineOptions(), Usage(), and ~QwOptions().
|
private |
Definition at line 313 of file QwOptions.h.
Referenced by AddOptions(), and CombineOptions().
|
private |
Definition at line 318 of file QwOptions.h.
Referenced by AddConfigFile(), Clear(), GetIntValuePair(), GetValue(), GetValueVector(), HasValue(), Parse(), QwOptions(), SetCommandLine(), and SetConfigFile().
|
private |
Definition at line 316 of file QwOptions.h.
Referenced by Clear(), GetIntValuePair(), GetValue(), GetValueVector(), HasValue(), ParseCommandLine(), ParseConfigFile(), and ParseEnvironment().