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:
using namespace itpp;
using namespace std;
#ifndef PARSER_TEST_FILE
int main()
{
cerr << "PARSER_TEST_FILE not defined. Test skipped." << endl;
return 1;
}
#else
int main(int argc, char *argv[])
{
int a;
double b;
string c;
ivec d;
vec e;
svec f;
bvec g;
imat h;
mat i;
smat j;
bool l, m;
string n;
cout << "Use the Parser class on a parameter file:" << boolalpha << endl;
p.
init(
string(PARSER_TEST_FILE));
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_0[] = "a=345";
char argv1_1[] = "b=1.001";
char argv1_2[] = "c=\"Hello Bird\"";
char argv1_3[] = "d=[1,2,3,-1,-2,-3]";
int argc1 = 4;
char *argv1[4] = { argv1_0, argv1_1, argv1_2, argv1_3 };
cout << "------------------------------------------------------------" << endl;
cout << "Use the Parser class on a parameter file and a char pointer:" << endl;
char argv2_0[] = "a=345";
char argv2_1[] = "d=[1,-2,3,-4,5,-6,7,-8]";
int argc2 = 2;
char *argv2[2] = { argv2_0, argv2_1 };
if (argc == 0) {
p.
init(
string(PARSER_TEST_FILE), argc2, argv2);
}
else {
p.
init(
string(PARSER_TEST_FILE), argc, argv);
}
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;
parser_data(0) = "a=-11";
parser_data(1) = "b=3.14";
parser_data(2) = "c=\"Hello Nerd\"";
parser_data(3) = "d=[0,1,2,0,3,4,7]";
cout << "------------------------------------------------------------" << endl;
cout << "Use the Parser::get() method on a parameter file:" << endl;
p.
init(
string(PARSER_TEST_FILE));
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
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");
%
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]}}
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.