|
Teuchos Package Browser (Single Doxygen Collection)
Version of the Day
|
00001 // @HEADER 00002 // *********************************************************************** 00003 // 00004 // Teuchos: Common Tools Package 00005 // Copyright (2004) Sandia Corporation 00006 // 00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive 00008 // license for use of this work by or on behalf of the U.S. Government. 00009 // 00010 // Redistribution and use in source and binary forms, with or without 00011 // modification, are permitted provided that the following conditions are 00012 // met: 00013 // 00014 // 1. Redistributions of source code must retain the above copyright 00015 // notice, this list of conditions and the following disclaimer. 00016 // 00017 // 2. Redistributions in binary form must reproduce the above copyright 00018 // notice, this list of conditions and the following disclaimer in the 00019 // documentation and/or other materials provided with the distribution. 00020 // 00021 // 3. Neither the name of the Corporation nor the names of the 00022 // contributors may be used to endorse or promote products derived from 00023 // this software without specific prior written permission. 00024 // 00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY 00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE 00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00036 // 00037 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 00038 // 00039 // *********************************************************************** 00040 // @HEADER 00041 00042 #include "Teuchos_CommandLineProcessor.hpp" 00043 #include "Teuchos_GlobalMPISession.hpp" 00044 #include "Teuchos_Version.hpp" 00045 00046 int main( int argc, char* argv[] ) 00047 { 00048 00049 using Teuchos::CommandLineProcessor; 00050 00051 bool verbose = true; 00052 bool parse_successful = true; 00053 00054 Teuchos::GlobalMPISession mpiSession(&argc, &argv); 00055 00056 // First create tests for a command line processor that doesn't throw exceptions. 00057 try { 00058 // Read options from the commandline 00059 CommandLineProcessor clp(false, false); // Don't throw exceptions 00060 00061 double rel_proc_speed = 1e-5; // Should 00062 clp.setOption( "rel-proc-speed", &rel_proc_speed, "Relative processor speed (try around 1.0 for timing)." ); 00063 00064 int size = 1; 00065 clp.setOption( "size", &size, "Size of memory blocks created." ); 00066 00067 size_t sizetOption = 10; 00068 clp.setOption( "sizeTOption", &sizetOption, "An option of type size_t."); 00069 00070 #ifdef HAVE_TEUCHOS_LONG_LONG_INT 00071 long long longLongOption = 42; 00072 clp.setOption( "longLongOption", &longLongOption, "An option of type long long." ); 00073 #endif // HAVE_TEUCHOS_LONG_LONG_INT 00074 00075 // Parse the current input, which should return succesful. 00076 CommandLineProcessor::EParseCommandLineReturn parse_return = clp.parse(argc,argv); 00077 if (verbose) 00078 std::cout << "Test 1: CommandLineProcessor - No exceptions - All extra options ignored: "; 00079 if( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL ) 00080 { 00081 parse_successful = false; 00082 if (verbose) std::cout << "FAILED" << std::endl; 00083 } 00084 else 00085 if (verbose) std::cout << "PASSED" << std::endl; 00086 00087 // Add a new option that is required 00088 int num = 1; 00089 clp.setOption( "num", &num, "Number of memory blocks created (required option).", true ); 00090 00091 // Now parse with this new option (which should not be passed in on the command line) 00092 parse_return = clp.parse(argc,argv); 00093 if (verbose) 00094 std::cout << "Test 2: CommandLineProcessor - No exceptions - All extra options ignored - 1 required: "; 00095 if( parse_return != CommandLineProcessor::PARSE_ERROR ) 00096 { 00097 parse_successful = false; 00098 if (verbose) std::cout << "FAILED" << std::endl; 00099 } 00100 else 00101 if (verbose) std::cout << "PASSED" << std::endl; 00102 00103 } 00104 catch( ... ) { 00105 if(verbose) 00106 std::cerr << "*** Caught UNEXPECTED unknown exception\n"; 00107 parse_successful = false; // No exceptions should be thrown for this command line processor. 00108 } 00109 00110 // Next create tests for a command line processor that does throw exceptions. 00111 // Read options from the commandline 00112 try { 00113 CommandLineProcessor clp2(true, false); // Throw exceptions 00114 00115 clp2.setOption( "verbose", "quiet", &verbose, "Set if output is printed or not." ); 00116 00117 double rel_proc_speed = 1e-5; // Should 00118 clp2.setOption( "rel-proc-speed", &rel_proc_speed, "Relative processor speed (try around 1.0 for timing)." ); 00119 00120 int size = 1; 00121 clp2.setOption( "size", &size, "Size of memory blocks created." ); 00122 00123 // Add a new option that is required 00124 int num = 1; 00125 clp2.setOption( "num", &num, "Number of memory blocks created (required option).", true ); 00126 00127 // Parse the argument line and see if we get an exception thrown 00128 clp2.parse(argc,argv); 00129 } 00130 catch( CommandLineProcessor::ParseError &excpt ) { 00131 if(verbose) 00132 std::cout << "*** Caught EXPECTED standard exception : " << excpt.what() << std::endl 00133 << "Test 3: CommandLineProcessor - Throw exceptions - All extra options ignored - 1 required: PASSED" << std::endl; 00134 } 00135 catch( ... ) { 00136 if(verbose) 00137 std::cout << "*** Caught UNEXPECTED unknown exception" << std::endl 00138 << "Test 3: CommandLineProcessor - Throw exceptions - All extra options ignored - 1 required: FAILED" << std::endl; 00139 parse_successful = false; // No exceptions should be thrown for this command line processor. 00140 } 00141 00142 // Next create tests for a command line processor that doesn't throw exceptions, and doesn't recognize all options. 00143 try { 00144 CommandLineProcessor clp3(false, true); // Don't recognize all options 00145 00146 // Parse the current input, which should not be successful because the test is run with "--verbose" argument. 00147 CommandLineProcessor::EParseCommandLineReturn parse_return = clp3.parse(argc,argv); 00148 if (verbose) 00149 std::cout << "Test 4 : CommandLineProcessor - No exceptions - Extra options not recognized: "; 00150 if( parse_return != CommandLineProcessor::PARSE_UNRECOGNIZED_OPTION ) 00151 { 00152 parse_successful = false; 00153 if (verbose) std::cout << "FAILED" << std::endl; 00154 } 00155 else 00156 if (verbose) std::cout << "PASSED" << std::endl; 00157 00158 // Now add the verbose option back in and add a required option. 00159 clp3.setOption( "verbose", "quiet", &verbose, "Set if output is printed or not." ); 00160 00161 int num = 1; 00162 clp3.setOption( "num", &num, "Number of memory blocks created (required option).", true ); 00163 00164 parse_return = clp3.parse(argc,argv); 00165 if (verbose) 00166 std::cout << "Test 5 : CommandLineProcessor - No exceptions - Extra options not recognized - 1 required: "; 00167 if( parse_return != CommandLineProcessor::PARSE_ERROR ) 00168 { 00169 parse_successful = false; 00170 if (verbose) std::cout << "FAILED" << std::endl; 00171 } 00172 else 00173 if (verbose) std::cout << "PASSED" << std::endl; 00174 } 00175 catch( ... ) { 00176 if(verbose) 00177 std::cerr << "*** Caught UNEXPECTED unknown exception" << std::endl; 00178 parse_successful = false; // No exceptions should be thrown for this command line processor. 00179 } 00180 00181 // Next create tests for a command line processor that doesn't throw exceptions, and doesn't recognize all options. 00182 try { 00183 if (verbose) 00184 std::cout << "Test 6 : CommandLineProcessor - Throw exceptions - Extra options not recognized: "; 00185 00186 CommandLineProcessor clp4(true, true); // Don't recognize all options AND throw exceptions (default mode) 00187 00188 // Parse the current input, which should not be successful because the test is run with "--verbose" argument. 00189 clp4.parse(argc,argv); 00190 } 00191 catch( CommandLineProcessor::UnrecognizedOption &excpt ) { 00192 if(verbose) 00193 std::cout << "*** Caught EXPECTED standard exception : " << excpt.what() << std::endl 00194 << "Test 6: CommandLineProcessor - Throw exceptions - Extra options not recognized: PASSED" << std::endl; 00195 } 00196 catch( ... ) { 00197 if(verbose) 00198 std::cout << "*** Caught UNEXPECTED unknown exception" << std::endl 00199 << "Test 5: CommandLineProcessor - Throw exceptions - Extra options not recognized: FAILED" << std::endl; 00200 parse_successful = false; // No exceptions should be thrown for this command line processor. 00201 } 00202 00203 // Return whether the command line processor tests passed. 00204 if (parse_successful) { 00205 std::cout << "End Result: TEST PASSED" << std::endl; 00206 return 0; 00207 } 00208 else { 00209 std::cout << "End Result: TEST FAILED" << std::endl; 00210 return 1; 00211 } 00212 }
1.7.6.1