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 // 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 
00050 // Enum for the speed option
00051 enum ESpeed { SPEED_SLOW=-1, SPEED_MEDIUM=0, SPEED_FAST=+1 };
00052 
00053 int main(int argc, char* argv[])
00054 {
00055   Teuchos::GlobalMPISession mpiSession(&argc,&argv);
00056   const int procRank = Teuchos::GlobalMPISession::getRank();
00057 
00058   Teuchos::oblackholestream blackhole;
00059   std::ostream &out = ( procRank == 0 ? std::cout : blackhole );
00060 
00061   bool success = true;
00062   
00063   try {
00064     
00065     out << Teuchos::Teuchos_Version() << std::endl << std::endl;
00066     
00067     // Creating an empty command line processor looks like:
00068     Teuchos::CommandLineProcessor My_CLP;
00069 
00070     My_CLP.setDocString(
00071       "This example program demonstrates how to use this Teuchos::CommandLineProcessor class\n"
00072       "to get options from the command-line and print this help messange automatically.\n"
00073       );
00074     
00075     /* To set and option, it must be given a name and default value.  Additionally,
00076        each option can be given a help std::string.  Although it is not necessary, a help
00077        std::string aids a users comprehension of the acceptable command line arguments.
00078        Some examples of setting command line options are:
00079     */
00080     // Set an integer command line option.
00081     int NumIters = 1550;
00082     My_CLP.setOption("iterations", &NumIters, "Number of iterations");
00083     // Set a double-precision command line option.
00084     double Tolerance = 1e-10;
00085     My_CLP.setOption("tolerance", &Tolerance, "Tolerance");
00086     // Set a std::string command line option.
00087     std::string Solver = "GMRES";
00088     My_CLP.setOption("solver", &Solver, "Linear solver");
00089     // Set a boolean command line option.    
00090     bool Precondition = true;
00091     My_CLP.setOption("precondition","no-precondition",
00092                      &Precondition,"Preconditioning flag");
00093     // Set an enumeration command line option
00094     const int    num_speed_values  = 3;
00095     const ESpeed speed_opt_values[] = { SPEED_SLOW, SPEED_MEDIUM, SPEED_FAST };
00096     const char*  speed_opt_names[]  = { "slow",     "medium",     "fast"     };
00097     ESpeed       Speed = SPEED_MEDIUM;
00098     My_CLP.setOption(
00099       "speed", &Speed,
00100       num_speed_values, speed_opt_values, speed_opt_names,
00101       "Speed of our solver"
00102       );
00103 
00104     /* There are also two methods that control the behavior of the
00105        command line processor.  First, for the command line processor to
00106        allow an unrecognized a command line option to be ignored (and
00107        only have a warning printed), use:
00108     */
00109     My_CLP.recogniseAllOptions(true);
00110   
00111     /* Second, by default, if the parser finds a command line option it
00112        doesn't recognize or finds the --help option, it will throw an
00113        std::exception.  If you want prevent a command line processor from
00114        throwing an std::exception (which is important in this program since
00115        we don't have an try/catch around this) when it encounters a
00116        unrecognized option or help is printed, use:
00117     */
00118     My_CLP.throwExceptions(false);
00119 
00120     /* We now parse the command line where argc and argv are passed to
00121        the parse method.  Note that since we have turned off std::exception
00122        throwing above we had better grab the return argument so that
00123        we can see what happened and act accordingly.
00124     */
00125     Teuchos::CommandLineProcessor::EParseCommandLineReturn
00126       parseReturn= My_CLP.parse( argc, argv );
00127     if( parseReturn == Teuchos::CommandLineProcessor::PARSE_HELP_PRINTED ) {
00128       return 0;
00129     }
00130     if( parseReturn != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL   ) {
00131       return 1; // Error!
00132     }
00133     // Here is where you would use these command line arguments but for this example program
00134     // we will just print the help message with the new values of the command-line arguments.
00135     if (procRank == 0)
00136       out << "\nPrinting help message with new values of command-line arguments ...\n\n";
00137     My_CLP.printHelpMessage(argv[0],out);
00138 
00139     // Now we will print the option values
00140     if (procRank == 0) {
00141       out << "\nPrinting user options after parsing ...\n\n";
00142       out << "NumIters     = " << NumIters << std::endl;
00143       out << "Tolerance    = " << Tolerance << std::endl;
00144       out << "Solver       = \"" << Solver << "\"\n";
00145       out << "Precondition = " << Precondition << std::endl;
00146       out << "Speed        = " << Speed << std::endl;
00147     }
00148 
00149   } // try
00150   TEUCHOS_STANDARD_CATCH_STATEMENTS(true,std::cerr,success);
00151   
00152   if(success)
00153     out << "\nEnd Result: TEST PASSED" << std::endl;  
00154 
00155   return ( success ? 0 : 1 );
00156 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines