Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
core/test/CommandLineProcessor/cxx_main.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
12 #include "Teuchos_Version.hpp"
13 
14 int main( int argc, char* argv[] )
15 {
16 
18 
19  bool verbose = true;
20  bool parse_successful = true;
21 
22  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
23 
24  // First create tests for a command line processor that doesn't throw exceptions.
25  try {
26  // Read options from the commandline
27  CommandLineProcessor clp(false, false); // Don't throw exceptions
28 
29  double rel_proc_speed = 1e-5; // Should
30  clp.setOption( "rel-proc-speed", &rel_proc_speed, "Relative processor speed (try around 1.0 for timing)." );
31 
32  int size = 1;
33  clp.setOption( "size", &size, "Size of memory blocks created." );
34 
35  size_t sizetOption = 10;
36  clp.setOption( "sizeTOption", &sizetOption, "An option of type size_t.");
37 
38  long long longLongOption = 42;
39  clp.setOption( "longLongOption", &longLongOption, "An option of type long long." );
40 
41  // Parse the current input, which should return succesful.
42  CommandLineProcessor::EParseCommandLineReturn parse_return = clp.parse(argc,argv);
43  if (verbose)
44  std::cout << "Test 1: CommandLineProcessor - No exceptions - All extra options ignored: ";
45  if( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL )
46  {
47  parse_successful = false;
48  if (verbose) std::cout << "FAILED" << std::endl;
49  }
50  else
51  if (verbose) std::cout << "PASSED" << std::endl;
52 
53  // Add a new option that is required
54  int num = 1;
55  clp.setOption( "num", &num, "Number of memory blocks created (required option).", true );
56 
57  // Now parse with this new option (which should not be passed in on the command line)
58  parse_return = clp.parse(argc,argv);
59  if (verbose)
60  std::cout << "Test 2: CommandLineProcessor - No exceptions - All extra options ignored - 1 required: ";
61  if( parse_return != CommandLineProcessor::PARSE_ERROR )
62  {
63  parse_successful = false;
64  if (verbose) std::cout << "FAILED" << std::endl;
65  }
66  else
67  if (verbose) std::cout << "PASSED" << std::endl;
68 
69  }
70  catch( ... ) {
71  if(verbose)
72  std::cerr << "*** Caught UNEXPECTED unknown exception\n";
73  parse_successful = false; // No exceptions should be thrown for this command line processor.
74  }
75 
76  // Next create tests for a command line processor that does throw exceptions.
77  // Read options from the commandline
78  try {
79  CommandLineProcessor clp2(true, false); // Throw exceptions
80 
81  clp2.setOption( "verbose", "quiet", &verbose, "Set if output is printed or not." );
82 
83  double rel_proc_speed = 1e-5; // Should
84  clp2.setOption( "rel-proc-speed", &rel_proc_speed, "Relative processor speed (try around 1.0 for timing)." );
85 
86  int size = 1;
87  clp2.setOption( "size", &size, "Size of memory blocks created." );
88 
89  // Add a new option that is required
90  int num = 1;
91  clp2.setOption( "num", &num, "Number of memory blocks created (required option).", true );
92 
93  // Parse the argument line and see if we get an exception thrown
94  clp2.parse(argc,argv);
95  }
96  catch( CommandLineProcessor::ParseError &excpt ) {
97  if(verbose)
98  std::cout << "*** Caught EXPECTED standard exception : " << excpt.what() << std::endl
99  << "Test 3: CommandLineProcessor - Throw exceptions - All extra options ignored - 1 required: PASSED" << std::endl;
100  }
101  catch( ... ) {
102  if(verbose)
103  std::cout << "*** Caught UNEXPECTED unknown exception" << std::endl
104  << "Test 3: CommandLineProcessor - Throw exceptions - All extra options ignored - 1 required: FAILED" << std::endl;
105  parse_successful = false; // No exceptions should be thrown for this command line processor.
106  }
107 
108  // Next create tests for a command line processor that doesn't throw exceptions, and doesn't recognize all options.
109  try {
110  CommandLineProcessor clp3(false, true); // Don't recognize all options
111 
112  // Parse the current input, which should not be successful because the test is run with "--verbose" argument.
113  CommandLineProcessor::EParseCommandLineReturn parse_return = clp3.parse(argc,argv);
114  if (verbose)
115  std::cout << "Test 4 : CommandLineProcessor - No exceptions - Extra options not recognized: ";
116  if( parse_return != CommandLineProcessor::PARSE_UNRECOGNIZED_OPTION )
117  {
118  parse_successful = false;
119  if (verbose) std::cout << "FAILED" << std::endl;
120  }
121  else
122  if (verbose) std::cout << "PASSED" << std::endl;
123 
124  // Now add the verbose option back in and add a required option.
125  clp3.setOption( "verbose", "quiet", &verbose, "Set if output is printed or not." );
126 
127  int num = 1;
128  clp3.setOption( "num", &num, "Number of memory blocks created (required option).", true );
129 
130  parse_return = clp3.parse(argc,argv);
131  if (verbose)
132  std::cout << "Test 5 : CommandLineProcessor - No exceptions - Extra options not recognized - 1 required: ";
133  if( parse_return != CommandLineProcessor::PARSE_ERROR )
134  {
135  parse_successful = false;
136  if (verbose) std::cout << "FAILED" << std::endl;
137  }
138  else
139  if (verbose) std::cout << "PASSED" << std::endl;
140  }
141  catch( ... ) {
142  if(verbose)
143  std::cerr << "*** Caught UNEXPECTED unknown exception" << std::endl;
144  parse_successful = false; // No exceptions should be thrown for this command line processor.
145  }
146 
147  // Next create tests for a command line processor that doesn't throw exceptions, and doesn't recognize all options.
148  try {
149  if (verbose)
150  std::cout << "Test 6 : CommandLineProcessor - Throw exceptions - Extra options not recognized: ";
151 
152  CommandLineProcessor clp4(true, true); // Don't recognize all options AND throw exceptions (default mode)
153 
154  // Parse the current input, which should not be successful because the test is run with "--verbose" argument.
155  clp4.parse(argc,argv);
156  }
157  catch( CommandLineProcessor::UnrecognizedOption &excpt ) {
158  if(verbose)
159  std::cout << "*** Caught EXPECTED standard exception : " << excpt.what() << std::endl
160  << "Test 6: CommandLineProcessor - Throw exceptions - Extra options not recognized: PASSED" << std::endl;
161  }
162  catch( ... ) {
163  if(verbose)
164  std::cout << "*** Caught UNEXPECTED unknown exception" << std::endl
165  << "Test 6: CommandLineProcessor - Throw exceptions - Extra options not recognized: FAILED" << std::endl;
166  parse_successful = false; // No exceptions should be thrown for this command line processor.
167  }
168 
169  // Next create tests for a command line processor that makes sure help output is the same independent of the arg position
170  try {
171  if (verbose)
172  std::cout << "Test 7 : CommandLineProcessor - Help position" << std::endl;
173 
174  CommandLineProcessor clp7(false, false); // Recognize all options AND do not throw exceptions
175 
176  int n = 10;
177  clp7.setOption("n", &n, "A parameter");
178 
179  char arg_c[] = "command";
180  char arg_n[] = "--n=20";
181  char arg_h[] = "--help";
182 
183  const int aux_argc = 3;
184  char *aux_argv[aux_argc];
185 
186  std::stringbuf buffer1, buffer2;;
187  std::streambuf* oldbuffer = NULL;
188 
189  // help before args
190  aux_argv[0] = &arg_c[0];
191  aux_argv[1] = &arg_h[0];
192  aux_argv[2] = &arg_n[0];
193 
194  oldbuffer = std::cerr.rdbuf(&buffer1); // redirect output
195  clp7.parse(aux_argc, aux_argv);
196  std::cerr.rdbuf(oldbuffer); // redirect output back
197 
198  // help after args
199  aux_argv[0] = &arg_c[0];
200  aux_argv[1] = &arg_n[0];
201  aux_argv[2] = &arg_h[0];
202 
203  oldbuffer = std::cerr.rdbuf(&buffer2); // redirect output
204  clp7.parse(aux_argc, aux_argv);
205  std::cerr.rdbuf(oldbuffer); // redirect output back
206 
207  if (verbose)
208  std::cout << "Test 7 : CommandLineProcessor - Help position: ";
209  if (buffer1.str() != buffer2.str())
210  {
211  parse_successful = false;
212  if (verbose) std::cout << "FAILED" << std::endl;
213  }
214  else
215  if (verbose) std::cout << "PASSED" << std::endl;
216  }
217  catch( CommandLineProcessor::UnrecognizedOption &excpt ) {
218  if(verbose)
219  std::cout << "*** Caught EXPECTED standard exception : " << excpt.what() << std::endl
220  << "Test 7: CommandLineProcessor - Help position: PASSED" << std::endl;
221  }
222  catch( ... ) {
223  if(verbose)
224  std::cout << "*** Caught UNEXPECTED unknown exception" << std::endl
225  << "Test 7: CommandLineProcessor - Help position: FAILED" << std::endl;
226  parse_successful = false; // No exceptions should be thrown for this command line processor.
227  }
228 
229  // Return whether the command line processor tests passed.
230  if (parse_successful) {
231  std::cout << "End Result: TEST PASSED" << std::endl;
232  return 0;
233  }
234  else {
235  std::cout << "End Result: TEST FAILED" << std::endl;
236  return 1;
237  }
238 }
Initialize, finalize, and query the global MPI session.
int size(const Comm< Ordinal > &comm)
Get the number of processes in the communicator.
int main(int argc, char *argv[])
A MPI utilities class, providing methods for initializing, finalizing, and querying the global MPI se...
Basic command line parser for input from (argc,argv[])
Class that helps parse command line input arguments from (argc,argv[]) and set options.