Teuchos Package Browser (Single Doxygen Collection) Version of the Day
core/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 // Redistribution and use in source and binary forms, with or without
00012 // modification, are permitted provided that the following conditions are
00013 // met:
00014 //
00015 // 1. Redistributions of source code must retain the above copyright
00016 // notice, this list of conditions and the following disclaimer.
00017 //
00018 // 2. Redistributions in binary form must reproduce the above copyright
00019 // notice, this list of conditions and the following disclaimer in the
00020 // documentation and/or other materials provided with the distribution.
00021 //
00022 // 3. Neither the name of the Corporation nor the names of the
00023 // contributors may be used to endorse or promote products derived from
00024 // this software without specific prior written permission.
00025 //
00026 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00027 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00029 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00030 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00031 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00032 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00033 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00034 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00035 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00036 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037 //
00038 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00039 //
00040 // ***********************************************************************
00041 // @HEADER
00042 */
00043 
00044 #include "Teuchos_CommandLineProcessor.hpp"
00045 #include "Teuchos_GlobalMPISession.hpp"
00046 #include "Teuchos_oblackholestream.hpp"
00047 #include "Teuchos_StandardCatchMacros.hpp"
00048 #include "Teuchos_Version.hpp"
00049 #include "Teuchos_ConfigDefs.hpp"
00050 #include "Teuchos_OrdinalTraits.hpp"
00051 
00052 // Enum for the speed option
00053 enum ESpeed { SPEED_SLOW=-1, SPEED_MEDIUM=0, SPEED_FAST=+1 };
00054 
00055 int main(int argc, char* argv[])
00056 {
00057   Teuchos::GlobalMPISession mpiSession(&argc,&argv);
00058   const int procRank = Teuchos::GlobalMPISession::getRank();
00059 
00060   Teuchos::oblackholestream blackhole;
00061   std::ostream &out = ( procRank == 0 ? std::cout : blackhole );
00062 
00063   bool success = true;
00064   
00065   try {
00066     
00067     out << Teuchos::Teuchos_Version() << std::endl << std::endl;
00068     
00069     // Creating an empty command line processor looks like:
00070     Teuchos::CommandLineProcessor My_CLP;
00071 
00072     My_CLP.setDocString(
00073       "This example program demonstrates how to use this Teuchos::CommandLineProcessor class\n"
00074       "to get options from the command-line and print this help messange automatically.\n"
00075       );
00076     
00077     /* To set and option, it must be given a name and default value.  Additionally,
00078        each option can be given a help std::string.  Although it is not necessary, a help
00079        std::string aids a users comprehension of the acceptable command line arguments.
00080        Some examples of setting command line options are:
00081     */
00082     // Set an integer command line option.
00083     int NumIters = 1550;
00084     My_CLP.setOption("iterations", &NumIters, "Number of iterations");
00085     // Set a long integer command line option
00086     long int MatrixDim = Teuchos::OrdinalTraits<long int>::max();
00087     My_CLP.setOption("long-matrix-dim", &MatrixDim, "Matrix dimension (long)");    
00088 #ifdef HAVE_TEUCHOS_LONG_LONG_INT
00089     long long int MatrixDim2 = Teuchos::OrdinalTraits<long long int>::max();
00090     My_CLP.setOption("long-long-matrix-dim", &MatrixDim2, "Matrix dimension (long long)");
00091 #endif
00092     // Set a double-precision command line option.
00093     double Tolerance = 1e-10;
00094     My_CLP.setOption("tolerance", &Tolerance, "Tolerance");
00095     // Set a std::string command line option.
00096     std::string Solver = "GMRES";
00097     My_CLP.setOption("solver", &Solver, "Linear solver");
00098     // Set a boolean command line option.    
00099     bool Precondition = true;
00100     My_CLP.setOption("precondition","no-precondition",
00101                      &Precondition,"Preconditioning flag");
00102     // Set an enumeration command line option
00103     const int    num_speed_values  = 3;
00104     const ESpeed speed_opt_values[] = { SPEED_SLOW, SPEED_MEDIUM, SPEED_FAST };
00105     const char*  speed_opt_names[]  = { "slow",     "medium",     "fast"     };
00106     ESpeed       Speed = SPEED_MEDIUM;
00107     My_CLP.setOption(
00108       "speed", &Speed,
00109       num_speed_values, speed_opt_values, speed_opt_names,
00110       "Speed of our solver"
00111       );
00112 
00113     /* There are also two methods that control the behavior of the
00114        command line processor.  First, for the command line processor to
00115        allow an unrecognized a command line option to be ignored (and
00116        only have a warning printed), use:
00117     */
00118     My_CLP.recogniseAllOptions(true);
00119   
00120     /* Second, by default, if the parser finds a command line option it
00121        doesn't recognize or finds the --help option, it will throw an
00122        std::exception.  If you want prevent a command line processor from
00123        throwing an std::exception (which is important in this program since
00124        we don't have an try/catch around this) when it encounters a
00125        unrecognized option or help is printed, use:
00126     */
00127     My_CLP.throwExceptions(false);
00128 
00129     /* We now parse the command line where argc and argv are passed to
00130        the parse method.  Note that since we have turned off std::exception
00131        throwing above we had better grab the return argument so that
00132        we can see what happened and act accordingly.
00133     */
00134     Teuchos::CommandLineProcessor::EParseCommandLineReturn
00135       parseReturn= My_CLP.parse( argc, argv );
00136     if( parseReturn == Teuchos::CommandLineProcessor::PARSE_HELP_PRINTED ) {
00137       return 0;
00138     }
00139     if( parseReturn != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL   ) {
00140       return 1; // Error!
00141     }
00142     // Here is where you would use these command line arguments but for this example program
00143     // we will just print the help message with the new values of the command-line arguments.
00144     if (procRank == 0)
00145       out << "\nPrinting help message with new values of command-line arguments ...\n\n";
00146     My_CLP.printHelpMessage(argv[0],out);
00147 
00148     // Now we will print the option values
00149     if (procRank == 0) {
00150       out << "\nPrinting user options after parsing ...\n\n";
00151       out << "NumIters     = " << NumIters << std::endl;
00152       out << "MatrixDim    = " << MatrixDim << std::endl;
00153 #ifdef HAVE_TEUCHOS_LONG_LONG_INT
00154       out << "MatrixDim2   = " << MatrixDim2 << std::endl;
00155 #endif
00156       out << "Tolerance    = " << Tolerance << std::endl;
00157       out << "Solver       = \"" << Solver << "\"\n";
00158       out << "Precondition = " << Precondition << std::endl;
00159       out << "Speed        = " << Speed << std::endl;
00160     }
00161 
00162   } // try
00163   TEUCHOS_STANDARD_CATCH_STATEMENTS(true,std::cerr,success);
00164   
00165   if(success)
00166     out << "\nEnd Result: TEST PASSED" << std::endl;  
00167 
00168   return ( success ? 0 : 1 );
00169 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines