Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
core/example/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 
14 #include "Teuchos_Version.hpp"
15 #include "Teuchos_ConfigDefs.hpp"
17 
18 // Enum for the speed option
20 
21 int main(int argc, char* argv[])
22 {
23  Teuchos::GlobalMPISession mpiSession(&argc,&argv);
24  const int procRank = Teuchos::GlobalMPISession::getRank();
25 
26  Teuchos::oblackholestream blackhole;
27  std::ostream &out = ( procRank == 0 ? std::cout : blackhole );
28 
29  bool success = true;
30 
31  try {
32 
33  out << Teuchos::Teuchos_Version() << std::endl << std::endl;
34 
35  // Creating an empty command line processor looks like:
37 
38  My_CLP.setDocString(
39  "This example program demonstrates how to use this Teuchos::CommandLineProcessor class\n"
40  "to get options from the command-line and print this help messange automatically.\n"
41  );
42 
43  /* To set and option, it must be given a name and default value. Additionally,
44  each option can be given a help std::string. Although it is not necessary, a help
45  std::string aids a users comprehension of the acceptable command line arguments.
46  Some examples of setting command line options are:
47  */
48  // Set an integer command line option.
49  int NumIters = 1550;
50  My_CLP.setOption("iterations", &NumIters, "Number of iterations");
51  // Set a long integer command line option
52  long int MatrixDim = Teuchos::OrdinalTraits<long int>::max();
53  My_CLP.setOption("long-matrix-dim", &MatrixDim, "Matrix dimension (long)");
54  long long int MatrixDim2 = Teuchos::OrdinalTraits<long long int>::max();
55  My_CLP.setOption("long-long-matrix-dim", &MatrixDim2, "Matrix dimension (long long)");
56  // Set a double-precision command line option.
57  double Tolerance = 1e-10;
58  My_CLP.setOption("tolerance", &Tolerance, "Tolerance");
59  // Set a std::string command line option.
60  std::string Solver = "GMRES";
61  My_CLP.setOption("solver", &Solver, "Linear solver");
62  // Set a boolean command line option.
63  bool Precondition = true;
64  My_CLP.setOption("precondition","no-precondition",
65  &Precondition,"Preconditioning flag");
66  // Set an enumeration command line option
67  const int num_speed_values = 3;
68  const ESpeed speed_opt_values[] = { SPEED_SLOW, SPEED_MEDIUM, SPEED_FAST };
69  const char* speed_opt_names[] = { "slow", "medium", "fast" };
70  ESpeed Speed = SPEED_MEDIUM;
71  My_CLP.setOption(
72  "speed", &Speed,
73  num_speed_values, speed_opt_values, speed_opt_names,
74  "Speed of our solver"
75  );
76 
77  /* There are also two methods that control the behavior of the
78  command line processor. First, for the command line processor to
79  allow an unrecognized a command line option to be ignored (and
80  only have a warning printed), use:
81  */
82  My_CLP.recogniseAllOptions(true);
83 
84  /* Second, by default, if the parser finds a command line option it
85  doesn't recognize or finds the --help option, it will throw an
86  std::exception. If you want prevent a command line processor from
87  throwing an std::exception (which is important in this program since
88  we don't have an try/catch around this) when it encounters a
89  unrecognized option or help is printed, use:
90  */
91  My_CLP.throwExceptions(false);
92 
93  /* We now parse the command line where argc and argv are passed to
94  the parse method. Note that since we have turned off std::exception
95  throwing above we had better grab the return argument so that
96  we can see what happened and act accordingly.
97  */
99  parseReturn= My_CLP.parse( argc, argv );
101  return 0;
102  }
104  return 1; // Error!
105  }
106  // Here is where you would use these command line arguments but for this example program
107  // we will just print the help message with the new values of the command-line arguments.
108  if (procRank == 0)
109  out << "\nPrinting help message with new values of command-line arguments ...\n\n";
110  My_CLP.printHelpMessage(argv[0],out);
111 
112  // Now we will print the option values
113  if (procRank == 0) {
114  out << "\nPrinting user options after parsing ...\n\n";
115  out << "NumIters = " << NumIters << std::endl;
116  out << "MatrixDim = " << MatrixDim << std::endl;
117  out << "MatrixDim2 = " << MatrixDim2 << std::endl;
118  out << "Tolerance = " << Tolerance << std::endl;
119  out << "Solver = \"" << Solver << "\"\n";
120  out << "Precondition = " << Precondition << std::endl;
121  out << "Speed = " << Speed << std::endl;
122  }
123 
124  } // try
125  TEUCHOS_STANDARD_CATCH_STATEMENTS(true,std::cerr,success);
126 
127  if(success)
128  out << "\nEnd Result: TEST PASSED" << std::endl;
129 
130  return ( success ? 0 : 1 );
131 }
static int getRank()
The rank of the calling process in MPI_COMM_WORLD.
void recogniseAllOptions(const bool &recogniseAllOptions)
Set if all options must be recognized or not.
void printHelpMessage(const char program_name[], std::ostream &out) const
Print the help message.
basic_ostream&lt;&gt; subclass that does nothing but discard output.
Teuchos header file which uses auto-configuration information to include necessary C++ headers...
Initialize, finalize, and query the global MPI session.
void setOption(const char option_true[], const char option_false[], bool *option_val, const char documentation[]=NULL)
Set a boolean option.
#define TEUCHOS_STANDARD_CATCH_STATEMENTS(VERBOSE, ERR_STREAM, SUCCESS_FLAG)
Simple macro that catches and reports standard exceptions and other exceptions.
static T max()
Returns a value designating the maximum value accessible by code using OrdinalTraits.
EParseCommandLineReturn parse(int argc, char *argv[], std::ostream *errout=&std::cerr) const
Parse a command line.
void throwExceptions(const bool &throwExceptions)
Set if an std::exception is thrown, there is a parse error, or help is printed.
std::string Teuchos_Version()
int main(int argc, char *argv[])
EParseCommandLineReturn
Return value for CommandLineProcessor::parse(). Note: These enums are all given non-negative values s...
Defines basic traits for the ordinal field type.
A MPI utilities class, providing methods for initializing, finalizing, and querying the global MPI se...
Basic command line parser for input from (argc,argv[])
void setDocString(const char doc_string[])
Set a documentation sting for the entire program printed when –help is specified. ...
Class that helps parse command line input arguments from (argc,argv[]) and set options.