JAPAn
Just Another Parity Analyzer
Loading...
Searching...
No Matches
QwOptions Class Reference

Command-line and configuration file options processor. More...

#include <QwOptions.h>

+ Collaboration diagram for QwOptions:

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>
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 QwOptionsInstance ()
 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.
 
QwOptionsoperator= (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 QwOptionsfInstance = 0
 

Detailed Description

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.

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 gQwOptions
Definition QwOptions.h:31

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

// 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");
gQwOptions.GetIntValuePair("run").first == gQwOptions.GetIntValuePairFirst("run");
gQwOptions.GetIntValuePair("run").second == gQwOptions.GetIntValuePairLast("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 141 of file QwOptions.h.

Constructor & Destructor Documentation

◆ QwOptions() [1/2]

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

50 : fArgc(0), fArgv(NULL), fParsed(false)
51{
52 // No default config files
53 fConfigFiles.clear();
54
55 // Declare the default options
56 AddDefaultOptions()("usage", "print this help message");
57 AddDefaultOptions()("help,h", "print this help message");
58 AddDefaultOptions()("version,V", "print the version string");
59 AddDefaultOptions()("config,c", po::value<std::string>(), "read ONLY this config file\n(will override default config files)");
60 AddDefaultOptions()("add-config,a", po::value<std::vector<std::string> >()->composing(), "read ALSO this config file\n(will keep the default config files)");
61}
char ** fArgv
Definition QwOptions.h:306
int fArgc
Command line arguments.
Definition QwOptions.h:305
bool fParsed
Definition QwOptions.h:318
po::options_description_easy_init AddDefaultOptions()
Add a default option.
Definition QwOptions.h:165
std::vector< std::string > fConfigFiles
Configuration file.
Definition QwOptions.h:297

References AddDefaultOptions(), fArgc, fArgv, fConfigFiles, and fParsed.

Referenced by DefineOptions(), Instance(), operator=(), and QwOptions().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ QwOptions() [2/2]

QwOptions::QwOptions ( QwOptions const & )
private

Private copy constructor, not implemented.

References QwOptions().

+ Here is the call graph for this function:

◆ ~QwOptions()

QwOptions::~QwOptions ( )
virtual

Default destructor.

Destructor where we clean up locally allocated memory

Definition at line 91 of file QwOptions.cc.

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

References fArgv, and fOptionBlock.

Member Function Documentation

◆ AddConfigFile() [1/2]

void QwOptions::AddConfigFile ( const std::string & configfile)

Add a configuration file.

Add the named configuration file to the list.

Parameters
configfileName of the config file, without path

Definition at line 161 of file QwOptions.cc.

162{
163 Bool_t notfound = kTRUE;
164 for (size_t i = 0; i < fConfigFiles.size(); i++){
165 if (fConfigFiles.at(i) == configfile){
166 notfound=kFALSE;
167 break;
168 }
169 }
170 if (notfound){
171 QwMessage << "Adding user-defined configuration file "
172 << configfile.c_str() << QwLog::endl;
173 fConfigFiles.push_back(configfile);
174 fParsed = false;
175 }
176};
#define QwMessage
Predefined log drain for regular messages.
Definition QwLog.h:49
static std::ostream & endl(std::ostream &)
End of the line.
Definition QwLog.cc:297

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

Referenced by AddConfigFile(), ParseCommandLine(), ParseConfigFile(), and SetCommandLine().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ AddConfigFile() [2/2]

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

Add some configuration files.

Definition at line 204 of file QwOptions.h.

204 {
205 for (size_t i = 0; i < configfiles.size(); i++)
206 AddConfigFile(configfiles.at(i));
207 };
void AddConfigFile(const std::string &configfile)
Add a configuration file.
Definition QwOptions.cc:161

References AddConfigFile().

+ Here is the call graph for this function:

◆ AddDefaultOptions()

po::options_description_easy_init QwOptions::AddDefaultOptions ( )
inline

Add a default option.

Definition at line 165 of file QwOptions.h.

165 {
166 return AddOptions("Default options");
167 };
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:170

References AddOptions().

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

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ AddOptions()

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 170 of file QwOptions.h.

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

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

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Clear()

void QwOptions::Clear ( )
inline

Clear the parsed variables.

Definition at line 277 of file QwOptions.h.

277 {
278 fVariablesMap.clear();
279 fParsed = false;
280 };
po::variables_map fVariablesMap
Definition QwOptions.h:316

References fParsed, and fVariablesMap.

Referenced by AddOptions().

+ Here is the caller graph for this function:

◆ CombineOptions()

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 183 of file QwOptions.cc.

184{
185 // The options can be grouped by defining a vector fOptions of
186 // options_description objects. Each entry can have a name and
187 // will show up as a separate section in the usage information.
188 po::options_description* options = new po::options_description("options");
189 for (size_t i = 0; i < fOptionBlockName.size(); i++) {
190 // Right now every parser gets access to all options
191 options->add(*fOptionBlock.at(i));
192 }
193 return options;
194}

References fOptionBlock, and fOptionBlockName.

Referenced by ParseCommandLine(), ParseConfigFile(), and ParseEnvironment().

+ Here is the caller graph for this function:

◆ DefineOptions()

void QwOptions::DefineOptions ( QwOptions & options)
static

Define the options.

Define standard options on the specified options object

Parameters
optionsOptions object

Definition at line 67 of file QwOptions.cc.

68{
69 // Define logging options (Note: only QwLog takes a pointer argument!!!)
70 QwLog::DefineOptions(&options);
71
72 // Define execution options
74#ifdef __USE_DATABASE__
75 // Define database options
77#endif //__USE_DATABASE__
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)
Defines available class options for QwOptions.
static void DefineOptions(QwOptions &options)
Define the configuration options.
static void DefineOptions(QwOptions &options)
static void DefineOptions(QwOptions &options)
Define the configuration options.
static void DefineOptions(QwOptions *options)
Define available class options for QwOptions.
Definition QwLog.cc:71
static void DefineOptions(QwOptions &options)
Define the configuration options.
static void DefineOptions(QwOptions &options)
Define configuration options for global array.

References QwDatabase::DefineOptions(), QwEPICSEvent::DefineOptions(), QwEventBuffer::DefineOptions(), QwHistogramHelper::DefineOptions(), QwLog::DefineOptions(), QwRootFile::DefineOptions(), QwSubsystemArray::DefineOptions(), and QwOptions().

Referenced by DefineOptionsParity().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ GetArgc()

int QwOptions::GetArgc ( )
inline

Get the number of command line arguments.

Definition at line 272 of file QwOptions.h.

272{ return (int) fArgc; };

References fArgc.

◆ GetArgv()

char ** QwOptions::GetArgv ( )
inline

Get the vector of command line arguments.

Definition at line 274 of file QwOptions.h.

274{ return (char**) fArgv; };

References fArgv.

◆ GetIntValuePair()

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 357 of file QwOptions.cc.

358{
359 std::pair<int, int> mypair;
360 mypair.first = 0;
361 mypair.second = 0;
362
363 if (fParsed == false) Parse();
364 if (fVariablesMap.count(key)) {
365 std::string range = fVariablesMap[key].as<std::string>();
366 mypair = QwParameterFile::ParseIntRange(":",range);
367 }
368
369 return mypair;
370}
void Parse(bool force=false)
Parse all sources of options.
Definition QwOptions.h:218
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.

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

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

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ GetIntValuePairFirst()

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

Get the first of a pair of integer values.

Definition at line 262 of file QwOptions.h.

262 {
263 return GetIntValuePair(key).first;
264 };
std::pair< int, int > GetIntValuePair(const std::string &key)
Get a pair of integer values.
Definition QwOptions.cc:357

References GetIntValuePair().

+ Here is the call graph for this function:

◆ GetIntValuePairLast()

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

Get the last of a pair of integer values.

Definition at line 267 of file QwOptions.h.

267 {
268 return GetIntValuePair(key).second;
269 };

References GetIntValuePair().

+ Here is the call graph for this function:

◆ GetValue()

template<class T>
T QwOptions::GetValue ( const std::string & key)
inline

Get a templated value.

Definition at line 236 of file QwOptions.h.

236 {
237 if (fParsed == false) Parse();
238 if (fVariablesMap.count(key)) {
239 QwVerbose << "Option " << key << ": "
240 << fVariablesMap[key].as<T>() << QwLog::endl;
241 return fVariablesMap[key].as<T>();
242 } else {
243 QwError << "Variable " << key << " unknown" << QwLog::endl;
244 return {};
245 }
246 }
#define QwVerbose
Predefined log drain for verbose messages.
Definition QwLog.h:54
#define QwError
Predefined log drain for errors.
Definition QwLog.h:39

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

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ GetValueVector()

template<class T>
std::vector< T > QwOptions::GetValueVector ( const std::string & key)
inline

Get a list of templated values.

Definition at line 249 of file QwOptions.h.

249 {
250 std::vector<T> list;
251 if (fParsed == false) Parse();
252 if (fVariablesMap.count(key)) {
253 list = fVariablesMap[key].as< std::vector<T> >();
254 }
255 return list;
256 }

References fParsed, fVariablesMap, and Parse().

Referenced by QwDataHandlerArray::ProcessOptions(), QwLog::ProcessOptions(), QwRootFile::ProcessOptions(), and QwSubsystemArray::ProcessOptionsToplevel().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ HasValue()

bool QwOptions::HasValue ( const std::string & key)
inline

Has this key been defined.

Definition at line 229 of file QwOptions.h.

229 {
230 if (fParsed == false) Parse();
231 return (fVariablesMap.count(key) > 0);
232 };

References fParsed, fVariablesMap, and Parse().

Referenced by QwDatabase::ProcessOptions(), QwDataHandlerArray::ProcessOptions(), QwEventBuffer::ProcessOptions(), QwHelicity::ProcessOptions(), QwHistogramHelper::ProcessOptions(), and QwLog::ProcessOptions().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Instance()

static QwOptions & QwOptions::Instance ( )
inlinestatic

Get instance.

Definition at line 155 of file QwOptions.h.

155 {
156 if (! fInstance) fInstance = new QwOptions();
157 return *fInstance;
158 }
QwOptions()
Private default constructor.
Definition QwOptions.cc:49
static QwOptions * fInstance
Definition QwOptions.h:144

References fInstance, and QwOptions().

+ Here is the call graph for this function:

◆ ListConfigFiles()

void QwOptions::ListConfigFiles ( )
inline

List the configuration files.

Definition at line 210 of file QwOptions.h.

210 {
211 QwMessage << "Config files:" << QwLog::endl;
212 for (size_t i = 0; i < fConfigFiles.size(); i++)
214 };

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

+ Here is the call graph for this function:

◆ operator=()

QwOptions & QwOptions::operator= ( QwOptions const & )
private

Private assignment operator, not implemented.

References QwOptions().

+ Here is the call graph for this function:

◆ Parse()

void QwOptions::Parse ( bool force = false)
inline

Parse all sources of options.

Definition at line 218 of file QwOptions.h.

218 {
219 if (! fParsed || force) {
223 fParsed = true;
224 }
225 };
void ParseCommandLine()
Parse the command line arguments.
Definition QwOptions.cc:201
void ParseConfigFile()
Parse the configuration file.
Definition QwOptions.cc:278
void ParseEnvironment()
Parse the environment variables.
Definition QwOptions.cc:242

References fParsed, ParseCommandLine(), ParseConfigFile(), and ParseEnvironment().

Referenced by GetIntValuePair(), GetValue(), GetValueVector(), and HasValue().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ ParseCommandLine()

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 201 of file QwOptions.cc.

202{
203 // Boost versions starting with 1.33.00 allow unrecognized options to be
204 // passed through the parser.
205 try {
206 po::options_description* command_line_options = CombineOptions();
207 po::store(po::command_line_parser(fArgc, fArgv).options(*command_line_options).allow_unregistered().run(), fVariablesMap);
208 delete command_line_options;
209 } catch (std::exception const& e) {
210 QwWarning << e.what() << " while parsing command line arguments" << QwLog::endl;
211 exit(10);
212 }
213
214 // Notify of new options
215 po::notify(fVariablesMap);
216
217 // If option help/usage, print help text
218 if (fVariablesMap.count("help") || fVariablesMap.count("usage")) {
219 Usage();
220 exit(0);
221 }
222
223 // If option version, print version string
224 if (fVariablesMap.count("version")) {
225 Version();
226 exit(0);
227 }
228
229 // If a configuration file is specified, load it.
230 if (fVariablesMap.count("config") > 0) {
231 SetConfigFile(fVariablesMap["config"].as<std::string>());
232 }
233 // If a configuration file is specified, load it.
234 if (fVariablesMap.count("add-config") > 0) {
235 AddConfigFile(fVariablesMap["add-config"].as<std::vector<std::string> >());
236 }
237}
#define QwWarning
Predefined log drain for warnings.
Definition QwLog.h:44
void SetConfigFile(const std::string &configfile)
Set a configuration file.
Definition QwOptions.cc:147
po::options_description * CombineOptions()
Combine the various option description in one.
Definition QwOptions.cc:183
void Usage()
Print usage information.
Definition QwOptions.cc:312
void Version()
Print version string.
Definition QwOptions.cc:325

References AddConfigFile(), CombineOptions(), QwLog::endl(), fArgc, fArgv, fVariablesMap, QwWarning, SetConfigFile(), Usage(), and Version().

Referenced by Parse().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ ParseConfigFile()

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 278 of file QwOptions.cc.

279{
280 for (size_t i = 0; i < fConfigFiles.size(); i++) {
281 QwParameterFile configfile(fConfigFiles.at(i).c_str());
282 std::stringstream configstream;
283 configstream << configfile.rdbuf();
284
285 try {
286 // Boost version after 1.35 have bool allow_unregistered = false in
287 // their signature. This allows for unknown options in the config file.
288 po::options_description* config_file_options = CombineOptions();
289 po::store(po::parse_config_file(configstream, *config_file_options, true),
291 delete config_file_options;
292 } catch (std::exception const& e) {
293 QwWarning << e.what() << " while parsing configuration file "
294 << fConfigFiles.at(i) << QwLog::endl;
295 exit(10);
296 }
297 // Notify of new options
298 po::notify(fVariablesMap);
299
300 // If a configuration file is specified, load it.
301 if (fVariablesMap.count("add-config") > 0) {
302 AddConfigFile(fVariablesMap["add-config"].as<std::vector<std::string> >());
303 }
304 }
305}

References AddConfigFile(), CombineOptions(), QwLog::endl(), fConfigFiles, fVariablesMap, QwWarning, and QwParameterFile::rdbuf().

Referenced by Parse().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ ParseEnvironment()

void QwOptions::ParseEnvironment ( )
private

Parse the environment variables.

Parse the environment variables for options and notify the variables map.

Definition at line 242 of file QwOptions.cc.

243{
244 class name_mapper {
245 public:
246 name_mapper(const std::string& prefix, const std::string& ignore)
247 : prefix(prefix),ignore(ignore) { }
248 std::string operator()(const std::string& s) {
249 string lc;
250 if (s.find(prefix) == 0) {
251 for(string::size_type n = prefix.size(); n < s.size(); ++n) {
252 lc += static_cast<char>(tolower(s[n]));
253 }
254 }
255 if (ignore.find(lc) == std::string::npos) return lc;
256 else return "";
257 }
258 private:
259 std::string prefix, ignore;
260 } qw_name_mapper("QW_", "bin fieldmap lib lookup prminput searchtree tmp");
261
262 try {
263 po::options_description* environment_options = CombineOptions();
264 po::store(po::parse_environment(*environment_options, qw_name_mapper), fVariablesMap);
265 delete environment_options;
266 } catch (std::exception const& e) {
267 QwWarning << e.what() << " while parsing environment variables" << QwLog::endl;
268 exit(10);
269 }
270 // Notify of new options
271 po::notify(fVariablesMap);
272}

References CombineOptions(), QwLog::endl(), fVariablesMap, and QwWarning.

Referenced by Parse().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ SetCommandLine()

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.

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 }
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}
#define QwDebug
Predefined log drain for debugging output.
Definition QwLog.h:59

References AddConfigFile(), QwLog::endl(), fArgc, fArgv, fParsed, and QwDebug.

+ Here is the call graph for this function:

◆ SetConfigFile()

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.

Parameters
configfileName of the config file, without path

Definition at line 147 of file QwOptions.cc.

148{
149 QwWarning << "Overriding the default configuration files with "
150 << "user-defined configuration file "
151 << configfile.c_str() << QwLog::endl;
152 fConfigFiles.clear();
153 fConfigFiles.push_back(configfile);
154 fParsed = false;
155};

References QwLog::endl(), fConfigFiles, fParsed, and QwWarning.

Referenced by ParseCommandLine().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Usage()

void QwOptions::Usage ( )

Print usage information.

Print usage information

Definition at line 312 of file QwOptions.cc.

313{
315 QwMessage << "Welcome to the Qweak analyzer code." << QwLog::endl;
317 for (size_t i = 0; i < fOptionBlock.size(); i++)
318 QwMessage << *(fOptionBlock.at(i)) << QwLog::endl;
319}

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

Referenced by ParseCommandLine().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Version()

void QwOptions::Version ( )

Print version string.

Print version string

Definition at line 325 of file QwOptions.cc.

326{
327 TString root_version = gROOT->GetVersion();
328 root_version += ", Date : ";
329 root_version += gROOT->GetVersionDate();
330#if ROOT_VERSION_CODE < ROOT_VERSION(6,0,0)
331 root_version += ", SVN : ";
332 root_version += gROOT->GetSvnRevision();
333 root_version += " ";
334 root_version += gROOT->GetSvnBranch();
335#else // ROOT_VERSION_CODE >= ROOT_VERSION(6,0,0)
336 root_version += ", GIT : ";
337 root_version += gROOT->GetGitCommit();
338 root_version += " ";
339 root_version += gROOT->GetGitBranch();
340#endif
341
342 QwMessage << "\n Qweak Analysis Framework : " << fArgv[0] << QwLog::endl;
343 QwMessage << " * GIT info: " << gGitInfo << QwLog::endl;
344 // QwMessage << " * Revision: " << QWANA_SVN_REVISION << QwLog::endl;
345 // QwMessage << " * URL: " << QWANA_SVN_URL << QwLog::endl;
346 // QwMessage << " * Last Changed Rev: " << QWANA_SVN_LASTCHANGEDREVISION << QwLog::endl;
347 QwMessage << " * ROOT " << root_version << QwLog::endl;
348}
const char *const gGitInfo

References QwLog::endl(), fArgv, gGitInfo, and QwMessage.

Referenced by ParseCommandLine().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Field Documentation

◆ fArgc

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

◆ fArgv

char** QwOptions::fArgv
private

Definition at line 306 of file QwOptions.h.

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

◆ fConfigFiles

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

Configuration file.

Definition at line 297 of file QwOptions.h.

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

◆ fInstance

QwOptions * QwOptions::fInstance = 0
staticprivate

Definition at line 144 of file QwOptions.h.

Referenced by Instance().

◆ fOptionBlock

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

Definition at line 312 of file QwOptions.h.

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

◆ fOptionBlockName

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

Definition at line 313 of file QwOptions.h.

Referenced by AddOptions(), and CombineOptions().

◆ fParsed

bool QwOptions::fParsed
private

◆ fVariablesMap

po::variables_map QwOptions::fVariablesMap
private

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