QwAnalysis
QwOptions Class Reference

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 >
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
 

Detailed Description

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.

* gQwOptions.SetCommandLine(argc, argv); // store command line arguments
* gQwOptions.SetConfigFile("qwtracking.conf"); // set the default config file
* gQwOptions.AddConfigFile("qweak-tracking.conf"); // add another config file
*

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).

* gQwOptions.AddOptions()("int,i", po::value<int>(), "integer-valued option");
* gQwOptions.AddOptions()("bool,b", po::value<bool>(), "boolean-valued option");
* gQwOptions.AddOptions()("float,f", po::value<float>(), "float-valued option");
* gQwOptions.AddOptions()("string,s", po::value<string>(), "string-valued option");
* gQwOptions.AddOptions()("range,r", po::value<string>(), "range-valued option");
* gQwOptions.AddOptions()("def-int", po::value<int>()->default_value(1), "int-valued option, default of 1");
*

For boolean-valued options you might want to use zero_tokens() to allow the option to be specified as –bool instead of –bool yes.

* gQwOptions.AddOptions()("bool,b", po::value<bool>()->zero_tokens(), "boolean-valued option");
*

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.

* // Test for presence of the option before using its value
* if (gQwOptions.HasValue("string")) string my_string = gQwOptions.GetValue<string>("string");
* // Or use options with default values (preferable)
* int my_int = gQwOptions.GetValue<int>("def-int");
*

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().

* std::pair<int,int> my_run_pair = gQwOptions.GetIntValuePair("run");
*

The default allowed options are:

  • –usage: print a help message
  • –help, -h: print a help message
  • –config, -c: configuration file to read

Definition at line 133 of file QwOptions.h.

Constructor & Destructor Documentation

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.

52  : fParsed(false)
53 {
54  // No default config files
55  fConfigFiles.clear();
56 
57  // Declare the default options
58  AddDefaultOptions()("usage", "print this help message");
59  AddDefaultOptions()("help,h", "print this help message");
60  AddDefaultOptions()("version,V", "print the version string");
61  AddDefaultOptions()("config,c", po::value<std::string>(), "read ONLY this config file\n(will override default config files)");
62  AddDefaultOptions()("add-config,a", po::value<std::vector<std::string> >(), "read ALSO this config file\n(will keep the default config files)");
63 }
po::options_description_easy_init AddDefaultOptions()
Add a default option.
Definition: QwOptions.h:159
std::vector< std::string > fConfigFiles
Configuration file.
Definition: QwOptions.h:301
bool fParsed
Definition: QwOptions.h:322

+ Here is the call graph for this function:

QwOptions::QwOptions ( int  argc,
char *  argv[] 
)
inline

Constructor with command line arguments.

Definition at line 141 of file QwOptions.h.

References SetCommandLine().

141  {
142  SetCommandLine(argc, argv);
143  };
void SetCommandLine(int argc, char *argv[], bool default_config_file=true)
Set the command line arguments.
Definition: QwOptions.cc:112

+ Here is the call graph for this function:

QwOptions::QwOptions ( const std::string &  configfile)
inline

Constructor with configuration file.

Definition at line 145 of file QwOptions.h.

References SetConfigFile().

145  {
146  SetConfigFile(configfile);
147  };
void SetConfigFile(const std::string &configfile)
Set a configuration file.
Definition: QwOptions.h:192

+ Here is the call graph for this function:

QwOptions::QwOptions ( int  argc,
char *  argv[],
const std::string &  configfile 
)
inline

Constructor with command line arguments and configuration file.

Definition at line 149 of file QwOptions.h.

References SetCommandLine(), and SetConfigFile().

149  {
150  SetCommandLine(argc, argv);
151  SetConfigFile(configfile);
152  };
void SetConfigFile(const std::string &configfile)
Set a configuration file.
Definition: QwOptions.h:192
void SetCommandLine(int argc, char *argv[], bool default_config_file=true)
Set the command line arguments.
Definition: QwOptions.cc:112

+ Here is the call graph for this function:

QwOptions::~QwOptions ( )
virtual

Default destructor.

Destructor where we clean up locally allocated memory

Definition at line 91 of file QwOptions.cc.

References fArgv, and fOptionBlock.

92 {
93  // Clean up the copy of the command line arguments
94  // Note: only the array of arguments is allocated, the arguments themselves
95  // are still owned by main.
96  if (fArgv)
97  delete[] fArgv;
98 
99  // Delete the option blocks
100  for (size_t i = 0; i < fOptionBlock.size(); i++)
101  delete fOptionBlock.at(i);
102  fOptionBlock.clear();
103 }
std::vector< po::options_description * > fOptionBlock
Definition: QwOptions.h:316
static char ** fArgv
Definition: QwOptions.h:310

Member Function Documentation

void QwOptions::AddConfigFile ( const std::string &  configfile)
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().

199  {
200  QwMessage << "Adding user-defined configuration file "
201  << configfile << QwLog::endl;
202  fConfigFiles.push_back(configfile);
203  fParsed = false;
204  };
#define QwMessage
Predefined log drain for regular messages.
Definition: QwLog.h:50
std::vector< std::string > fConfigFiles
Configuration file.
Definition: QwOptions.h:301
bool fParsed
Definition: QwOptions.h:322
static std::ostream & endl(std::ostream &)
End of the line.
Definition: QwLog.cc:299

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void QwOptions::AddConfigFile ( std::vector< std::string >  configfiles)
inline

Add some configuration files.

Definition at line 207 of file QwOptions.h.

References fConfigFiles, and fParsed.

207  {
208  for (size_t i = 0; i < configfiles.size(); i++)
209  fConfigFiles.push_back(configfiles.at(i));
210  fParsed = false;
211  };
std::vector< std::string > fConfigFiles
Configuration file.
Definition: QwOptions.h:301
bool fParsed
Definition: QwOptions.h:322
po::options_description_easy_init QwOptions::AddDefaultOptions ( )
inline

Add a default option.

Definition at line 159 of file QwOptions.h.

References AddOptions().

Referenced by QwEventRing::DefineOptions(), QwEventBuffer::DefineOptions(), and QwOptions().

159  {
160  return AddOptions("Default options");
161  };
po::options_description_easy_init AddOptions(const std::string &blockname="Specialized options")
Add an option to a named block or create new block.
Definition: QwOptions.h:164

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

po::options_description_easy_init QwOptions::AddOptions ( const std::string &  blockname = "Specialized options")
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().

164  {
165  // Clear the parsed options
166  Clear();
167  // Note: Would like to solve this with find_if(b,e,bind()) but no access to block name
168  // Search for the block name if it exists
169  for (size_t i = 0; i < fOptionBlockName.size(); i++)
170  if (blockname == fOptionBlockName.at(i))
171  return fOptionBlock.at(i)->add_options();
172  // Create new block if not existing yet
173  fOptionBlock.push_back(new po::options_description(blockname));
174  fOptionBlockName.push_back(blockname);
175  return fOptionBlock.back()->add_options();
176  };
std::vector< po::options_description * > fOptionBlock
Definition: QwOptions.h:316
std::vector< std::string > fOptionBlockName
Definition: QwOptions.h:317
void Clear()
Clear the parsed variables.
Definition: QwOptions.h:281

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void QwOptions::Clear ( )
inline

Clear the parsed variables.

Definition at line 281 of file QwOptions.h.

References fParsed, and fVariablesMap.

Referenced by AddOptions().

281  {
282  fVariablesMap.clear();
283  fParsed = false;
284  };
bool fParsed
Definition: QwOptions.h:322
po::variables_map fVariablesMap
Definition: QwOptions.h:320

+ Here is the caller graph for this function:

po::options_description * QwOptions::CombineOptions ( )
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().

150 {
151  // The options can be grouped by defining a vector fOptions of
152  // options_description objects. Each entry can have a name and
153  // will show up as a separate section in the usage information.
154  po::options_description* options = new po::options_description("options");
155  for (size_t i = 0; i < fOptionBlockName.size(); i++) {
156  // Right now every parser gets access to all options
157  options->add(*fOptionBlock.at(i));
158  }
159  return options;
160 }
std::vector< po::options_description * > fOptionBlock
Definition: QwOptions.h:316
std::vector< std::string > fOptionBlockName
Definition: QwOptions.h:317

+ Here is the caller graph for this function:

void QwOptions::DefineOptions ( QwOptions options)
static

Define the options.

Define standard options on the specified options object

Parameters
optionsOptions 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().

70 {
71  // Define logging options (Note: only QwLog takes a pointer argument!!!)
72  QwLog::DefineOptions(&options);
73 
74  // Define execution options
76  // Define database options
78  // Define ROOT file options
80  // Define EPICS event options
82  // Define subsystem array options
84  // Define histogram helper options
86 }
static void DefineOptions(QwOptions &options)
Define the configuration options.
static void DefineOptions(QwOptions *options)
Define available class options for QwOptions.
Definition: QwLog.cc:73
static void DefineOptions(QwOptions &options)
Define the configuration options.
Definition: QwEPICSEvent.cc:69
static void DefineOptions(QwOptions &options)
static void DefineOptions(QwOptions &options)
Define configuration options for global array.
static void DefineOptions(QwOptions &options)
Defines available class options for QwOptions.
Definition: QwDatabase.cc:209
static void DefineOptions(QwOptions &options)
Define the configuration options.
Definition: QwRootFile.cc:169

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int QwOptions::GetArgc ( )
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().

276 { return (int) fArgc; };
static int fArgc
Command line arguments.
Definition: QwOptions.h:309

+ Here is the caller graph for this function:

char** QwOptions::GetArgv ( )
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().

278 { return (char**) fArgv; };
static char ** fArgv
Definition: QwOptions.h:310

+ Here is the caller graph for this function:

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.

Parameters
keyOption key
Returns
Pair of integers

Definition at line 332 of file QwOptions.cc.

References fParsed, fVariablesMap, Parse(), and QwParameterFile::ParseIntRange().

Referenced by GetIntValuePairFirst(), GetIntValuePairLast(), QwTreeEventBuffer::ProcessOptions(), and QwEventBuffer::ProcessOptions().

333 {
334  std::pair<int, int> mypair;
335  mypair.first = 0;
336  mypair.second = 0;
337 
338  if (fParsed == false) Parse();
339  if (fVariablesMap.count(key)) {
340  std::string range = fVariablesMap[key].as<std::string>();
341  mypair = QwParameterFile::ParseIntRange(":",range);
342  }
343 
344  return mypair;
345 }
bool fParsed
Definition: QwOptions.h:322
void Parse(bool force=false)
Parse all sources of options.
Definition: QwOptions.h:222
static std::pair< int, int > ParseIntRange(const std::string &separatorchars, const std::string &range)
Parse a range of integers as #:# where either can be missing.
po::variables_map fVariablesMap
Definition: QwOptions.h:320

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int QwOptions::GetIntValuePairFirst ( const std::string &  key)
inline

Get the first of a pair of integer values.

Definition at line 266 of file QwOptions.h.

References GetIntValuePair().

Referenced by main().

266  {
267  return GetIntValuePair(key).first;
268  };
std::pair< int, int > GetIntValuePair(const std::string &key)
Get a pair of integer values.
Definition: QwOptions.cc:332

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int QwOptions::GetIntValuePairLast ( const std::string &  key)
inline

Get the last of a pair of integer values.

Definition at line 271 of file QwOptions.h.

References GetIntValuePair().

Referenced by main().

271  {
272  return GetIntValuePair(key).second;
273  };
std::pair< int, int > GetIntValuePair(const std::string &key)
Get a pair of integer values.
Definition: QwOptions.cc:332

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

template<class T >
T QwOptions::GetValue ( const std::string &  key)
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().

240  {
241  if (fParsed == false) Parse();
242  if (fVariablesMap.count(key)) {
243  QwVerbose << "Option " << key << ": "
244  << fVariablesMap[key].as<T>() << QwLog::endl;
245  return fVariablesMap[key].as<T>();
246  } else {
247  QwError << "Variable " << key << " unknown" << QwLog::endl;
248  return 0;
249  }
250  }
#define QwVerbose
Predefined log drain for verbose messages.
Definition: QwLog.h:55
bool fParsed
Definition: QwOptions.h:322
void Parse(bool force=false)
Parse all sources of options.
Definition: QwOptions.h:222
static const double T
Magnetic field: base unit is T.
Definition: QwUnits.h:111
static std::ostream & endl(std::ostream &)
End of the line.
Definition: QwLog.cc:299
po::variables_map fVariablesMap
Definition: QwOptions.h:320
#define QwError
Predefined log drain for errors.
Definition: QwLog.h:40

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

template<class T >
std::vector< T > QwOptions::GetValueVector ( const std::string &  key)
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().

253  {
254  std::vector<T> list;
255  if (fParsed == false) Parse();
256  if (fVariablesMap.count(key)) {
257  list = fVariablesMap[key].as< std::vector<T> >();
258  }
259  return list;
260  }
bool fParsed
Definition: QwOptions.h:322
void Parse(bool force=false)
Parse all sources of options.
Definition: QwOptions.h:222
po::variables_map fVariablesMap
Definition: QwOptions.h:320

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool QwOptions::HasValue ( const std::string &  key)
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().

233  {
234  if (fParsed == false) Parse();
235  return (fVariablesMap.count(key) > 0);
236  };
bool fParsed
Definition: QwOptions.h:322
void Parse(bool force=false)
Parse all sources of options.
Definition: QwOptions.h:222
po::variables_map fVariablesMap
Definition: QwOptions.h:320

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void QwOptions::ListConfigFiles ( )
inline

List the configuration files.

Definition at line 214 of file QwOptions.h.

References QwLog::endl(), fConfigFiles, and QwMessage.

Referenced by main().

214  {
215  QwMessage << "Config files:" << QwLog::endl;
216  for (size_t i = 0; i < fConfigFiles.size(); i++)
217  QwMessage << fConfigFiles.at(i) << QwLog::endl;
218  };
#define QwMessage
Predefined log drain for regular messages.
Definition: QwLog.h:50
std::vector< std::string > fConfigFiles
Configuration file.
Definition: QwOptions.h:301
static std::ostream & endl(std::ostream &)
End of the line.
Definition: QwLog.cc:299

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void QwOptions::Parse ( bool  force = false)
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().

222  {
223  if (! fParsed || force) {
226  ParseConfigFile();
227  fParsed = true;
228  }
229  };
void ParseCommandLine()
Parse the command line arguments.
Definition: QwOptions.cc:167
void ParseConfigFile()
Parse the configuration file.
Definition: QwOptions.cc:247
void ParseEnvironment()
Parse the environment variables.
Definition: QwOptions.cc:229
bool fParsed
Definition: QwOptions.h:322

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void QwOptions::ParseCommandLine ( )
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().

168 {
169 #if BOOST_VERSION >= 103300
170  // Boost versions starting with 1.33.00 allow unrecognized options to be
171  // passed through the parser.
172  try {
173  po::options_description* command_line_options = CombineOptions();
174  po::store(po::command_line_parser(fArgc, fArgv).options(*command_line_options).allow_unregistered().run(), fVariablesMap);
175  delete command_line_options;
176  } catch (std::exception const& e) {
177  QwWarning << e.what() << " while parsing command line arguments" << QwLog::endl;
178  exit(10);
179  }
180 #endif
181 
182 #if BOOST_VERSION < 103300
183  // Boost versions before 1.33.00 do not allow unrecognized options to be
184  // passed through the parser.
185  try {
186  // Boost versions before 1.33.00 do not recognize "allow_unregistered".
187  po::options_description* command_line_options = CombineOptions();
188  po::store(po::command_line_parser(fArgc, fArgv).options(*command_line_options).run(), fVariablesMap);
189  delete command_line_options;
190  } catch (std::exception const& e) {
191  QwWarning << e.what() << " while parsing command line arguments" << QwLog::endl;
192  QwWarning << "All command line arguments may have been ignored!" << QwLog::endl;
193  exit(10);
194  }
195 #endif
196 
197  // Notify of new options
198  po::notify(fVariablesMap);
199 
200  // If option help/usage, print help text
201  if (fVariablesMap.count("help") || fVariablesMap.count("usage")) {
202  Usage();
203  exit(0);
204  }
205 
206  // If option version, print version string
207  if (fVariablesMap.count("version")) {
208  Version();
209  exit(0);
210  }
211 
212  // If a configuration file is specified, load it.
213  if (fVariablesMap.count("config") > 0) {
214  QwWarning << "Overriding the default configuration files with "
215  << "user-defined configuration file "
216  << fVariablesMap["config"].as<std::string>() << QwLog::endl;
217  SetConfigFile(fVariablesMap["config"].as<std::string>());
218  }
219  // If a configuration file is specified, load it.
220  if (fVariablesMap.count("add-config") > 0) {
221  QwWarning << "Adding user-defined configuration file " << QwLog::endl;
222  AddConfigFile(fVariablesMap["add-config"].as<std::vector<std::string> >());
223  }
224 }
static const double e
Definition: QwUnits.h:91
static char ** fArgv
Definition: QwOptions.h:310
void Usage()
Print usage information.
Definition: QwOptions.cc:288
void SetConfigFile(const std::string &configfile)
Set a configuration file.
Definition: QwOptions.h:192
void Version()
Print version string.
Definition: QwOptions.cc:301
void AddConfigFile(const std::string &configfile)
Add a configuration file.
Definition: QwOptions.h:199
po::options_description * CombineOptions()
Combine the various option description in one.
Definition: QwOptions.cc:149
static std::ostream & endl(std::ostream &)
End of the line.
Definition: QwLog.cc:299
#define QwWarning
Predefined log drain for warnings.
Definition: QwLog.h:45
static int fArgc
Command line arguments.
Definition: QwOptions.h:309
po::variables_map fVariablesMap
Definition: QwOptions.h:320

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void QwOptions::ParseConfigFile ( )
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().

248 {
249  for (size_t i = 0; i < fConfigFiles.size(); i++) {
250  QwParameterFile configfile(fConfigFiles.at(i).c_str());
251  std::stringstream configstream;
252  configstream << configfile.rdbuf();
253 
254  try {
255 #if BOOST_VERSION >= 103500
256  // Boost version after 1.35 have bool allow_unregistered = false in
257  // their signature. This allows for unknown options in the config file.
258  po::options_description* config_file_options = CombineOptions();
259  po::store(po::parse_config_file(configstream, *config_file_options, true),
260  fVariablesMap);
261  delete config_file_options;
262 #else
263  // Boost versions before 1.35 cannot handle files with unregistered
264  // options.
265  po::options_description* config_file_options = CombineOptions();
266  po::store(po::parse_config_file(configstream, *config_file_options),
267  fVariablesMap);
268  delete config_file_options;
269 #endif
270  } catch (std::exception const& e) {
271  QwWarning << e.what() << " while parsing configuration file "
272  << fConfigFiles.at(i) << QwLog::endl;
273 #if BOOST_VERSION < 103500
274  QwWarning << "The entire configuration file was ignored!" << QwLog::endl;
275 #endif
276  exit(10);
277  }
278  // Notify of new options
279  po::notify(fVariablesMap);
280  }
281 }
std::streambuf * rdbuf() const
Access the streambuf pointer in the same way as on a std::ifstream.
static const double e
Definition: QwUnits.h:91
std::vector< std::string > fConfigFiles
Configuration file.
Definition: QwOptions.h:301
po::options_description * CombineOptions()
Combine the various option description in one.
Definition: QwOptions.cc:149
static std::ostream & endl(std::ostream &)
End of the line.
Definition: QwLog.cc:299
#define QwWarning
Predefined log drain for warnings.
Definition: QwLog.h:45
po::variables_map fVariablesMap
Definition: QwOptions.h:320

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void QwOptions::ParseEnvironment ( )
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().

230 {
231  try {
232  po::options_description* environment_options = CombineOptions();
233  po::store(po::parse_environment(*environment_options, "Qw"), fVariablesMap);
234  delete environment_options;
235  } catch (std::exception const& e) {
236  QwWarning << e.what() << " while parsing environment variables" << QwLog::endl;
237  exit(10);
238  }
239  // Notify of new options
240  po::notify(fVariablesMap);
241 }
static const double e
Definition: QwUnits.h:91
po::options_description * CombineOptions()
Combine the various option description in one.
Definition: QwOptions.cc:149
static std::ostream & endl(std::ostream &)
End of the line.
Definition: QwLog.cc:299
#define QwWarning
Predefined log drain for warnings.
Definition: QwLog.h:45
po::variables_map fVariablesMap
Definition: QwOptions.h:320

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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.

Parameters
argcNumber of arguments
argv[]Array of arguments
default_config_fileFlag 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().

113 {
114  // Copy command line options
115  fArgc = argc;
116  if (fArgv) delete[] fArgv;
117  fArgv = new char*[fArgc];
118  QwDebug << "Arguments:";
119  for (int i = 0; i < argc; i++) {
120  fArgv[i] = argv[i];
121  QwDebug << " " << fArgv[i];
122  }
123  QwDebug << QwLog::endl;
124 
125  fParsed = false;
126 
127  // Add default config file based on file name
128  if (fArgc > 0 && default_config_file) {
129  std::string path = fArgv[0];
130  QwDebug << "Invocation name: " << path << QwLog::endl;
131  // Find file name from full path
132  size_t pos = path.find_last_of('/');
133  if (pos != std::string::npos)
134  // Called with path
135  AddConfigFile(path.substr(pos+1) + ".conf");
136  else
137  // Called without path
138  AddConfigFile(path + ".conf");
139  }
140 }
static char ** fArgv
Definition: QwOptions.h:310
bool fParsed
Definition: QwOptions.h:322
#define QwDebug
Predefined log drain for debugging output.
Definition: QwLog.h:60
void AddConfigFile(const std::string &configfile)
Add a configuration file.
Definition: QwOptions.h:199
static std::ostream & endl(std::ostream &)
End of the line.
Definition: QwLog.cc:299
static int fArgc
Command line arguments.
Definition: QwOptions.h:309

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void QwOptions::SetConfigFile ( const std::string &  configfile)
inline

Set a configuration file.

Definition at line 192 of file QwOptions.h.

References fConfigFiles, and fParsed.

Referenced by main(), ParseCommandLine(), and QwOptions().

192  {
193  fConfigFiles.clear();
194  fConfigFiles.push_back(configfile);
195  fParsed = false;
196  };
std::vector< std::string > fConfigFiles
Configuration file.
Definition: QwOptions.h:301
bool fParsed
Definition: QwOptions.h:322

+ Here is the caller graph for this function:

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().

289 {
291  QwMessage << "Welcome to the Qweak analyzer code." << QwLog::endl;
293  for (size_t i = 0; i < fOptionBlock.size(); i++)
294  QwMessage << *(fOptionBlock.at(i)) << QwLog::endl;
295 }
#define QwMessage
Predefined log drain for regular messages.
Definition: QwLog.h:50
std::vector< po::options_description * > fOptionBlock
Definition: QwOptions.h:316
static std::ostream & endl(std::ostream &)
End of the line.
Definition: QwLog.cc:299

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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().

302 {
303  TString root_version = gROOT->GetVersion();
304  root_version += ", Date : ";
305  root_version += gROOT->GetVersionDate();
306 #if ROOT_VERSION_CODE < ROOT_VERSION(6,0,0)
307  root_version += ", SVN : ";
308  root_version += gROOT->GetSvnRevision();
309  root_version += " ";
310  root_version += gROOT->GetSvnBranch();
311 #else // ROOT_VERSION_CODE >= ROOT_VERSION(6,0,0)
312  root_version += ", GIT : ";
313  root_version += gROOT->GetGitCommit();
314  root_version += " ";
315  root_version += gROOT->GetGitBranch();
316 #endif
317 
318  QwMessage << "\n Qweak Analysis Framework : " << fArgv[0] << QwLog::endl;
319  QwMessage << " * Revision: " << QWANA_SVN_REVISION << QwLog::endl;
320  QwMessage << " * URL: " << QWANA_SVN_URL << QwLog::endl;
321  QwMessage << " * Last Changed Rev: " << QWANA_SVN_LASTCHANGEDREVISION << QwLog::endl;
322  QwMessage << " * ROOT " << root_version << QwLog::endl;
323 }
#define QwMessage
Predefined log drain for regular messages.
Definition: QwLog.h:50
#define QWANA_SVN_REVISION
#define QWANA_SVN_LASTCHANGEDREVISION
static char ** fArgv
Definition: QwOptions.h:310
static std::ostream & endl(std::ostream &)
End of the line.
Definition: QwLog.cc:299
#define QWANA_SVN_URL

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Field Documentation

int QwOptions::fArgc = 0
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().

char ** QwOptions::fArgv = 0
staticprivate

Definition at line 310 of file QwOptions.h.

Referenced by GetArgv(), ParseCommandLine(), SetCommandLine(), Version(), and ~QwOptions().

std::vector<std::string> QwOptions::fConfigFiles
private

Configuration file.

Definition at line 301 of file QwOptions.h.

Referenced by AddConfigFile(), ListConfigFiles(), ParseConfigFile(), QwOptions(), and SetConfigFile().

std::vector<po::options_description*> QwOptions::fOptionBlock
private

Definition at line 316 of file QwOptions.h.

Referenced by AddOptions(), CombineOptions(), Usage(), and ~QwOptions().

std::vector<std::string> QwOptions::fOptionBlockName
private

Definition at line 317 of file QwOptions.h.

Referenced by AddOptions(), and CombineOptions().

bool QwOptions::fParsed
private
po::variables_map QwOptions::fVariablesMap
private

The documentation for this class was generated from the following files: