Teuchos Package Browser (Single Doxygen Collection) Version of the Day
example/CommandLineProcessor/cxx_main.cpp
Go to the documentation of this file.
00001 /*
00002 // @HEADER
00003 // ***********************************************************************
00004 //
00005 //                    Teuchos: Common Tools Package
00006 //                 Copyright (2004) Sandia Corporation
00007 //
00008 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00009 // license for use of this work by or on behalf of the U.S. Government.
00010 //
00011 // This library is free software; you can redistribute it and/or modify
00012 // it under the terms of the GNU Lesser General Public License as
00013 // published by the Free Software Foundation; either version 2.1 of the
00014 // License, or (at your option) any later version.
00015 //
00016 // This library is distributed in the hope that it will be useful, but
00017 // WITHOUT ANY WARRANTY; without even the implied warranty of
00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019 // Lesser General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License along with this library; if not, write to the Free Software
00023 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00024 // USA
00025 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00026 //
00027 // ***********************************************************************
00028 // @HEADER
00029 */
00030 
00031 #include "Teuchos_CommandLineProcessor.hpp"
00032 #include "Teuchos_GlobalMPISession.hpp"
00033 #include "Teuchos_oblackholestream.hpp"
00034 #include "Teuchos_StandardCatchMacros.hpp"
00035 #include "Teuchos_Version.hpp"
00036 
00037 // Enum for the speed option
00038 enum ESpeed { SPEED_SLOW=-1, SPEED_MEDIUM=0, SPEED_FAST=+1 };
00039 
00040 int main(int argc, char* argv[])
00041 {
00042   Teuchos::GlobalMPISession mpiSession(&argc,&argv);
00043   const int procRank = Teuchos::GlobalMPISession::getRank();
00044 
00045   Teuchos::oblackholestream blackhole;
00046   std::ostream &out = ( procRank == 0 ? std::cout : blackhole );
00047 
00048   bool success = true;
00049   
00050   try {
00051     
00052     out << Teuchos::Teuchos_Version() << std::endl << std::endl;
00053     
00054     // Creating an empty command line processor looks like:
00055     Teuchos::CommandLineProcessor My_CLP;
00056 
00057     My_CLP.setDocString(
00058       "This example program demonstrates how to use this Teuchos::CommandLineProcessor class\n"
00059       "to get options from the command-line and print this help messange automatically.\n"
00060       );
00061     
00062     /* To set and option, it must be given a name and default value.  Additionally,
00063        each option can be given a help std::string.  Although it is not necessary, a help
00064        std::string aids a users comprehension of the acceptable command line arguments.
00065        Some examples of setting command line options are:
00066     */
00067     // Set an integer command line option.
00068     int NumIters = 1550;
00069     My_CLP.setOption("iterations", &NumIters, "Number of iterations");
00070     // Set a double-precision command line option.
00071     double Tolerance = 1e-10;
00072     My_CLP.setOption("tolerance", &Tolerance, "Tolerance");
00073     // Set a std::string command line option.
00074     std::string Solver = "GMRES";
00075     My_CLP.setOption("solver", &Solver, "Linear solver");
00076     // Set a boolean command line option.    
00077     bool Precondition = true;
00078     My_CLP.setOption("precondition","no-precondition",
00079                      &Precondition,"Preconditioning flag");
00080     // Set an enumeration command line option
00081     const int    num_speed_values  = 3;
00082     const ESpeed speed_opt_values[] = { SPEED_SLOW, SPEED_MEDIUM, SPEED_FAST };
00083     const char*  speed_opt_names[]  = { "slow",     "medium",     "fast"     };
00084     ESpeed       Speed = SPEED_MEDIUM;
00085     My_CLP.setOption(
00086       "speed", &Speed,
00087       num_speed_values, speed_opt_values, speed_opt_names,
00088       "Speed of our solver"
00089       );
00090 
00091     /* There are also two methods that control the behavior of the
00092        command line processor.  First, for the command line processor to
00093        allow an unrecognized a command line option to be ignored (and
00094        only have a warning printed), use:
00095     */
00096     My_CLP.recogniseAllOptions(true);
00097   
00098     /* Second, by default, if the parser finds a command line option it
00099        doesn't recognize or finds the --help option, it will throw an
00100        std::exception.  If you want prevent a command line processor from
00101        throwing an std::exception (which is important in this program since
00102        we don't have an try/catch around this) when it encounters a
00103        unrecognized option or help is printed, use:
00104     */
00105     My_CLP.throwExceptions(false);
00106 
00107     /* We now parse the command line where argc and argv are passed to
00108        the parse method.  Note that since we have turned off std::exception
00109        throwing above we had better grab the return argument so that
00110        we can see what happened and act accordingly.
00111     */
00112     Teuchos::CommandLineProcessor::EParseCommandLineReturn
00113       parseReturn= My_CLP.parse( argc, argv );
00114     if( parseReturn == Teuchos::CommandLineProcessor::PARSE_HELP_PRINTED ) {
00115       return 0;
00116     }
00117     if( parseReturn != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL   ) {
00118       return 1; // Error!
00119     }
00120     // Here is where you would use these command line arguments but for this example program
00121     // we will just print the help message with the new values of the command-line arguments.
00122     if (procRank == 0)
00123       out << "\nPrinting help message with new values of command-line arguments ...\n\n";
00124     My_CLP.printHelpMessage(argv[0],out);
00125 
00126     // Now we will print the option values
00127     if (procRank == 0) {
00128       out << "\nPrinting user options after parsing ...\n\n";
00129       out << "NumIters     = " << NumIters << std::endl;
00130       out << "Tolerance    = " << Tolerance << std::endl;
00131       out << "Solver       = \"" << Solver << "\"\n";
00132       out << "Precondition = " << Precondition << std::endl;
00133       out << "Speed        = " << Speed << std::endl;
00134     }
00135 
00136   } // try
00137   TEUCHOS_STANDARD_CATCH_STATEMENTS(true,std::cerr,success);
00138   
00139   if(success)
00140     out << "\nEnd Result: TEST PASSED" << std::endl;  
00141 
00142   return ( success ? 0 : 1 );
00143 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines