13 #ifndef FFA_CMD_LINE_ARG_H
14 #define FFA_CMD_LINE_ARG_H
107 std::stringstream sval;
125 typedef std::map<std::string,FFaCmdLineEntryBase*>
OptionMap;
148 template<
class T>
void addOption(
const std::string& identifier,
150 const std::string& helpText,
151 bool showToAll =
true)
154 OptionMap::iterator it =
myOptions.find(identifier);
155 if (it !=
myOptions.end())
delete it->second;
167 const std::string& helpText,
168 bool showToAll =
true)
170 this->
addOption(identifier,std::string(value),helpText,showToAll);
177 template<
class T>
bool getValue(
const std::string& identifier, T& aVal)
181 OptionMap::const_iterator opIt =
myOptions.find(identifier);
193 std::cerr <<
" *** Could not evaluate command-line option \""
194 << identifier <<
"\""<< std::endl;
202 bool setValue(
const std::string& identifier,
const std::string& value);
225 void initArgs(
int argc,
char** argv);
251 return !myValue.empty();
261 return !myDefault.empty();
273 return !myDefault.empty();
285 return !myDefault.empty();
306 if (noDefaults && myValue == myDefault)
307 return std::string();
320 return std::string(myDefault ?
"+ (true)" :
"- (false)");
331 if (noDefaults && myValue == myDefault)
332 return std::string();
334 return std::string(myValue ?
"+ (true)" :
"- (false)");
349 long int aval = strtol(value.c_str(),&endPtr,10);
350 if (*endPtr)
return 1 + (endPtr - value.c_str());
353 iAmSetOnCmdLine =
true;
369 float aval = strtof(value.c_str(),&endPtr);
370 if (*endPtr)
return 1 + (endPtr - value.c_str());
373 iAmSetOnCmdLine =
true;
389 double aval = strtod(value.c_str(),&endPtr);
390 if (*endPtr)
return 1 + (endPtr - value.c_str());
393 iAmSetOnCmdLine =
true;
410 long int aval = strtol(value.c_str(),&endPtr,10);
414 if (!isdigit(next[0]) && !isspace(next[0]) &&
415 next[0] !=
'-' && next[0] !=
'+')
416 return 1 + (endPtr - value.c_str());
418 myValue.push_back((
int)aval);
419 aval = strtol(next,&endPtr,10);
422 myValue.push_back(aval);
423 iAmSetOnCmdLine =
true;
440 double aval = strtod(value.c_str(),&endPtr);
444 if (!isdigit(next[0]) && !isspace(next[0]) &&
445 next[0] !=
'-' && next[0] !=
'+' && next[0] !=
'.')
446 return 1 + (endPtr - value.c_str());
448 myValue.push_back(aval);
449 aval = strtod(next,&endPtr);
452 myValue.push_back(aval);
453 iAmSetOnCmdLine =
true;
462 template<
class T>
inline
468 std::stringstream sval;
469 sval << value.front();
470 for (
size_t i = 1; i < value.size(); i++)
471 sval <<
" "<< value[i];
508 if (myValue.size() > 1)
511 if (*myValue.begin() ==
'"') myValue.erase(myValue.begin());
512 if (*myValue.rbegin() ==
'"') myValue.erase(myValue.end()-1);
514 iAmSetOnCmdLine =
true;
528 else if (value.size() != 1)
530 else if (value ==
"+")
532 else if (value ==
"-")
537 iAmSetOnCmdLine =
true;
546 template<
class T>
inline
548 const std::vector<T>& defval)
552 else if (value.size() >= defval.size())
555 static std::vector<T> tmp;
556 tmp.assign(value.begin(),value.end());
557 tmp.insert(tmp.end(),defval.begin()+value.size(),defval.end());
std::vector< double > DoubleVec
Convenience type definition.
Definition: FFaCmdLineArg.H:241
std::vector< int > IntVec
Convenience type definition.
Definition: FFaCmdLineArg.H:240
std::string vec2string(const std::vector< T > &value)
Helper function converting a vector of something to a string.
Definition: FFaCmdLineArg.H:463
const std::vector< T > & complDefault(const std::vector< T > &value, const std::vector< T > &defval)
Helper function completing a vector-valued option value from defaults.
Definition: FFaCmdLineArg.H:547
Singleton class for management of command-line options.
Definition: FFaCmdLineArg.H:123
bool readOptionsFile(const std::string &fileName)
Reads command-line option values from the file fileName.
Definition: FFaCmdLineArgImplementation.C:334
static std::string additionalHelpText
This text is printed after the option list itself when using "-help".
Definition: FFaCmdLineArg.H:219
static FFaCmdLineArg * instance()
Returns the one and only instance of this class.
Definition: FFaCmdLineArgImplementation.C:89
std::vector< std::string > myArgs
Temporary command-line option storage.
Definition: FFaCmdLineArg.H:229
static void init(int argc, char **argv)
Initializes the command-line options singleton object.
Definition: FFaCmdLineArg.H:135
static bool empty()
Returns true if no command-line options have been defined.
Definition: FFaCmdLineArgImplementation.C:83
bool setValue(const std::string &identifier, const std::string &value)
Assigns a new value to the specified command-line option.
Definition: FFaCmdLineArgImplementation.C:115
OptionMap myOptions
Command-line options container.
Definition: FFaCmdLineArg.H:230
static void removeInstance()
Deletes the dynamically allocated instance.
Definition: FFaCmdLineArgImplementation.C:98
~FFaCmdLineArg()
The destructor deletes the allocated FFaCmdLineEntry objects.
Definition: FFaCmdLineArgImplementation.C:73
static bool mute
If true, error messages are muted.
Definition: FFaCmdLineArg.H:221
static FFaCmdLineArg * myInstance
The actual instance of this class.
Definition: FFaCmdLineArg.H:232
void addOption(const std::string &identifier, const T &value, const std::string &helpText, bool showToAll=true)
Defines a new command-line option of arbitrary type.
Definition: FFaCmdLineArg.H:148
std::map< std::string, FFaCmdLineEntryBase * > OptionMap
Data type for the command-line options container.
Definition: FFaCmdLineArg.H:125
FFaCmdLineArg()
The default constructor is private to avoid other instances.
Definition: FFaCmdLineArgImplementation.C:65
void listOptions(bool noDefaults=false) const
Prints out all specified command-line options.
Definition: FFaCmdLineArgImplementation.C:288
void composeSingleLineHelpText(std::string &report, bool all=false) const
Same as composeHelpText(), but with each option as a single line.
Definition: FFaCmdLineArgImplementation.C:251
bool isOptionSetOnCmdLine(const std::string &identifier)
Checks if an option identifier is specified on the command-line.
Definition: FFaCmdLineArgImplementation.C:319
void addOption(const std::string &identifier, const char *value, const std::string &helpText, bool showToAll=true)
Defines a new command-line option of string type.
Definition: FFaCmdLineArg.H:165
void initArgs(int argc, char **argv)
Copies all command-line arguments into a temporary container.
Definition: FFaCmdLineArgImplementation.C:105
bool getValue(const std::string &identifier, T &aVal)
Returns the actual value of the specified command-line option.
Definition: FFaCmdLineArg.H:177
void evaluate()
Processes the temporary command-line option list myArgs.
Definition: FFaCmdLineArgImplementation.C:134
void composeHelpText(std::string &report, bool all=false) const
Creates a multi-line string with a list of all defined options.
Definition: FFaCmdLineArgImplementation.C:208
Abstract interface for command-line options.
Definition: FFaCmdLineArg.H:30
virtual void reset()=0
Resets the option to its default value.
virtual bool hasValue() const =0
Checks if this option has been assigned a non-empty value.
bool iAmSetOnCmdLine
true if this option has been specified
Definition: FFaCmdLineArg.H:54
virtual std::string getValueString(bool=false) const =0
Returns a string representation of the command-line option value.
virtual int convertOption(const std::string &)=0
Converts the command-line option value from the given string.
virtual bool hasDefault() const =0
Checks if this option has been assigned a non-empty default value.
std::string myHelpText
Text description of this command-line option.
Definition: FFaCmdLineArg.H:53
virtual ~FFaCmdLineEntryBase()
Empty destructor.
Definition: FFaCmdLineArg.H:38
FFaCmdLineEntryBase(const std::string &text, bool showToAll)
The constructor initializes the class attributes.
Definition: FFaCmdLineArg.H:33
bool isPublic
true unless this option is hidden
Definition: FFaCmdLineArg.H:55
virtual std::string getDefaultString() const =0
Composes the default section of the help text for this option.
Template class for command-line options of arbitrary value type.
Definition: FFaCmdLineArg.H:66
virtual void reset()
Resets the option to its default value.
Definition: FFaCmdLineArg.H:101
virtual bool hasValue() const
Checks if this option has been assigned a non-empty value.
Definition: FFaCmdLineArg.H:75
virtual std::string getValueString(bool noDefaults) const
Returns a string representation of the command-line option value.
Definition: FFaCmdLineArg.H:89
T myValue
The assigned value of this command-line option.
Definition: FFaCmdLineArg.H:113
FFaCmdLineEntry(const std::string &text, const T &value, bool showToAll)
The constructor initializes the class attributes and option values.
Definition: FFaCmdLineArg.H:69
virtual ~FFaCmdLineEntry()
Empty destructor.
Definition: FFaCmdLineArg.H:72
virtual std::string getDefaultString() const
Composes the default section of the help text for this option.
Definition: FFaCmdLineArg.H:83
const T & getValue() const
Returns the actual command-line option value.
Definition: FFaCmdLineArg.H:98
virtual int convertOption(const std::string &)
Converts the command-line option value from the given string.
Definition: FFaCmdLineArg.H:80
virtual std::string toString(const T &value) const
Returns the string representation of the specified value.
Definition: FFaCmdLineArg.H:105
T myDefault
The default value of this command-line option.
Definition: FFaCmdLineArg.H:114
virtual bool hasDefault() const
Checks if this option has been assigned a non-empty default value.
Definition: FFaCmdLineArg.H:77