CommandLineProcessor/cxx_main.cpp
This is an example of how to use the
Teuchos::CommandLineProcessor class.
#include "Teuchos_CommandLineProcessor.hpp"
#include "Teuchos_GlobalMPISession.hpp"
#include "Teuchos_oblackholestream.hpp"
#include "Teuchos_StandardCatchMacros.hpp"
#include "Teuchos_Version.hpp"
enum ESpeed { SPEED_SLOW=-1, SPEED_MEDIUM=0, SPEED_FAST=+1 };
int main(int argc, char* argv[])
{
Teuchos::GlobalMPISession mpiSession(&argc,&argv);
const int procRank = Teuchos::GlobalMPISession::getRank();
Teuchos::oblackholestream blackhole;
std::ostream &out = ( procRank == 0 ? std::cout : blackhole );
bool success = true;
try {
out << Teuchos::Teuchos_Version() << std::endl << std::endl;
Teuchos::CommandLineProcessor My_CLP;
My_CLP.setDocString(
"This example program demonstrates how to use this Teuchos::CommandLineProcessor class\n"
"to get options from the command-line and print this help messange automatically.\n"
);
int NumIters = 1550;
My_CLP.setOption("iterations", &NumIters, "Number of iterations");
double Tolerance = 1e-10;
My_CLP.setOption("tolerance", &Tolerance, "Tolerance");
std::string Solver = "GMRES";
My_CLP.setOption("solver", &Solver, "Linear solver");
bool Precondition;
My_CLP.setOption("precondition","no-precondition",
&Precondition,"Preconditioning flag");
const int num_speed_values = 3;
const ESpeed speed_opt_values[] = { SPEED_SLOW, SPEED_MEDIUM, SPEED_FAST };
const char* speed_opt_names[] = { "slow", "medium", "fast" };
ESpeed Speed = SPEED_MEDIUM;
My_CLP.setOption(
"speed", &Speed,
num_speed_values, speed_opt_values, speed_opt_names,
"Speed of our solver"
);
My_CLP.recogniseAllOptions(true);
My_CLP.throwExceptions(false);
Teuchos::CommandLineProcessor::EParseCommandLineReturn
parseReturn= My_CLP.parse( argc, argv );
if( parseReturn == Teuchos::CommandLineProcessor::PARSE_HELP_PRINTED ) {
return 0;
}
if( parseReturn != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL ) {
return 1;
}
if (procRank == 0)
out << "\nPrinting help message with new values of command-line arguments ...\n\n";
My_CLP.printHelpMessage(argv[0],out);
if (procRank == 0) {
out << "\nPrinting user options after parsing ...\n\n";
out << "NumIters = " << NumIters << std::endl;
out << "Tolerance = " << Tolerance << std::endl;
out << "Solver = \"" << Solver << "\"\n";
out << "Precondition = " << Precondition << std::endl;
out << "Speed = " << Speed << std::endl;
}
}
TEUCHOS_STANDARD_CATCH_STATEMENTS(true,std::cerr,success);
if(success)
out << "\nEnd Result: TEST PASSED" << std::endl;
return ( success ? 0 : 1 );
}