#include <Trilinos_Util_CommandLineParser.h>
Public Member Functions | |
CommandLineParser (int argc, char *argv[]) | |
Trilinos_Util_ShellOptions constructor using the options given at the shell line. | |
virtual string | GetProgramName (void) |
Returns the name of the program as a C++ string. | |
virtual int | GetIntShellVariable (const char *str) |
Returns the value of the environmenta variable str as an integer. | |
virtual double | GetDoubleShellVariable (const char *str) |
Returns the value of the environmenta variable str as an double. | |
virtual string | GetStringShellVariable (const char *str) |
Returns the value of the environmenta variable str as a C++ string. |
Using Trilinos_Util::CommandLineParser, it is easy to handle input line arguments and shell varibles. For instance, the user can write
$ ./a.out -nx 10 -tol 1e-6 -solver=cg
nx
, tol
, and solver
.A simple code using this class is as follows:
int main(int argc, char *argv[]) { Trilinos_Util::CommandLineParser CLP(argc,argv); int nx = CLP.GetInt("-nx", 123); int ny = CLP.GetInt("-ny", 145); double tol = CLP.GetDouble("-tol", 1e-12); string solver = CLP.GetInt("-solver"); cout << "nx = " << nx << endl; cout << "ny = " << ny << " (default value)" << endl; cout << "tol = " << tol << endl; cout << "solver = " << solver << endl; return 0; }
Each line option can have a value or not. For options with a value, the user can specify this values as follows. Let -tolerance
be the name of the option and 1e-12
its value. Both choices are valid:
-option
1e-12
(with one or more spaces)-option=1e-12
(an `=' sign and no spaces)Options are indentified with one or more dashes (`-'). Each option cannot have more than one value.
Note that the user can specify some values without giving them a name. This can be done as follows:
$ ./a.out value1 value2 value 3 -nx 10 -tol 1e-6 -solver=cg
valueX
, (X=1,...,9) is stored in the database entry ARGV_X
.
To use this class, the user has to build the database using the argc
,argv input arguments. Then, to retrive the option value, the user has to use one of the following functions:
If option name is not found in the database, a value of 0, 0.0 or an empty string is returned. If needed, the user can also specify a default value to return when the option name is not found in the database. Method HaveOption
can be used to query the database for an option.
The user can modify the database as well, using
(GetInt, GetDouble, GetString, Set and Add are derived from the base class, Trilinos_Util_Map).
Finally, the user can retrive the integer, double or string value of a shell environmental variable using:
Trilinos_Util::CommandLineParser::CommandLineParser | ( | int | argc, | |
char * | argv[] | |||
) |
Trilinos_Util_ShellOptions constructor using the options given at the shell line.
References Trilinos_Util_Map::Set(), and Trilinos_Util_Map::SetLabel().
double Trilinos_Util::CommandLineParser::GetDoubleShellVariable | ( | const char * | str | ) | [virtual] |
Returns the value of the environmenta variable str
as an double.
This methods returns the value of the environmenta variable str
. If the variable does not exists, returns 0.0
.
References SPBLASMAT_STRUCT::buffer.
int Trilinos_Util::CommandLineParser::GetIntShellVariable | ( | const char * | str | ) | [virtual] |
Returns the value of the environmenta variable str
as an integer.
This methods returns the value of the environmental variable str
. If the variable does not exists, returns 0
.
References SPBLASMAT_STRUCT::buffer.
string Trilinos_Util::CommandLineParser::GetProgramName | ( | void | ) | [virtual] |
string Trilinos_Util::CommandLineParser::GetStringShellVariable | ( | const char * | str | ) | [virtual] |
Returns the value of the environmenta variable str
as a C++ string.
This methods returns the value of the environmenta variable str
. If the variable does not exists, returns ""
.
References SPBLASMAT_STRUCT::buffer.