Argument Parser
Base Module

Argument Parser. More...

## Classes

class | itpp::Parser
| Argument Parser Class. More...

## Detailed Description

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:

#include <itpp/itbase.h>
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[])
{
Parser p;
int a;
double b;
string c;
ivec d;
vec e;
svec f;
bvec g;
imat h;
mat i;
smat j;
bmat k;
bool l, m;
string n;
Array<Array<cvec> > o, q;
cout << "Use the Parser class on a parameter file:" << boolalpha << endl;
p.init(string(PARSER_TEST_FILE));
a = p.get_int("a");
cout << "a = " << a << endl;
p.get_double("b"); //The default value of b
p.get_double("b", 1); //The first alternative value of b
p.get_double("b", 2); //The second alternative value of b
c = p.get_string("c");
cout << "c = " << c << endl;
d = p.get_ivec("d");
cout << "d = " << d << endl;
e = p.get_vec("e");
cout << "e = " << e << endl;
f = p.get_svec("f");
cout << "f = " << f << endl;
g = p.get_bvec("g");
cout << "g = " << g << endl;
h = p.get_imat("h");
cout << "h = " << h << endl;
i = p.get_mat("i");
cout << "i = " << i << endl;
j = p.get_smat("j");
cout << "j = " << j << endl;
k = p.get_bmat("k");
cout << "k = " << k << endl;
l = p.get_bool("l");
cout << "l = " << l << endl;
m = p.get_bool("m");
cout << "m = " << m << endl;
n = p.get_string("n");
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 };
p.init(argc1, argv1);
a = p.get_int("a");
b = p.get_double("b");
c = p.get_string("c");
d = p.get_ivec("d");
cout << "------------------------------------------------------------" << endl;
cout << "Use the Parser class on a parameter file and a char pointer:" << endl;
// The data in the char pointer are selected first. The data in the
// file are used as default values
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);
}
a = p.get_int("a");
cout << "a = " << a << endl;
b = p.get_double("b");
cout << "b = " << b << endl;
c = p.get_string("c");
cout << "c = " << c << endl;
d = p.get_ivec("d");
cout << "d = " << d << endl;
cout << "------------------------------------------------------------" << endl;
cout << "Use the Parser class on an Array of strings:" << endl;
Array<string> parser_data(4);
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]";
p.init(parser_data);
a = p.get_int("a");
b = p.get_double("b");
c = p.get_string("c");
d = p.get_ivec("d");
cout << "------------------------------------------------------------" << endl;
cout << "Use the Parser::get() method on a parameter file:" << endl;
p.init(string(PARSER_TEST_FILE));
p.get(a, "a");
p.get(b, "b");
p.get(b, "b", 1);
p.get(b, "b", 2);
p.get(c, "c");
p.get(d, "d");
p.get(e, "e");
p.get(f, "f");
p.get(g, "g");
p.get(h, "h");
p.get(i, "i");
p.get(j, "j");
p.get(k, "k");
p.get(l, "l");
p.get(m, "m");
p.get(n, "n");
p.get(o, "o");
// The following tries can not be found in the data file, so their initial
// values should not be changed:
set_array(q, "{{[1][2 3][4 5 6]}}");
p.get(q, "q");
bool r = true;
p.get(r, "r");
int s = 7;
p.get(s, "s");
double t = 3.14;
p.get(t, "t");
std::string u = "test string";
p.get(u, "u");
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 /* ----- #ifndef PARSER_TEST_FILE ----- */
itpp::Array
General array class.
Definition array.h:105
itpp::Parser
Argument Parser Class.
Definition parser.h:112
itpp::Parser::get_double
double get_double(const std::string &name, int num=-1)
Interpret variable name as a double.
Definition parser.cpp:459
itpp::Parser::get_svec
svec get_svec(const std::string &name, int num=-1)
Interpret variable name as a svec.
Definition parser.cpp:500
itpp::Parser::get_mat
mat get_mat(const std::string &name, int num=-1)
Interpret variable name as a mat.
Definition parser.cpp:520
itpp::Parser::get_int
int get_int(const std::string &name, int num=-1)
Interpret variable name as an integer.
Definition parser.cpp:445
itpp::Parser::get_ivec
ivec get_ivec(const std::string &name, int num=-1)
Interpret variable name as a ivec.
Definition parser.cpp:490
itpp::Parser::get_bvec
bvec get_bvec(const std::string &name, int num=-1)
Interpret variable name as a bvec.
Definition parser.cpp:510
itpp::Parser::get_imat
imat get_imat(const std::string &name, int num=-1)
Interpret variable name as a imat.
Definition parser.cpp:530
itpp::Parser::get_bool
bool get_bool(const std::string &name, int num=-1)
Interpret variable name as a bool.
Definition parser.cpp:425
itpp::Parser::init
void init(const std::string &filename)
Initialization function. Sets input file name.
Definition parser.cpp:272
itpp::Parser::get_smat
smat get_smat(const std::string &name, int num=-1)
Interpret variable name as a smat.
Definition parser.cpp:540
itpp::Parser::get_string
std::string get_string(const std::string &name, int num=-1)
Interpret variable name as a string.
Definition parser.cpp:470
itpp::Parser::get
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...
Definition parser.h:209
itpp::Parser::exist
bool exist(const std::string &name)
Check is name exists in the file. Returns true if the name is found and false otherwise.
Definition parser.cpp:335
itpp::Parser::get_vec
vec get_vec(const std::string &name, int num=-1)
Interpret variable name as a vec.
Definition parser.cpp:480
itpp::Parser::get_bmat
bmat get_bmat(const std::string &name, int num=-1)
Interpret variable name as a bmat.
Definition parser.cpp:550
itbase.h
Include file for the IT++ base module.
bmat
Mat< bin > bmat
bin matrix
Definition mat.h:508
itpp
itpp namespace
Definition itmex.h:37
std
STL namespace.

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]}}
itpp::to
T to(double x)
Convert double to T.
Definition fix_functions.h:365

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:

// Declare and initialize a variable
Array<ivec> var;
set_array(var, "{[1] [2 3] [4 5 6]}");
// Let the Parser p get the variable named my_var_name
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.

Generated on Mon Jun 10 2024 11:49:29 for IT++ by Doxygen 1.9.8