QwAnalysis
|
An options class. More...
#include <QwOptions.h>
Public Member Functions | |
QwOptions () | |
Default constructor. More... | |
QwOptions (int argc, char *argv[]) | |
Constructor with command line arguments. More... | |
QwOptions (const std::string &configfile) | |
Constructor with configuration file. More... | |
QwOptions (int argc, char *argv[], const std::string &configfile) | |
Constructor with command line arguments and configuration file. More... | |
virtual | ~QwOptions () |
Default destructor. More... | |
po::options_description_easy_init | AddDefaultOptions () |
Add a default option. More... | |
po::options_description_easy_init | AddOptions (const std::string &blockname="Specialized options") |
Add an option to a named block or create new block. More... | |
void | Usage () |
Print usage information. More... | |
void | Version () |
Print version string. More... | |
void | SetCommandLine (int argc, char *argv[], bool default_config_file=true) |
Set the command line arguments. More... | |
void | SetConfigFile (const std::string &configfile) |
Set a configuration file. More... | |
void | AddConfigFile (const std::string &configfile) |
Add a configuration file. More... | |
void | AddConfigFile (std::vector< std::string > configfiles) |
Add some configuration files. More... | |
void | ListConfigFiles () |
List the configuration files. More... | |
void | Parse (bool force=false) |
Parse all sources of options. More... | |
bool | HasValue (const std::string &key) |
Has this key been defined. More... | |
template<class T > | |
T | GetValue (const std::string &key) |
Get a templated value. More... | |
template<class T > | |
std::vector< T > | GetValueVector (const std::string &key) |
Get a list of templated values. More... | |
std::pair< int, int > | GetIntValuePair (const std::string &key) |
Get a pair of integer values. More... | |
int | GetIntValuePairFirst (const std::string &key) |
Get the first of a pair of integer values. More... | |
int | GetIntValuePairLast (const std::string &key) |
Get the last of a pair of integer values. More... | |
int | GetArgc () |
Get the number of command line arguments. More... | |
char ** | GetArgv () |
Get the vector of command line arguments. More... | |
void | Clear () |
Clear the parsed variables. More... | |
Static Public Member Functions | |
static void | DefineOptions (QwOptions &options) |
Define the options. More... | |
Private Member Functions | |
po::options_description * | CombineOptions () |
Combine the various option description in one. More... | |
void | ParseCommandLine () |
Parse the command line arguments. More... | |
void | ParseConfigFile () |
Parse the configuration file. More... | |
void | ParseEnvironment () |
Parse the environment variables. More... | |
Private Attributes | |
std::vector< std::string > | fConfigFiles |
Configuration file. More... | |
std::vector < po::options_description * > | fOptionBlock |
std::vector< std::string > | fOptionBlockName |
po::variables_map | fVariablesMap |
bool | fParsed |
Static Private Attributes | |
static int | fArgc = 0 |
Command line arguments. More... | |
static char ** | fArgv = 0 |
An options class.
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 syntac 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 133 of file QwOptions.h.
QwOptions::QwOptions | ( | ) |
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 51 of file QwOptions.cc.
References AddDefaultOptions(), and fConfigFiles.
|
inline |
Constructor with command line arguments.
Definition at line 141 of file QwOptions.h.
References SetCommandLine().
|
inline |
Constructor with configuration file.
Definition at line 145 of file QwOptions.h.
References SetConfigFile().
|
inline |
Constructor with command line arguments and configuration file.
Definition at line 149 of file QwOptions.h.
References SetCommandLine(), and SetConfigFile().
|
virtual |
Default destructor.
Destructor where we clean up locally allocated memory
Definition at line 91 of file QwOptions.cc.
References fArgv, and fOptionBlock.
|
inline |
Add a configuration file.
Definition at line 199 of file QwOptions.h.
References QwLog::endl(), fConfigFiles, fParsed, and QwMessage.
Referenced by main(), ParseCommandLine(), and SetCommandLine().
|
inline |
Add some configuration files.
Definition at line 207 of file QwOptions.h.
References fConfigFiles, and fParsed.
|
inline |
Add a default option.
Definition at line 159 of file QwOptions.h.
References AddOptions().
Referenced by QwEventRing::DefineOptions(), QwEventBuffer::DefineOptions(), and QwOptions().
|
inline |
Add an option to a named block or create new block.
Definition at line 164 of file QwOptions.h.
References Clear(), fOptionBlock, and fOptionBlockName.
Referenced by AddDefaultOptions(), QwParityDB::DefineAdditionalOptions(), QwHistogramHelper::DefineOptions(), QwEventRing::DefineOptions(), QwEventBuffer::DefineOptions(), QwDriftChamberHDC::DefineOptions(), QwEPICSEvent::DefineOptions(), QwDriftChamberVDC::DefineOptions(), QwHelicityPattern::DefineOptions(), QwRegression::DefineOptions(), QwRayTracer::DefineOptions(), QwDriftChamber::DefineOptions(), QwHelicity::DefineOptions(), QwDatabase::DefineOptions(), QwMainCerenkovDetector::DefineOptions(), QwTrackingWorker::DefineOptions(), QwTreeEventBuffer::DefineOptions(), QwMagneticField::DefineOptions(), QwTriggerScintillator::DefineOptions(), QwMainDetector::DefineOptions(), QwLog::DefineOptions(), QwLumi::DefineOptions(), QwBlinder::DefineOptions(), QwSubsystemArray::DefineOptions(), QwRootFile::DefineOptions(), main(), and setOptions().
|
inline |
Clear the parsed variables.
Definition at line 281 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 149 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 69 of file QwOptions.cc.
References QwHistogramHelper::DefineOptions(), QwEventBuffer::DefineOptions(), QwEPICSEvent::DefineOptions(), QwDatabase::DefineOptions(), QwLog::DefineOptions(), QwSubsystemArray::DefineOptions(), and QwRootFile::DefineOptions().
Referenced by DefineOptionsParity(), and DefineOptionsTracking().
|
inline |
Get the number of command line arguments.
Definition at line 276 of file QwOptions.h.
References fArgc.
Referenced by QwRootFile::QwRootFile(), and QwParityDB::SetAnalysisID().
|
inline |
Get the vector of command line arguments.
Definition at line 278 of file QwOptions.h.
References fArgv.
Referenced by QwRootFile::QwRootFile(), and QwParityDB::SetAnalysisID().
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 332 of file QwOptions.cc.
References fParsed, fVariablesMap, Parse(), and QwParameterFile::ParseIntRange().
Referenced by GetIntValuePairFirst(), GetIntValuePairLast(), QwTreeEventBuffer::ProcessOptions(), and QwEventBuffer::ProcessOptions().
|
inline |
Get the first of a pair of integer values.
Definition at line 266 of file QwOptions.h.
References GetIntValuePair().
Referenced by main().
|
inline |
Get the last of a pair of integer values.
Definition at line 271 of file QwOptions.h.
References GetIntValuePair().
Referenced by main().
|
inline |
Get a templated value.
Definition at line 240 of file QwOptions.h.
References QwLog::endl(), fParsed, fVariablesMap, Parse(), QwError, QwVerbose, and Qw::T.
Referenced by QwTrackingWorker::InitTree(), QwDriftChamberHDC::LoadChannelMap(), QwDriftChamberVDC::LoadChannelMap(), VQwSubsystemTracking::LoadGeometryDefinition(), main(), QwParityDB::ProcessAdditionalOptions(), QwHistogramHelper::ProcessOptions(), QwEventRing::ProcessOptions(), QwDriftChamberHDC::ProcessOptions(), QwEPICSEvent::ProcessOptions(), QwDriftChamberVDC::ProcessOptions(), QwHelicityPattern::ProcessOptions(), QwRegression::ProcessOptions(), QwRayTracer::ProcessOptions(), QwHelicity::ProcessOptions(), QwDatabase::ProcessOptions(), QwEventBuffer::ProcessOptions(), QwTrackingWorker::ProcessOptions(), QwTreeEventBuffer::ProcessOptions(), QwMainCerenkovDetector::ProcessOptions(), QwMagneticField::ProcessOptions(), QwTriggerScintillator::ProcessOptions(), QwMainDetector::ProcessOptions(), QwLog::ProcessOptions(), QwLumi::ProcessOptions(), QwBlinder::ProcessOptions(), QwRootFile::ProcessOptions(), QwSubsystemArray::ProcessOptionsToplevel(), QwTrackingTreeCombine::QwTrackingTreeCombine(), QwTrackingTreeSearch::QwTrackingTreeSearch(), QwTrackingWorker::QwTrackingWorker(), QwTrackingTreeCombine::TlCheckForX(), and QwTrackingTreeCombine::TlTreeCombine().
|
inline |
Get a list of templated values.
Definition at line 253 of file QwOptions.h.
References fParsed, fVariablesMap, and Parse().
Referenced by QwLog::ProcessOptions(), and QwSubsystemArray::ProcessOptionsToplevel().
|
inline |
Has this key been defined.
Definition at line 233 of file QwOptions.h.
References fParsed, fVariablesMap, and Parse().
Referenced by VQwSubsystemTracking::LoadGeometryDefinition(), main(), QwHistogramHelper::ProcessOptions(), QwEventRing::ProcessOptions(), QwHelicity::ProcessOptions(), QwDatabase::ProcessOptions(), QwEventBuffer::ProcessOptions(), QwLog::ProcessOptions(), and QwBlinder::ProcessOptions().
|
inline |
List the configuration files.
Definition at line 214 of file QwOptions.h.
References QwLog::endl(), fConfigFiles, and QwMessage.
Referenced by main().
|
inline |
Parse all sources of options.
Definition at line 222 of file QwOptions.h.
References fParsed, ParseCommandLine(), ParseConfigFile(), and ParseEnvironment().
Referenced by GetIntValuePair(), GetValue(), GetValueVector(), HasValue(), and main().
|
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 167 of file QwOptions.cc.
References AddConfigFile(), CombineOptions(), Qw::e, 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 247 of file QwOptions.cc.
References CombineOptions(), Qw::e, 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 229 of file QwOptions.cc.
References CombineOptions(), Qw::e, 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.
Referenced by main(), and QwOptions().
|
inline |
Set a configuration file.
Definition at line 192 of file QwOptions.h.
References fConfigFiles, and fParsed.
Referenced by main(), ParseCommandLine(), and QwOptions().
void QwOptions::Usage | ( | ) |
Print usage information.
Print usage information
Definition at line 288 of file QwOptions.cc.
References QwLog::endl(), fOptionBlock, and QwMessage.
Referenced by main(), and ParseCommandLine().
void QwOptions::Version | ( | ) |
Print version string.
Print version string
Definition at line 301 of file QwOptions.cc.
References QwLog::endl(), fArgv, QWANA_SVN_LASTCHANGEDREVISION, QWANA_SVN_REVISION, QWANA_SVN_URL, and QwMessage.
Referenced by ParseCommandLine().
|
staticprivate |
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 309 of file QwOptions.h.
Referenced by GetArgc(), ParseCommandLine(), and SetCommandLine().
|
staticprivate |
Definition at line 310 of file QwOptions.h.
Referenced by GetArgv(), ParseCommandLine(), SetCommandLine(), Version(), and ~QwOptions().
|
private |
Configuration file.
Definition at line 301 of file QwOptions.h.
Referenced by AddConfigFile(), ListConfigFiles(), ParseConfigFile(), QwOptions(), and SetConfigFile().
|
private |
Definition at line 316 of file QwOptions.h.
Referenced by AddOptions(), CombineOptions(), Usage(), and ~QwOptions().
|
private |
Definition at line 317 of file QwOptions.h.
Referenced by AddOptions(), and CombineOptions().
|
private |
Definition at line 322 of file QwOptions.h.
Referenced by AddConfigFile(), Clear(), GetIntValuePair(), GetValue(), GetValueVector(), HasValue(), Parse(), SetCommandLine(), and SetConfigFile().
|
private |
Definition at line 320 of file QwOptions.h.
Referenced by Clear(), GetIntValuePair(), GetValue(), GetValueVector(), HasValue(), ParseCommandLine(), ParseConfigFile(), and ParseEnvironment().