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