Argument Parser.
More...
Argument Parser.
- Author
- Thomas Eriksson and Pal Frenger (Thanks to Svante Signell for valuable input), modifications by Johan Bergman
This class parses strings to variables. The syntax is compatible with Matlab and Octave. It can be used in several different ways. The following test program and test data file gives several examples:
-
Use the Parser class on a parameter file
-
Use the Parser class on the command line input
-
Use the Parser class on a char pointer (usually the command line input)
-
Use the Parser class on a parameter file and a char pointer
-
Use the Parser class on an Array of strings
-
Use the Parser::get() method on a parameter file
-
Use the Parser::exist() method to check if variables exist
The test program looks as follows:
#ifndef PARSER_TEST_FILE
{
cerr <<
"PARSER_TEST_FILE not defined. Test skipped." << endl;
return 1;
}
#else
{
double b;
ivec d;
vec e;
svec f;
bvec g;
bool l, m;
string n;
cout <<
"Use the Parser class on a parameter file:" <<
boolalpha << endl;
cout <<
"a = " <<
a << endl;
cout <<
"c = " <<
c << endl;
cout << "d = " << d << endl;
cout << "e = " << e << endl;
cout << "f = " << f << endl;
cout << "g = " << g << endl;
cout <<
"h = " <<
h << endl;
cout <<
"i = " <<
i << endl;
cout <<
"j = " <<
j << endl;
cout << "k = " << k << endl;
cout << "l = " << l << endl;
cout << "m = " << m << endl;
cout << "n = " << n << endl;
cout << "------------------------------------------------------------" << endl;
cout << "Use the Parser class on the command line input:" << endl;
cout << "Example: > ./parser_test.run \"a=3; b=-1.2; d=[0 1 2 45];\"" << endl << endl;
cout << "Use the following code:" << endl;
cout << " p.init(argc,argv);" << endl;
cout << " a = p.get_int(\"a\"); cout << \"a = \" << a << endl;" << endl;
cout << " b = p.get_double(\"b\"); cout << \"b = \" << b << endl;" << endl;
cout << " d = p.get_ivec(\"d\"); cout << \"d = \" << d << endl;" << endl;
cout << "------------------------------------------------------------" << endl;
cout << "Use the Parser class on a char pointer (usually the command line input):" << endl;
char argv1_2[] =
"c=\"Hello Bird\"";
char argv1_3[] =
"d=[1,2,3,-1,-2,-3]";
cout << "------------------------------------------------------------" << endl;
cout << "Use the Parser class on a parameter file and a char pointer:" << endl;
char argv2_1[] =
"d=[1,-2,3,-4,5,-6,7,-8]";
}
else {
}
cout <<
"a = " <<
a << endl;
cout << "b = " << b << endl;
cout <<
"c = " <<
c << endl;
cout << "d = " << d << endl;
cout << "------------------------------------------------------------" << endl;
cout << "Use the Parser class on an Array of strings:" << endl;
cout << "------------------------------------------------------------" << endl;
cout << "Use the Parser::get() method on a parameter file:" << endl;
set_array(q, "{{[1][2 3][4 5 6]}}");
bool r = true;
int s = 7;
double t = 3.14;
std::string u = "test string";
cout << "------------------------------------------------------------" << endl;
cout << "Check if variables exist in the Parser:" << endl;
cout <<
"p.exist(\"a\") = " << p.
exist(
"a") << endl;
cout <<
"p.exist(\"aa\") = " << p.
exist(
"aa") << endl;
cout << "------------------------------------------------------------" << endl;
return 0;
}
#endif
double get_double(const std::string &name, int num=-1)
Interpret variable name as a double.
svec get_svec(const std::string &name, int num=-1)
Interpret variable name as a svec.
mat get_mat(const std::string &name, int num=-1)
Interpret variable name as a mat.
int get_int(const std::string &name, int num=-1)
Interpret variable name as an integer.
ivec get_ivec(const std::string &name, int num=-1)
Interpret variable name as a ivec.
bvec get_bvec(const std::string &name, int num=-1)
Interpret variable name as a bvec.
imat get_imat(const std::string &name, int num=-1)
Interpret variable name as a imat.
bool get_bool(const std::string &name, int num=-1)
Interpret variable name as a bool.
void init(const std::string &filename)
Initialization function. Sets input file name.
smat get_smat(const std::string &name, int num=-1)
Interpret variable name as a smat.
std::string get_string(const std::string &name, int num=-1)
Interpret variable name as a string.
bool get(T &var, const std::string &name, int num=-1)
Get variable value if name can be found (and return true), otherwise keep old value (and return false...
bool exist(const std::string &name)
Check is name exists in the file. Returns true if the name is found and false otherwise.
vec get_vec(const std::string &name, int num=-1)
Interpret variable name as a vec.
bmat get_bmat(const std::string &name, int num=-1)
Interpret variable name as a bmat.
Include file for the IT++ base module.
Mat< bin > bmat
bin matrix
The data file parser_test_data.txt
looks as follows:
%---------------------------------------------------------------------
% Test data file for the parser_test.cpp program
%---------------------------------------------------------------------
% Lines that begins with % are ignored. Expressions that ends with ';'
% are not printed while parsing.
a = 4; %Comments may be put at the end of each line
% You can give several values for the same parameter name. Each
% alternative value must be on a new row. Select the value you
% want with the num parameter in the get_double member function.
% Use:
% Parser p("filename.txt");
% double b = p.get_double("b",1);
%
to select the value 2.36 below.
b = 2.35
2.36
2.37
% Several expressions may be put on the same row (separated by , or ;)
% c is a string, d is an integer vector. Spaces and/or commas separate
% the vector elements.
c = "Hello World"; d =[1,2,3 4,3 2, -1];
% This a vector.
e=[1.2,2,3.33 4.01,3.2 2, -1.2];
% This is a short vector.
f=[1,2,3 4,3 2, -1];
% This is a binary vector.
g=[0 1 0 0 1];
% This an integer matrix. Spaces and/or commas separate the colums. Semicolons separate rows.
h=[1 2 3;4 5 6];
% Expressions can continue over several rows by inserting '...' at the end of the rows
% (as in Matlab). This is a matrix.
i=[...
1.0, 2 ; ...
-3 4.2 ...
] ;
% This is a short matrix.
j=[1 -2 -3;4 -5 6];
% This is a binary matrix.
k=[0 1 0 1;1 0 1 0;0 0 0 0;1 1 1 1];
% These are boolean variables:
l= 0;
m= true;
% This is a string with single instead of double quotes (For Matlab compatibility)
n='Hello World';
% This is an Array<Array<cvec> >
% In Matlab, the elements defined below are accessible as
% o{1}{1}, o{1}{2} and o{2}{1}; note the curly brackets.
% For complex numbers, both 1+2i and (1,2) are allowed formats,
% but only the first one is Matlab compatible.
o = {{[1+2i 3+4i] [5+6i]} {[7 8 9]}}
T to(double x)
Convert double to T.
Beside the type-specific get methods, like get_int(), there is a templated get method called get(), that can handle all variable types that have an istream operator defined, e.g. Arrays. The get() method takes the variable value as an input/output parameter. If the variable name cannot be found, the old value is kept. Here is an example:
Array<ivec> var;
set_array(var, "{[1] [2 3] [4 5 6]}");
bool my_var_name_was_found = p.get(var, "my_var_name");
In the above example, if my_var_name
was not found, var
keeps its old value {[1] [2 3] [4 5 6]}. In non-silent mode, the get() method echoes the variable values to the standard output in Matlab/Octave m-file style.