CommandLineProcessor/cxx_main.cpp

This is an example of how to use the Teuchos::CommandLineProcessor class.

00001 #include "Teuchos_CommandLineProcessor.hpp"
00002 #include "Teuchos_GlobalMPISession.hpp"
00003 #include "Teuchos_oblackholestream.hpp"
00004 #include "Teuchos_StandardCatchMacros.hpp"
00005 #include "Teuchos_Version.hpp"
00006 
00007 // Enum for the speed option
00008 enum ESpeed { SPEED_SLOW=-1, SPEED_MEDIUM=0, SPEED_FAST=+1 };
00009 
00010 int main(int argc, char* argv[])
00011 {
00012   Teuchos::GlobalMPISession mpiSession(&argc,&argv);
00013   const int procRank = Teuchos::GlobalMPISession::getRank();
00014 
00015   Teuchos::oblackholestream blackhole;
00016   std::ostream &out = ( procRank == 0 ? std::cout : blackhole );
00017 
00018   bool success = true;
00019   
00020   try {
00021     
00022     out << Teuchos::Teuchos_Version() << std::endl << std::endl;
00023     
00024     // Creating an empty command line processor looks like:
00025     Teuchos::CommandLineProcessor My_CLP;
00026 
00027     My_CLP.setDocString(
00028       "This example program demonstrates how to use this Teuchos::CommandLineProcessor class\n"
00029       "to get options from the command-line and print this help messange automatically.\n"
00030       );
00031     
00032     /* To set and option, it must be given a name and default value.  Additionally,
00033        each option can be given a help std::string.  Although it is not necessary, a help
00034        std::string aids a users comprehension of the acceptable command line arguments.
00035        Some examples of setting command line options are:
00036     */
00037     // Set an integer command line option.
00038     int NumIters = 1550;
00039     My_CLP.setOption("iterations", &NumIters, "Number of iterations");
00040     // Set a double-precision command line option.
00041     double Tolerance = 1e-10;
00042     My_CLP.setOption("tolerance", &Tolerance, "Tolerance");
00043     // Set a std::string command line option.
00044     std::string Solver = "GMRES";
00045     My_CLP.setOption("solver", &Solver, "Linear solver");
00046     // Set a boolean command line option.    
00047     bool Precondition;
00048     My_CLP.setOption("precondition","no-precondition",
00049                      &Precondition,"Preconditioning flag");
00050     // Set an enumeration command line option
00051     const int    num_speed_values  = 3;
00052     const ESpeed speed_opt_values[] = { SPEED_SLOW, SPEED_MEDIUM, SPEED_FAST };
00053     const char*  speed_opt_names[]  = { "slow",     "medium",     "fast"     };
00054     ESpeed       Speed = SPEED_MEDIUM;
00055     My_CLP.setOption(
00056       "speed", &Speed,
00057       num_speed_values, speed_opt_values, speed_opt_names,
00058       "Speed of our solver"
00059       );
00060 
00061     /* There are also two methods that control the behavior of the
00062        command line processor.  First, for the command line processor to
00063        allow an unrecognized a command line option to be ignored (and
00064        only have a warning printed), use:
00065     */
00066     My_CLP.recogniseAllOptions(true);
00067   
00068     /* Second, by default, if the parser finds a command line option it
00069        doesn't recognize or finds the --help option, it will throw an
00070        std::exception.  If you want prevent a command line processor from
00071        throwing an std::exception (which is important in this program since
00072        we don't have an try/catch around this) when it encounters a
00073        unrecognized option or help is printed, use:
00074     */
00075     My_CLP.throwExceptions(false);
00076 
00077     /* We now parse the command line where argc and argv are passed to
00078        the parse method.  Note that since we have turned off std::exception
00079        throwing above we had better grab the return argument so that
00080        we can see what happened and act accordingly.
00081     */
00082     Teuchos::CommandLineProcessor::EParseCommandLineReturn
00083       parseReturn= My_CLP.parse( argc, argv );
00084     if( parseReturn == Teuchos::CommandLineProcessor::PARSE_HELP_PRINTED ) {
00085       return 0;
00086     }
00087     if( parseReturn != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL   ) {
00088       return 1; // Error!
00089     }
00090     // Here is where you would use these command line arguments but for this example program
00091     // we will just print the help message with the new values of the command-line arguments.
00092     if (procRank == 0)
00093       out << "\nPrinting help message with new values of command-line arguments ...\n\n";
00094     My_CLP.printHelpMessage(argv[0],out);
00095 
00096     // Now we will print the option values
00097     if (procRank == 0) {
00098       out << "\nPrinting user options after parsing ...\n\n";
00099       out << "NumIters     = " << NumIters << std::endl;
00100       out << "Tolerance    = " << Tolerance << std::endl;
00101       out << "Solver       = \"" << Solver << "\"\n";
00102       out << "Precondition = " << Precondition << std::endl;
00103       out << "Speed        = " << Speed << std::endl;
00104     }
00105 
00106   } // try
00107   TEUCHOS_STANDARD_CATCH_STATEMENTS(true,std::cerr,success);
00108   
00109   if(success)
00110     out << "\nEnd Result: TEST PASSED" << std::endl;  
00111 
00112   return ( success ? 0 : 1 );
00113 }

Generated on Wed May 12 21:40:31 2010 for Teuchos - Trilinos Tools Package by  doxygen 1.4.7