Teuchos Package Browser (Single Doxygen Collection) Version of the Day
Teuchos_CommandLineProcessor.cpp
Go to the documentation of this file.
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 // //////////////////////////////////////////////////
00043 // Teuchos_CommandLineProcessor.cpp
00044 
00045 
00046 #include "Teuchos_CommandLineProcessor.hpp"
00047 #include "Teuchos_GlobalMPISession.hpp"
00048 #include "Teuchos_VerboseObject.hpp"
00049 //#include "Teuchos_TimeMonitor.hpp"
00050 #include "Teuchos_Assert.hpp"
00051 #include "Teuchos_as.hpp"
00052 
00053 
00054 namespace {
00055 
00056 
00057 inline int my_max( int a, int b ) { return a > b ? a : b; }
00058 
00059 
00060 std::string remove_quotes( const std::string& str )
00061 {
00062   if(str[0] != '\"')
00063     return str;
00064   return str.substr(1,str.size()-2);
00065 }
00066 
00067 
00068 std::string add_quotes( const std::string& str )
00069 {
00070   if(str[0] == '\"')
00071     return str;
00072   return "\"" + str + "\"";
00073 }
00074 
00075 
00076 } // end namespace
00077 
00078 
00079 namespace Teuchos {
00080 
00081 
00082 const bool  CommandLineProcessor::output_all_front_matter_default_(false);
00083 const bool  CommandLineProcessor::output_show_line_prefix_default_(false);
00084 const bool  CommandLineProcessor::output_show_tab_count_default_(false);
00085 const bool  CommandLineProcessor::output_show_proc_rank_default_(false);
00086 const int   CommandLineProcessor::output_to_root_rank_only_default_(0);
00087 const bool  CommandLineProcessor::print_rcpnode_statistics_on_exit_default_(false);
00088 const bool  CommandLineProcessor::show_timer_summary_on_exit_default_(false);
00089 
00090 
00091 CommandLineProcessor::CommandLineProcessor(
00092   bool   throwExceptions_in
00093   ,bool  recogniseAllOptions_in
00094   ,bool  addOutputSetupOptions_in
00095   )
00096   :throwExceptions_(throwExceptions_in)
00097   ,recogniseAllOptions_(recogniseAllOptions_in)
00098   ,addOutputSetupOptions_(addOutputSetupOptions_in)
00099   ,output_all_front_matter_(output_all_front_matter_default_)
00100   ,output_show_line_prefix_(output_show_line_prefix_default_)
00101   ,output_show_tab_count_(output_show_tab_count_default_)
00102   ,output_show_proc_rank_(output_show_proc_rank_default_)
00103   ,output_to_root_rank_only_(output_to_root_rank_only_default_)
00104   ,print_rcpnode_statistics_on_exit_(print_rcpnode_statistics_on_exit_default_)
00105   ,show_timer_summary_on_exit_(show_timer_summary_on_exit_default_)
00106   ,printed_timer_summary_(false)
00107   ,added_extra_output_setup_options_(false)
00108   ,in_add_extra_output_setup_options_(false)
00109 {}
00110 
00111 
00112 CommandLineProcessor::~CommandLineProcessor()
00113 {
00114   printFinalTimerSummary();
00115 }
00116 
00117 
00118 // Set up options
00119 
00120 
00121 void CommandLineProcessor::setDocString( const char doc_string[] )
00122 {
00123   doc_string_ = doc_string;
00124 }
00125 
00126 
00127 void CommandLineProcessor::setOption(
00128   const char     option_true[]
00129   ,const char    option_false[]
00130   ,bool          *option_val
00131   ,const char    documentation[]
00132   )
00133 {
00134   add_extra_output_setup_options();
00135   TEUCHOS_TEST_FOR_EXCEPT(!(option_val!=NULL));
00136   options_list_[std::string(option_true)]
00137     = opt_val_val_t(OPT_BOOL_TRUE,any(option_val),false);
00138   options_list_[std::string(option_false)]
00139     = opt_val_val_t(OPT_BOOL_FALSE,any(option_val),false);
00140   options_documentation_list_.push_back(
00141     opt_doc_t(OPT_BOOL_TRUE, option_true, option_false,
00142       std::string(documentation?documentation:""), any(option_val)) 
00143     );
00144 }
00145 
00146 
00147 void CommandLineProcessor::setOption(
00148   const char     option_name[]
00149   ,int           *option_val
00150   ,const char    documentation[]
00151   ,const bool    required
00152   )
00153 {
00154   add_extra_output_setup_options();
00155   TEUCHOS_TEST_FOR_EXCEPT(!(option_val!=NULL));
00156   options_list_[std::string(option_name)]
00157     = opt_val_val_t(OPT_INT,any(option_val),required);
00158   options_documentation_list_.push_back(
00159     opt_doc_t(OPT_INT, option_name, "", std::string(documentation?documentation:""),
00160       any(option_val))
00161     );
00162 }
00163 
00164 
00165 void CommandLineProcessor::setOption(
00166   const char     option_name[]
00167   ,long int      *option_val
00168   ,const char    documentation[]
00169   ,const bool    required
00170   )
00171 {
00172   add_extra_output_setup_options();
00173   TEUCHOS_TEST_FOR_EXCEPT(!(option_val!=NULL));
00174   options_list_[std::string(option_name)]
00175     = opt_val_val_t(OPT_LONG_INT,any(option_val),required);
00176   options_documentation_list_.push_back(
00177     opt_doc_t(OPT_LONG_INT, option_name, "", std::string(documentation?documentation:""),
00178       any(option_val))
00179     );
00180 }
00181 
00182 
00183 #ifdef HAVE_TEUCHOS_LONG_LONG_INT
00184 void CommandLineProcessor::setOption(
00185   const char     option_name[]
00186   ,long long int *option_val
00187   ,const char    documentation[]
00188   ,const bool    required
00189   )
00190 {
00191   add_extra_output_setup_options();
00192   TEUCHOS_TEST_FOR_EXCEPT(!(option_val!=NULL));
00193   options_list_[std::string(option_name)]
00194     = opt_val_val_t(OPT_LONG_LONG_INT,any(option_val),required);
00195   options_documentation_list_.push_back(
00196     opt_doc_t(OPT_LONG_LONG_INT, option_name, "", std::string(documentation?documentation:""),
00197       any(option_val))
00198     );
00199 }
00200 #endif
00201 
00202 
00203 void CommandLineProcessor::setOption(
00204   const char     option_name[]
00205   ,double        *option_val
00206   ,const char    documentation[]
00207   ,const bool    required
00208   )
00209 {
00210   add_extra_output_setup_options();
00211   TEUCHOS_TEST_FOR_EXCEPT(!(option_val!=NULL));
00212   options_list_[std::string(option_name)]
00213     = opt_val_val_t(OPT_DOUBLE,any(option_val),required);
00214   options_documentation_list_.push_back(
00215     opt_doc_t(OPT_DOUBLE, option_name, "", std::string(documentation?documentation:""),
00216       any(option_val))
00217     );
00218 }
00219 
00220 
00221 void CommandLineProcessor::setOption(
00222   const char     option_name[]
00223   ,std::string   *option_val
00224   ,const char    documentation[]
00225   ,const bool    required
00226   )
00227 {
00228   add_extra_output_setup_options();
00229   TEUCHOS_TEST_FOR_EXCEPT(!(option_val!=NULL));
00230   options_list_[std::string(option_name)]
00231     = opt_val_val_t(OPT_STRING,any(option_val),required);
00232   options_documentation_list_.push_back(
00233     opt_doc_t(OPT_STRING, option_name, "", std::string(documentation?documentation:""),
00234       any(option_val))
00235     );
00236 }
00237 
00238 
00239 // Parse command line
00240 
00241 
00242 CommandLineProcessor::EParseCommandLineReturn
00243 CommandLineProcessor::parse(
00244   int             argc
00245   ,char*          argv[]
00246   ,std::ostream   *errout
00247   ) const
00248 {
00249   add_extra_output_setup_options();
00250   std::string        opt_name;
00251   std::string        opt_val_str;
00252   const std::string  echo_cl_opt = "echo-command-line";
00253   const std::string  help_opt = "help";
00254   const std::string  pause_opt = "pause-for-debugging";
00255   int procRank = GlobalMPISession::getRank();
00256   for( int i = 1; i < argc; ++i ) {
00257     bool gov_return = get_opt_val( argv[i], &opt_name, &opt_val_str );
00258     if( !gov_return ) {
00259       if(procRank == 0) 
00260         print_bad_opt(i,argv,errout);
00261       if( recogniseAllOptions() ) 
00262         return PARSE_UNRECOGNIZED_OPTION;
00263       else {
00264         continue;
00265       }
00266     }
00267     if( opt_name == echo_cl_opt ) {
00268       if(errout && procRank == 0) {
00269         *errout << "\nEchoing the command-line:\n\n";
00270         for( int j = 0; j < argc; ++j )
00271           *errout << argv[j] << " ";
00272         *errout << "\n\n";
00273       }
00274       continue;
00275     }
00276     if( opt_name == help_opt ) {
00277       if(errout) printHelpMessage( argv[0], *errout );
00278       return PARSE_HELP_PRINTED;
00279     }
00280     if( opt_name == pause_opt ) {
00281       if(procRank == 0) {
00282         std::cerr << "\nType 0 and press enter to continue : ";
00283         int dummy_int = 0;
00284         std::cin >> dummy_int;
00285       }
00286       GlobalMPISession::barrier();
00287       continue;
00288     }
00289     // Lookup the option (we had better find it!)
00290     options_list_t::iterator  itr = options_list_.find(opt_name);
00291     if( itr == options_list_.end() ) {
00292       if(procRank == 0)
00293         print_bad_opt(i,argv,errout);
00294       if( recogniseAllOptions() ) 
00295         return PARSE_UNRECOGNIZED_OPTION;
00296       else
00297         continue;
00298     }
00299     // Changed access to second value of std::map to not use overloaded arrow operator, 
00300     // otherwise this code will not compile on Janus (HKT, 12/01/2003) 
00301     opt_val_val_t &opt_val_val = (*itr).second;
00302     opt_val_val.was_read = true;
00303     switch( opt_val_val.opt_type ) {
00304       case OPT_BOOL_TRUE:
00305         *(any_cast<bool*>(opt_val_val.opt_val)) = true;
00306         break;
00307       case OPT_BOOL_FALSE:
00308         *(any_cast<bool*>(opt_val_val.opt_val)) = false;
00309         break;
00310       case OPT_INT:
00311         *(any_cast<int*>(opt_val_val.opt_val)) = asSafe<int> (opt_val_str);
00312         break;
00313       case OPT_LONG_INT:
00314         *(any_cast<long int*>(opt_val_val.opt_val)) = asSafe<long int> (opt_val_str);
00315         break;
00316 #ifdef HAVE_TEUCHOS_LONG_LONG_INT
00317       case OPT_LONG_LONG_INT:
00318         *(any_cast<long long int*>(opt_val_val.opt_val)) = asSafe<long long int> (opt_val_str);
00319         break;
00320 #endif
00321       case OPT_DOUBLE:
00322         *(any_cast<double*>(opt_val_val.opt_val)) = asSafe<double> (opt_val_str);
00323         break;
00324       case OPT_STRING:
00325         *(any_cast<std::string*>(opt_val_val.opt_val)) = remove_quotes(opt_val_str);
00326         break;
00327       case OPT_ENUM_INT:
00328         if( !set_enum_value( i, argv, opt_name, any_cast<int>(opt_val_val.opt_val),
00329             remove_quotes(opt_val_str), errout ) )
00330         {
00331           return PARSE_UNRECOGNIZED_OPTION;
00332         }
00333         break;
00334       default:
00335         TEUCHOS_TEST_FOR_EXCEPT(true); // Local programming error only
00336     }
00337   }
00338   // Look for options that were required but were not set
00339   for(
00340     options_list_t::const_iterator itr = options_list_.begin();
00341     itr != options_list_.end();
00342     ++itr
00343     )
00344   {
00345     const opt_val_val_t   &opt_val_val  = (*itr).second;
00346     if( opt_val_val.required && !opt_val_val.was_read ) {
00347       const std::string     &opt_val_name = (*itr).first;
00348 #define CLP_ERR_MSG \
00349       "Error, the option --"<<opt_val_name<<" was required but was not set!"
00350       if(errout)
00351         *errout << std::endl << argv[0] << " : " << CLP_ERR_MSG << std::endl;
00352       if( throwExceptions() ) {
00353         TEUCHOS_TEST_FOR_EXCEPTION( true, ParseError, CLP_ERR_MSG );
00354       }
00355       return PARSE_ERROR;
00356 #undef CLP_ERR_MSG
00357     }
00358   }
00359   // Set the options of a default stream exists and if we are asked to
00360   RCP<FancyOStream>
00361     defaultOut = VerboseObjectBase::getDefaultOStream();
00362   if (defaultOut.get() && addOutputSetupOptions_) {
00363     if (output_all_front_matter_ != output_all_front_matter_default_)
00364       defaultOut->setShowAllFrontMatter(output_all_front_matter_);
00365     if (output_show_line_prefix_ != output_show_line_prefix_default_)
00366       defaultOut->setShowLinePrefix(output_show_line_prefix_);
00367     if (output_show_tab_count_ != output_show_tab_count_default_)
00368       defaultOut->setShowTabCount(output_show_tab_count_);
00369     if (output_show_proc_rank_ != output_show_proc_rank_default_)
00370       defaultOut->setShowProcRank(output_show_proc_rank_);
00371     if (output_to_root_rank_only_ != output_to_root_rank_only_default_)
00372       defaultOut->setOutputToRootOnly(output_to_root_rank_only_);
00373     RCPNodeTracer::setPrintRCPNodeStatisticsOnExit(print_rcpnode_statistics_on_exit_);
00374   }
00375   return PARSE_SUCCESSFUL;
00376 }
00377 
00378 
00379 void CommandLineProcessor::printHelpMessage( const char program_name[],
00380   std::ostream &out ) const
00381 {
00382   add_extra_output_setup_options();
00383   int procRank = GlobalMPISession::getRank();
00384   if (procRank == 0) {
00385     using std::setw;
00386     using std::endl;
00387     
00388     const int opt_type_w = 14;
00389     const char spc_chars[] = "  ";
00390     
00391     // Get the maximum length of an option name
00392     int opt_name_w = 19; // For the 'pause-for-debugging' option
00393     options_documentation_list_t::const_iterator itr;
00394     for (
00395       itr = options_documentation_list_.begin();
00396       itr != options_documentation_list_.end();
00397       ++itr
00398       )
00399     {
00400       opt_name_w = my_max(opt_name_w,itr->opt_name.length());
00401       if( itr->opt_type )
00402         opt_name_w = my_max(opt_name_w,itr->opt_name_false.length());
00403     }
00404     opt_name_w += 2;
00405     
00406     // Some built-in options
00407     out
00408       << "Usage: " << program_name << " [options]\n"
00409       << spc_chars << "options:\n"
00410       << spc_chars
00411       << "--"
00412 #ifdef HAVE_STD_IOS_BASE_FMTFLAGS
00413       << std::left << setw(opt_name_w) << "help"
00414       << std::left << setw(opt_type_w) << " "
00415 #else
00416       << std::setiosflags(std::ios::left) << setw(opt_name_w) << "help"
00417       << std::setiosflags(std::ios::left) << setw(opt_type_w) << " "
00418 #endif
00419       << "Prints this help message"
00420       << std::endl
00421       << spc_chars
00422       << "--"
00423 #ifdef HAVE_STD_IOS_BASE_FMTFLAGS
00424       << std::left << setw(opt_name_w) << "pause-for-debugging"
00425       << std::left << setw(opt_type_w) << " "
00426 #else
00427       << std::setiosflags(std::ios::left) << setw(opt_name_w) << "pause-for-debugging"
00428       << std::setiosflags(std::ios::left) << setw(opt_type_w) << " "
00429 #endif
00430       << "Pauses for user input to allow attaching a debugger"
00431       << std::endl
00432       << spc_chars
00433       << "--"
00434 #ifdef HAVE_STD_IOS_BASE_FMTFLAGS
00435       << std::left << setw(opt_name_w) << "echo-command-line"
00436       << std::left << setw(opt_type_w) << " "
00437 #else
00438       << std::setiosflags(std::ios::left) << setw(opt_name_w) << "echo-command-line"
00439       << std::setiosflags(std::ios::left) << setw(opt_type_w) << " "
00440 #endif
00441       << "Echo the command-line but continue as normal"
00442       << std::endl;
00443     for(
00444       itr = options_documentation_list_.begin();
00445       itr != options_documentation_list_.end();
00446       ++itr )
00447     {
00448       // print top line with option name, type and short documentation string
00449       out
00450         << spc_chars
00451         << "--"
00452 #ifdef HAVE_STD_IOS_BASE_FMTFLAGS
00453         << std::left << setw(opt_name_w) << itr->opt_name
00454         << std::left << setw(opt_type_w) << opt_type_str(itr->opt_type)
00455 #else
00456         << std::setiosflags(std::ios::left) << setw(opt_name_w) << itr->opt_name
00457         << std::setiosflags(std::ios::left) << setw(opt_type_w) << opt_type_str(itr->opt_type)
00458 #endif
00459         << ( itr->documentation.length() ? itr->documentation.c_str() : "No documentation" )
00460         << std::endl;
00461       // If an enumeration option then the next line is the value options
00462       if( itr->opt_type == OPT_ENUM_INT ) {
00463         out
00464           << spc_chars
00465           << "  "
00466           << setw(opt_name_w) << ""
00467           << setw(opt_type_w) << "";
00468         print_enum_opt_names( any_cast<int>(itr->default_val), out );
00469         out
00470           << std::endl;
00471       }
00472       // Now print the line that contains the default values
00473       if( itr->opt_type == OPT_BOOL_TRUE ) {
00474         out
00475           << spc_chars
00476           << "--"
00477           << setw(opt_name_w) << itr->opt_name_false;
00478       }
00479       else {
00480         out
00481           << spc_chars
00482           << "  "
00483           << setw(opt_name_w) << " ";
00484       }
00485       out
00486         << setw(opt_type_w) << " "
00487         << "(default: ";
00488       switch( itr->opt_type ) {
00489         case OPT_BOOL_TRUE:
00490           out << "--" << ( (*(any_cast<bool*>(itr->default_val))) ?
00491             itr->opt_name : itr->opt_name_false );
00492           break;
00493         case OPT_INT:
00494         case OPT_LONG_INT:
00495 #ifdef HAVE_TEUCHOS_LONG_LONG_INT
00496         case OPT_LONG_LONG_INT:
00497 #endif
00498         case OPT_DOUBLE:
00499         case OPT_STRING:
00500         case OPT_ENUM_INT:
00501           out << "--" << itr->opt_name;
00502           break;
00503         default:
00504           TEUCHOS_TEST_FOR_EXCEPT(true); // Local programming error only
00505       }
00506       switch( itr->opt_type ) {
00507         case OPT_BOOL_TRUE:
00508           break;
00509         case OPT_INT:
00510           out << "=" << (*(any_cast<int*>(itr->default_val)));
00511           break;
00512         case OPT_LONG_INT:
00513           out << "=" << (*(any_cast<long int*>(itr->default_val)));
00514           break;
00515 #ifdef HAVE_TEUCHOS_LONG_LONG_INT
00516         case OPT_LONG_LONG_INT:
00517           out << "=" << (*(any_cast<long long int*>(itr->default_val)));
00518           break;
00519 #endif
00520         case OPT_DOUBLE:
00521           out <<  "=" << (*(any_cast<double*>(itr->default_val)));
00522           break;
00523         case OPT_STRING:
00524           out <<  "=" << add_quotes(*(any_cast<std::string*>(itr->default_val)));
00525           break;
00526         case OPT_ENUM_INT:
00527           out <<  "=" << add_quotes(
00528             enum_opt_default_val_name(itr->opt_name,any_cast<int>(itr->default_val),&out));
00529           break;
00530         default:
00531           TEUCHOS_TEST_FOR_EXCEPT(true); // Local programming error only
00532       }
00533       out << ")\n";
00534     }
00535     if(doc_string_.length()) {
00536       out << "\nDETAILED DOCUMENTATION:\n\n" << doc_string_ << std::endl << std::endl;
00537     }
00538     if(throwExceptions_)
00539       TEUCHOS_TEST_FOR_EXCEPTION( true, HelpPrinted, "Help message was printed" );
00540   }
00541 }
00542 
00543 
00544 void CommandLineProcessor::printFinalTimerSummary(
00545   const Ptr<std::ostream> &out_inout
00546   )
00547 {
00548   if (!printed_timer_summary_ && show_timer_summary_on_exit_) {
00549     RCP<std::ostream> out;
00550     if (nonnull(out_inout)) {
00551       out = rcpFromPtr(out_inout);
00552     }
00553     else {
00554       out = VerboseObjectBase::getDefaultOStream();
00555     }
00556     getTimeMonitorSurrogate()->summarize(*out << "\n");
00557     printed_timer_summary_ = true;
00558   }
00559 }
00560 
00561 
00562 // private
00563 
00564 
00565 void CommandLineProcessor::add_extra_output_setup_options() const
00566 {
00567   if(
00568     // Are we in this function already and calling it recursively?
00569     in_add_extra_output_setup_options_
00570     ||
00571     // Have we already setup these options?
00572     added_extra_output_setup_options_
00573     ||
00574     // Are we not supposed to setup these options?
00575     !addOutputSetupOptions_
00576     )
00577   {
00578     return; // If any of the above is true, we need to return right away!
00579   }
00580   // Set the commandline options for this ...
00581   CommandLineProcessor
00582     *clp = const_cast<CommandLineProcessor*>(this);
00583   clp->in_add_extra_output_setup_options_ = true;
00584   clp->setOption(
00585     "output-all-front-matter","output-no-front-matter",&clp->output_all_front_matter_
00586     ,"Set if all front matter is printed to the default FancyOStream or not"
00587     );
00588   clp->setOption(
00589     "output-show-line-prefix","output-no-show-line-prefix",&clp->output_show_line_prefix_
00590     ,"Set if the line prefix matter is printed to the default FancyOStream or not"
00591     );
00592   clp->setOption(
00593     "output-show-tab-count","output-no-show-tab-count",&clp->output_show_tab_count_
00594     ,"Set if the tab count is printed to the default FancyOStream or not"
00595     );
00596   clp->setOption(
00597     "output-show-proc-rank","output-no-show-proc-rank",&clp->output_show_proc_rank_
00598     ,"Set if the processor rank is printed to the default FancyOStream or not"
00599     );
00600   clp->setOption(
00601     "output-to-root-rank-only",&clp->output_to_root_rank_only_
00602     ,"Set which processor (the root) gets the output.  If < 0, then all processors get output."
00603     );
00604   clp->setOption(
00605     "print-rcpnode-statistics-on-exit", "no-print-rcpnode-statistics-on-exit",
00606     &clp->print_rcpnode_statistics_on_exit_,
00607     "Set if the RCPNode usage statistics will be printed on exit or not.  Warning,"
00608     " this prints to std::cerr or every process so do not turn this on for very large"
00609     " parallel runs."
00610     );
00611   if (nonnull(getTimeMonitorSurrogate())) {
00612     clp->setOption(
00613       "show-timer-summary", "no-show-timer-sumary", &clp->show_timer_summary_on_exit_,
00614       "If true, then Teuchos::TimeMonitor::summarize() is called in"
00615       " CommandLineProcessor's destructor (usually at the end of main)."
00616       );
00617   }
00618 
00619   clp->added_extra_output_setup_options_ = true;
00620   clp->in_add_extra_output_setup_options_ = false;
00621 }
00622 
00623 
00624 void CommandLineProcessor::setEnumOption(
00625   const char    enum_option_name[]
00626   ,int          *enum_option_val
00627   ,const int    num_enum_opt_values
00628   ,const int    enum_opt_values[]
00629   ,const char*  enum_opt_names[]
00630   ,const char   documentation[]
00631   ,const bool   required
00632   )
00633 {
00634   add_extra_output_setup_options();
00635 
00636   TEUCHOS_TEST_FOR_EXCEPT(enum_option_val==NULL);
00637   TEUCHOS_TEST_FOR_EXCEPT(num_enum_opt_values<=0);
00638   TEUCHOS_TEST_FOR_EXCEPT(enum_opt_values==NULL);
00639   TEUCHOS_TEST_FOR_EXCEPT(enum_opt_names==NULL);
00640 
00641   enum_opt_data_list_.push_back(
00642     enum_opt_data_t(enum_option_val,num_enum_opt_values,enum_opt_values,enum_opt_names)
00643     );
00644   const int opt_id = enum_opt_data_list_.size()-1;
00645   options_list_[std::string(enum_option_name)]
00646     = opt_val_val_t(OPT_ENUM_INT,any(opt_id),required);
00647   options_documentation_list_.push_back(
00648     opt_doc_t(OPT_ENUM_INT,enum_option_name, "",
00649       std::string(documentation?documentation:""), any(opt_id))
00650     );
00651 }
00652 
00653 
00654 bool CommandLineProcessor::set_enum_value(
00655   int                  argv_i
00656   ,char*               argv[]
00657   ,const std::string   &enum_opt_name
00658   ,const int           enum_id
00659   ,const std::string   &enum_str_val
00660   ,std::ostream        *errout
00661   ) const
00662 {
00663   const enum_opt_data_t
00664     &enum_opt_data = enum_opt_data_list_.at(enum_id);
00665   std::vector<std::string>::const_iterator
00666     itr_begin = enum_opt_data.enum_opt_names.begin(),
00667     itr_end   = enum_opt_data.enum_opt_names.end(),
00668     itr       =  std::find( itr_begin, itr_end, enum_str_val );
00669   if( itr == itr_end ) {
00670     const int j = argv_i;
00671 #define CLP_ERR_MSG \
00672       "Error, the value \"" << enum_str_val << "\" for the " \
00673       << j<<(j==1?"st":(j==2?"nd":(j==3?"rd":"th"))) << " option --" \
00674       << enum_opt_name << " was not recognized (use --help)!"
00675     if(errout)
00676       *errout << std::endl << argv[0] << " : " << CLP_ERR_MSG << std::endl;
00677     if( throwExceptions() ) {
00678       TEUCHOS_TEST_FOR_EXCEPTION( true, UnrecognizedOption, CLP_ERR_MSG );
00679     }
00680     else {
00681       return false;
00682     }
00683 #undef CLP_ERR_MSG
00684   }
00685   const int enum_opt_val_index = itr - itr_begin;
00686   *enum_opt_data.enum_option_val = enum_opt_data.enum_opt_values.at(enum_opt_val_index);
00687   return true;
00688 }
00689 
00690 
00691 void CommandLineProcessor::print_enum_opt_names(
00692   const int            enum_id
00693   ,std::ostream        &out
00694   ) const
00695 {
00696   const enum_opt_data_t
00697     &enum_opt_data = enum_opt_data_list_.at(enum_id);
00698   typedef std::vector<std::string>::const_iterator itr_t;
00699   out << "Valid options:";
00700   for(
00701     itr_t itr = enum_opt_data.enum_opt_names.begin();
00702     itr != enum_opt_data.enum_opt_names.end();
00703     ++itr
00704     )
00705   {
00706     if( itr != enum_opt_data.enum_opt_names.begin() ) out << ",";
00707     out << " " << add_quotes(*itr);
00708   }
00709 }
00710 
00711 
00712 std::string
00713 CommandLineProcessor::enum_opt_default_val_name(
00714   const std::string    &enum_name
00715   ,const int           enum_id
00716   ,std::ostream        *errout
00717   ) const
00718 {
00719   const enum_opt_data_t
00720     &enum_opt_data = enum_opt_data_list_.at(enum_id);
00721   return enum_opt_data.enum_opt_names.at(
00722     find_enum_opt_index(
00723       enum_name,*enum_opt_data.enum_option_val,enum_opt_data,errout
00724       )
00725     );
00726 }
00727 
00728 
00729 int CommandLineProcessor::find_enum_opt_index(
00730   const std::string           &enum_opt_name
00731   ,const int                  opt_value
00732   ,const enum_opt_data_t      &enum_data
00733   ,std::ostream               *errout
00734   ) const
00735 {
00736   std::vector<int>::const_iterator
00737     itr_begin = enum_data.enum_opt_values.begin(),
00738     itr_end   = enum_data.enum_opt_values.end(),
00739     itr       =  std::find( itr_begin, itr_end, opt_value );
00740   if( itr == itr_end ) {
00741 #define CLP_ERR_MSG \
00742       ( recogniseAllOptions() ? "Error" : "Warning" ) \
00743       << ", option --" << enum_opt_name << " was given an invalid " \
00744       "initial option value of " << opt_value << "!"
00745     if(errout)
00746       *errout << CLP_ERR_MSG << std::endl;
00747     if( throwExceptions() )
00748       TEUCHOS_TEST_FOR_EXCEPTION( true, std::invalid_argument, CLP_ERR_MSG );
00749 #undef CLP_ERR_MSG
00750   }
00751   return itr - itr_begin;
00752 }
00753 
00754 
00755 bool CommandLineProcessor::get_opt_val(
00756   const char     str[]
00757   ,std::string   *opt_name
00758   ,std::string   *opt_val_str
00759   ) const
00760 {
00761   const int len = std::strlen(str);
00762   if( len < 3 )
00763     return false; // Can't be an option with '--' followed by at least one char
00764   if( str[0] != '-' || str[1] != '-' )
00765     return false; // Not a recognised option
00766   // Find the '='
00767   int equ_i;
00768   for( equ_i = 2; equ_i < len && str[equ_i] != '='; ++equ_i );
00769   // Set opt_name
00770   opt_name->assign( str + 2, equ_i-2 );
00771   // Set opt_val_str
00772   if( equ_i == len ) {
00773     *opt_val_str = "";
00774   }
00775   else {
00776     opt_val_str->assign( str + equ_i + 1, len - equ_i - 1 );
00777   }
00778   return true;
00779 }
00780 
00781 void CommandLineProcessor::print_bad_opt(
00782   int             argv_i
00783   ,char*          argv[]
00784   ,std::ostream   *errout
00785   ) const
00786 {
00787   const int j = argv_i;
00788 #define CLP_ERR_MSG \
00789     ( recogniseAllOptions() ? "Error" : "Warning" ) \
00790     << ", the " << j<<(j==1?"st":(j==2?"nd":(j==3?"rd":"th"))) \
00791     << " option \'" << argv[argv_i] << "\' was not recognized (use --help)!"
00792   if(errout)
00793     *errout << std::endl << argv[0] << " : " << CLP_ERR_MSG << std::endl;
00794   if( recogniseAllOptions() && throwExceptions() )
00795     TEUCHOS_TEST_FOR_EXCEPTION( true, UnrecognizedOption, CLP_ERR_MSG );
00796 #undef CLP_ERR_MSG
00797 }
00798 
00799 
00800 // Hidden stuff
00801 
00802 
00803 void CommandLineProcessor::setTimeMonitorSurrogate(
00804   const RCP<CommandLineProcessor::TimeMonitorSurrogate> &timeMonitorSurrogate)
00805 {
00806   getRawTimeMonitorSurrogate() = timeMonitorSurrogate;
00807 }
00808 
00809 
00810 RCP<CommandLineProcessor::TimeMonitorSurrogate>
00811 CommandLineProcessor::getTimeMonitorSurrogate()
00812 {
00813   return getRawTimeMonitorSurrogate();
00814 }
00815 
00816 
00817 RCP<CommandLineProcessor::TimeMonitorSurrogate>&
00818 CommandLineProcessor::getRawTimeMonitorSurrogate()
00819 {
00820   static RCP<TimeMonitorSurrogate> timeMonitorSurrogate;
00821   return timeMonitorSurrogate;
00822 } 
00823 
00824 
00825 } // end namespace Teuchos
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines