bpp-core3  3.0.0
bpp::AttributesTools Class Reference

Some functions to deal with attributes, i.e. parameters passed to a program/method. More...

#include <Bpp/Utils/AttributesTools.h>

+ Collaboration diagram for bpp::AttributesTools:

Public Member Functions

 AttributesTools ()
 
virtual ~AttributesTools ()
 

Static Public Member Functions

static std::vector< std::string > getVector (int argc, char *argv[])
 Get attributes a vector of strings from command line arguments. More...
 
static std::map< std::string, std::string > getAttributesMap (const std::vector< std::string > &argv, const std::string &delimiter="=")
 Get an attribute map from a vector of arguments. More...
 
static void getAttributesMap (const std::vector< std::string > &argv, std::map< std::string, std::string > &am, const std::string &delimiter="=")
 Get an attribute map from a vector of arguments. More...
 
static std::map< std::string, std::string > getAttributesMapFromFile (const std::string &file, const std::string &delimiter)
 Get an attribute map from a file. More...
 
static void getAttributesMapFromFile (const std::string &file, std::map< std::string, std::string > &params, const std::string &delimiter)
 Get an attribute map from a file. More...
 
static void actualizeAttributesMap (std::map< std::string, std::string > &attMap, const std::map< std::string, std::string > &atts, bool replace=true)
 Actualizes an attribute map with another. More...
 
static void resolveVariables (std::map< std::string, std::string > &am, char varCode='$', char varBeg='(', char varEnd=')')
 Resolve the variables. More...
 
static std::map< std::string, std::string > parseOptions (int args, char **argv)
 Global function that reads all parameters from command line and files, and set the values in a map. More...
 

Static Private Member Functions

static std::string removeComments (const std::string &s, const std::string &begin, const std::string &end)
 Remove comments from a string. More...
 

Static Private Attributes

static std::vector< std::string > vParam_
 

Detailed Description

Some functions to deal with attributes, i.e. parameters passed to a program/method.

These methods allows you to retrieve attributes from command line arguments or from a file. The underlying syntax is attributeName = argument . Here the delimiter char is '=', but another character may be used.

In files, shell comments: # my comment line here , C comments: / * my comment block here * / (but multiline not supported!) and C++ comments: // my comment line here are allowed, and ignored while parsing.

Lines may be broken, using the bash character '\' at the end of the line:

optionfile=/home/foo/\
bar.txt

will be read as

optionfile=/home/foo/bar.txt

Attributes are stored as a map<string, string>, with attributes names as keys, and arguments as values.

Here is an example of use. This piece of code typically is at the begining of the main function. It uses the FileTools and ApplicationTools classes, for checking file existence and displaying messages respectively.

// Get the parameters from command line:
map<string, string> cmdParams = AttributesTools::getAttributesMap(
AttributesTools::getVector(argc, argv), "=");
// Look for a specified file with parameters:
int main(int argc, char *argv[]) {
map<string, string> params;
if(cmdParams.find("param") != cmdParams.end()) {
string file = cmdParams["param"];
if(!FileTools::fileExists(file)) {
ApplicationTools::displayError("Parameter file not found.");
exit(-1);
} else {
// Actualize attributes with the ones passed to command line:
}
} else {
params = cmdParams;
}
...

These pieces of code does the following:

  • get all parameters from the command line and store them in a map,
  • check if some parameter is called 'param' or 'params'. If so, look for the file given as value and try to read some parameters from it.
  • If an parameter file was found, update the parameter in it with those from the command line. This implies that when a parameter is found in both the command line and the option file, the value from the command line will be retained.

Support for variable is also available. A variable character is specified (typically '$()') and may be used to de-reference any argument. For instance,

data=LSU
file=$(data).out

will be equivalent to

data=LSU
file=LSU.out

Definition at line 93 of file AttributesTools.h.

Constructor & Destructor Documentation

◆ AttributesTools()

bpp::AttributesTools::AttributesTools ( )
inline

Definition at line 104 of file AttributesTools.h.

◆ ~AttributesTools()

virtual bpp::AttributesTools::~AttributesTools ( )
inlinevirtual

Member Function Documentation

◆ actualizeAttributesMap()

void AttributesTools::actualizeAttributesMap ( std::map< std::string, std::string > &  attMap,
const std::map< std::string, std::string > &  atts,
bool  replace = true 
)
static

Actualizes an attribute map with another.

All fields in map 2 will be added to map 1. If two attributes have the same name, then map 1 value will be overwritten.

Parameters
attMapThe attributes map.
attsThe attributes to add to the map.
replaceif existing params of attMap should be replaced

Definition at line 121 of file AttributesTools.cpp.

Referenced by ~AttributesTools().

◆ getAttributesMap() [1/2]

std::map< std::string, std::string > AttributesTools::getAttributesMap ( const std::vector< std::string > &  argv,
const std::string &  delimiter = "=" 
)
static

Get an attribute map from a vector of arguments.

This method also resolve all variable calls.

Parameters
argvThe vector of arguments.
delimiterThe string that separates attribute names from arguments (for instance '=').
Returns
The attribute map.

Definition at line 40 of file AttributesTools.cpp.

Referenced by ~AttributesTools().

◆ getAttributesMap() [2/2]

void AttributesTools::getAttributesMap ( const std::vector< std::string > &  argv,
std::map< std::string, std::string > &  am,
const std::string &  delimiter = "=" 
)
static

Get an attribute map from a vector of arguments.

This method also resolve all variable calls.

Parameters
argvThe vector of arguments.
amThe attribute map to fill.
delimiterThe string that separates attribute names from arguments (for instance '=').

Definition at line 51 of file AttributesTools.cpp.

References bpp::TextTools::removeWhiteSpaces().

◆ getAttributesMapFromFile() [1/2]

std::map< std::string, std::string > AttributesTools::getAttributesMapFromFile ( const std::string &  file,
const std::string &  delimiter 
)
static

Get an attribute map from a file.

Parameters
fileThe file with all arguments.
delimiterThe string that separates attribute names from arguments (for instance '=').
Returns
An attribute map.

Definition at line 110 of file AttributesTools.cpp.

Referenced by ~AttributesTools().

◆ getAttributesMapFromFile() [2/2]

void AttributesTools::getAttributesMapFromFile ( const std::string &  file,
std::map< std::string, std::string > &  params,
const std::string &  delimiter 
)
static

Get an attribute map from a file.

Parameters
fileThe file with all arguments.
paramsAn attribute map to fill.
delimiterThe string that separates attribute names from arguments (for instance '=').

Definition at line 97 of file AttributesTools.cpp.

◆ getVector()

std::vector< std::string > AttributesTools::getVector ( int  argc,
char *  argv[] 
)
static

Get attributes a vector of strings from command line arguments.

Parameters
argcThe number of arguments.
argvThe array with all arguments.
Returns
A vector with all arguments as strings.

Definition at line 26 of file AttributesTools.cpp.

Referenced by ~AttributesTools().

◆ parseOptions()

std::map< std::string, std::string > AttributesTools::parseOptions ( int  args,
char **  argv 
)
static

Global function that reads all parameters from command line and files, and set the values in a map.

Parameters
argsNumber of arguments, as passed to the main function.
argvArray of values, as passed to the main function.
Returns
An attributes map.
Exceptions
Exceptionin case an option file is not found.

Definition at line 216 of file AttributesTools.cpp.

References bpp::StringTokenizer::hasMoreToken(), and bpp::StringTokenizer::nextToken().

Referenced by bpp::BppApplication::BppApplication(), and ~AttributesTools().

◆ removeComments()

std::string AttributesTools::removeComments ( const std::string &  s,
const std::string &  begin,
const std::string &  end 
)
staticprivate

Remove comments from a string.

Parameters
sThe string to parse.
beginComments front delimiter.
endComments end delimiter.

Definition at line 187 of file AttributesTools.cpp.

Referenced by ~AttributesTools().

◆ resolveVariables()

void AttributesTools::resolveVariables ( std::map< std::string, std::string > &  am,
char  varCode = '$',
char  varBeg = '(',
char  varEnd = ')' 
)
static

Resolve the variables.

If used prior to the actualizeAttributesMap, this function will make the variables 'local', whereas using them after will make them 'global'.

Parameters
amThe attributes map.
varCodeThe code that defines variable recalls.
varBegVariables begin name code.
varEndVariables end name code.
Exceptions
ExceptionIf there is a syntax error.

Definition at line 135 of file AttributesTools.cpp.

References bpp::TextTools::toString().

Referenced by ~AttributesTools().

Member Data Documentation

◆ vParam_

std::vector< std::string > AttributesTools::vParam_
staticprivate

Definition at line 101 of file AttributesTools.h.


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