Teuchos - Trilinos Tools Package  Version of the Day
Teuchos_CommandLineProcessor.cpp
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Teuchos: Common Tools Package
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 // //////////////////////////////////////////////////
43 // Teuchos_CommandLineProcessor.cpp
44 
45 
48 #include "Teuchos_VerboseObject.hpp"
49 //#include "Teuchos_TimeMonitor.hpp"
50 #include "Teuchos_Assert.hpp"
51 #include "Teuchos_as.hpp"
52 
53 
54 namespace {
55 
56 
57 inline int my_max( int a, int b ) { return a > b ? a : b; }
58 
59 
60 std::string remove_quotes( const std::string& str )
61 {
62  if(str[0] != '\"')
63  return str;
64  return str.substr(1,str.size()-2);
65 }
66 
67 
68 std::string add_quotes( const std::string& str )
69 {
70  if(str[0] == '\"')
71  return str;
72  return "\"" + str + "\"";
73 }
74 
75 
76 } // end namespace
77 
78 
79 namespace Teuchos {
80 
81 
82 const bool CommandLineProcessor::output_all_front_matter_default_(false);
83 const bool CommandLineProcessor::output_show_line_prefix_default_(false);
84 const bool CommandLineProcessor::output_show_tab_count_default_(false);
85 const bool CommandLineProcessor::output_show_proc_rank_default_(false);
86 const int CommandLineProcessor::output_to_root_rank_only_default_(0);
87 const bool CommandLineProcessor::print_rcpnode_statistics_on_exit_default_(false);
88 const bool CommandLineProcessor::show_timer_summary_on_exit_default_(false);
89 
90 
92  bool throwExceptions_in
93  ,bool recogniseAllOptions_in
94  ,bool addOutputSetupOptions_in
95  )
96  :throwExceptions_(throwExceptions_in)
97  ,recogniseAllOptions_(recogniseAllOptions_in)
98  ,addOutputSetupOptions_(addOutputSetupOptions_in)
99  ,output_all_front_matter_(output_all_front_matter_default_)
100  ,output_show_line_prefix_(output_show_line_prefix_default_)
101  ,output_show_tab_count_(output_show_tab_count_default_)
102  ,output_show_proc_rank_(output_show_proc_rank_default_)
103  ,output_to_root_rank_only_(output_to_root_rank_only_default_)
104  ,print_rcpnode_statistics_on_exit_(print_rcpnode_statistics_on_exit_default_)
105  ,show_timer_summary_on_exit_(show_timer_summary_on_exit_default_)
106  ,printed_timer_summary_(false)
107  ,added_extra_output_setup_options_(false)
108  ,in_add_extra_output_setup_options_(false)
109 {}
110 
111 
113 {
115 }
116 
117 
118 // Set up options
119 
120 
121 void CommandLineProcessor::setDocString( const char doc_string[] )
122 {
123  doc_string_ = doc_string;
124 }
125 
126 
128  const char option_true[]
129  ,const char option_false[]
130  ,bool *option_val
131  ,const char documentation[]
132  )
133 {
134  add_extra_output_setup_options();
135  TEUCHOS_TEST_FOR_EXCEPT(!(option_val!=NULL));
136  options_list_[std::string(option_true)]
137  = opt_val_val_t(OPT_BOOL_TRUE,any(option_val),false);
138  options_list_[std::string(option_false)]
139  = opt_val_val_t(OPT_BOOL_FALSE,any(option_val),false);
140  options_documentation_list_.push_back(
141  opt_doc_t(OPT_BOOL_TRUE, option_true, option_false,
142  std::string(documentation?documentation:""), any(option_val))
143  );
144 }
145 
146 
148  const char option_name[]
149  ,int *option_val
150  ,const char documentation[]
151  ,const bool required
152  )
153 {
154  add_extra_output_setup_options();
155  TEUCHOS_TEST_FOR_EXCEPT(!(option_val!=NULL));
156  options_list_[std::string(option_name)]
157  = opt_val_val_t(OPT_INT,any(option_val),required);
158  options_documentation_list_.push_back(
159  opt_doc_t(OPT_INT, option_name, "", std::string(documentation?documentation:""),
160  any(option_val))
161  );
162 }
163 
164 
166  const char option_name[]
167  ,long int *option_val
168  ,const char documentation[]
169  ,const bool required
170  )
171 {
172  add_extra_output_setup_options();
173  TEUCHOS_TEST_FOR_EXCEPT(!(option_val!=NULL));
174  options_list_[std::string(option_name)]
175  = opt_val_val_t(OPT_LONG_INT,any(option_val),required);
176  options_documentation_list_.push_back(
177  opt_doc_t(OPT_LONG_INT, option_name, "", std::string(documentation?documentation:""),
178  any(option_val))
179  );
180 }
181 
182 
183 #ifdef HAVE_TEUCHOS_LONG_LONG_INT
185  const char option_name[]
186  ,long long int *option_val
187  ,const char documentation[]
188  ,const bool required
189  )
190 {
191  add_extra_output_setup_options();
192  TEUCHOS_TEST_FOR_EXCEPT(!(option_val!=NULL));
193  options_list_[std::string(option_name)]
194  = opt_val_val_t(OPT_LONG_LONG_INT,any(option_val),required);
195  options_documentation_list_.push_back(
196  opt_doc_t(OPT_LONG_LONG_INT, option_name, "", std::string(documentation?documentation:""),
197  any(option_val))
198  );
199 }
200 #endif
201 
202 
204  const char option_name[]
205  ,double *option_val
206  ,const char documentation[]
207  ,const bool required
208  )
209 {
210  add_extra_output_setup_options();
211  TEUCHOS_TEST_FOR_EXCEPT(!(option_val!=NULL));
212  options_list_[std::string(option_name)]
213  = opt_val_val_t(OPT_DOUBLE,any(option_val),required);
214  options_documentation_list_.push_back(
215  opt_doc_t(OPT_DOUBLE, option_name, "", std::string(documentation?documentation:""),
216  any(option_val))
217  );
218 }
219 
220 
222  const char option_name[]
223  ,std::string *option_val
224  ,const char documentation[]
225  ,const bool required
226  )
227 {
228  add_extra_output_setup_options();
229  TEUCHOS_TEST_FOR_EXCEPT(!(option_val!=NULL));
230  options_list_[std::string(option_name)]
231  = opt_val_val_t(OPT_STRING,any(option_val),required);
232  options_documentation_list_.push_back(
233  opt_doc_t(OPT_STRING, option_name, "", std::string(documentation?documentation:""),
234  any(option_val))
235  );
236 }
237 
238 
239 // Parse command line
240 
241 
244  int argc
245  ,char* argv[]
246  ,std::ostream *errout
247  ) const
248 {
249  add_extra_output_setup_options();
250  std::string opt_name;
251  std::string opt_val_str;
252  const std::string echo_cl_opt = "echo-command-line";
253  const std::string help_opt = "help";
254  const std::string pause_opt = "pause-for-debugging";
255  int procRank = GlobalMPISession::getRank();
256  for( int i = 1; i < argc; ++i ) {
257  bool gov_return = get_opt_val( argv[i], &opt_name, &opt_val_str );
258  if( !gov_return ) {
259  if(procRank == 0)
260  print_bad_opt(i,argv,errout);
261  if( recogniseAllOptions() )
263  else {
264  continue;
265  }
266  }
267  if( opt_name == echo_cl_opt ) {
268  if(errout && procRank == 0) {
269  *errout << "\nEchoing the command-line:\n\n";
270  for( int j = 0; j < argc; ++j )
271  *errout << argv[j] << " ";
272  *errout << "\n\n";
273  }
274  continue;
275  }
276  if( opt_name == help_opt ) {
277  if(errout) printHelpMessage( argv[0], *errout );
278  return PARSE_HELP_PRINTED;
279  }
280  if( opt_name == pause_opt ) {
281  if(procRank == 0) {
282  std::cerr << "\nType 0 and press enter to continue : ";
283  int dummy_int = 0;
284  std::cin >> dummy_int;
285  }
287  continue;
288  }
289  // Lookup the option (we had better find it!)
290  options_list_t::iterator itr = options_list_.find(opt_name);
291  if( itr == options_list_.end() ) {
292  if(procRank == 0)
293  print_bad_opt(i,argv,errout);
294  if( recogniseAllOptions() )
296  else
297  continue;
298  }
299  // Changed access to second value of std::map to not use overloaded arrow operator,
300  // otherwise this code will not compile on Janus (HKT, 12/01/2003)
301  opt_val_val_t &opt_val_val = (*itr).second;
302  opt_val_val.was_read = true;
303  switch( opt_val_val.opt_type ) {
304  case OPT_BOOL_TRUE:
305  *(any_cast<bool*>(opt_val_val.opt_val)) = true;
306  break;
307  case OPT_BOOL_FALSE:
308  *(any_cast<bool*>(opt_val_val.opt_val)) = false;
309  break;
310  case OPT_INT:
311  *(any_cast<int*>(opt_val_val.opt_val)) = asSafe<int> (opt_val_str);
312  break;
313  case OPT_LONG_INT:
314  *(any_cast<long int*>(opt_val_val.opt_val)) = asSafe<long int> (opt_val_str);
315  break;
316 #ifdef HAVE_TEUCHOS_LONG_LONG_INT
317  case OPT_LONG_LONG_INT:
318  *(any_cast<long long int*>(opt_val_val.opt_val)) = asSafe<long long int> (opt_val_str);
319  break;
320 #endif
321  case OPT_DOUBLE:
322  *(any_cast<double*>(opt_val_val.opt_val)) = asSafe<double> (opt_val_str);
323  break;
324  case OPT_STRING:
325  *(any_cast<std::string*>(opt_val_val.opt_val)) = remove_quotes(opt_val_str);
326  break;
327  case OPT_ENUM_INT:
328  if( !set_enum_value( i, argv, opt_name, any_cast<int>(opt_val_val.opt_val),
329  remove_quotes(opt_val_str), errout ) )
330  {
332  }
333  break;
334  default:
335  TEUCHOS_TEST_FOR_EXCEPT(true); // Local programming error only
336  }
337  }
338  // Look for options that were required but were not set
339  for(
340  options_list_t::const_iterator itr = options_list_.begin();
341  itr != options_list_.end();
342  ++itr
343  )
344  {
345  const opt_val_val_t &opt_val_val = (*itr).second;
346  if( opt_val_val.required && !opt_val_val.was_read ) {
347  const std::string &opt_val_name = (*itr).first;
348 #define CLP_ERR_MSG \
349  "Error, the option --"<<opt_val_name<<" was required but was not set!"
350  if(errout)
351  *errout << std::endl << argv[0] << " : " << CLP_ERR_MSG << std::endl;
352  if( throwExceptions() ) {
353  TEUCHOS_TEST_FOR_EXCEPTION( true, ParseError, CLP_ERR_MSG );
354  }
355  return PARSE_ERROR;
356 #undef CLP_ERR_MSG
357  }
358  }
359  // Set the options of a default stream exists and if we are asked to
362  if (defaultOut.get() && addOutputSetupOptions_) {
363  if (output_all_front_matter_ != output_all_front_matter_default_)
364  defaultOut->setShowAllFrontMatter(output_all_front_matter_);
365  if (output_show_line_prefix_ != output_show_line_prefix_default_)
366  defaultOut->setShowLinePrefix(output_show_line_prefix_);
367  if (output_show_tab_count_ != output_show_tab_count_default_)
368  defaultOut->setShowTabCount(output_show_tab_count_);
369  if (output_show_proc_rank_ != output_show_proc_rank_default_)
370  defaultOut->setShowProcRank(output_show_proc_rank_);
371  if (output_to_root_rank_only_ != output_to_root_rank_only_default_)
372  defaultOut->setOutputToRootOnly(output_to_root_rank_only_);
373  RCPNodeTracer::setPrintRCPNodeStatisticsOnExit(print_rcpnode_statistics_on_exit_);
374  }
375  return PARSE_SUCCESSFUL;
376 }
377 
378 
379 void CommandLineProcessor::printHelpMessage( const char program_name[],
380  std::ostream &out ) const
381 {
382  add_extra_output_setup_options();
383  int procRank = GlobalMPISession::getRank();
384  if (procRank == 0) {
385  using std::setw;
386  using std::endl;
387 
388  const int opt_type_w = 14;
389  const char spc_chars[] = " ";
390 
391  // Get the maximum length of an option name
392  int opt_name_w = 19; // For the 'pause-for-debugging' option
393  options_documentation_list_t::const_iterator itr;
394  for (
395  itr = options_documentation_list_.begin();
396  itr != options_documentation_list_.end();
397  ++itr
398  )
399  {
400  opt_name_w = my_max(opt_name_w,itr->opt_name.length());
401  if( itr->opt_type )
402  opt_name_w = my_max(opt_name_w,itr->opt_name_false.length());
403  }
404  opt_name_w += 2;
405 
406  // Some built-in options
407  out
408  << "Usage: " << program_name << " [options]\n"
409  << spc_chars << "options:\n"
410  << spc_chars
411  << "--"
412 #ifdef HAVE_STD_IOS_BASE_FMTFLAGS
413  << std::left << setw(opt_name_w) << "help"
414  << std::left << setw(opt_type_w) << " "
415 #else
416  << std::setiosflags(std::ios::left) << setw(opt_name_w) << "help"
417  << std::setiosflags(std::ios::left) << setw(opt_type_w) << " "
418 #endif
419  << "Prints this help message"
420  << std::endl
421  << spc_chars
422  << "--"
423 #ifdef HAVE_STD_IOS_BASE_FMTFLAGS
424  << std::left << setw(opt_name_w) << "pause-for-debugging"
425  << std::left << setw(opt_type_w) << " "
426 #else
427  << std::setiosflags(std::ios::left) << setw(opt_name_w) << "pause-for-debugging"
428  << std::setiosflags(std::ios::left) << setw(opt_type_w) << " "
429 #endif
430  << "Pauses for user input to allow attaching a debugger"
431  << std::endl
432  << spc_chars
433  << "--"
434 #ifdef HAVE_STD_IOS_BASE_FMTFLAGS
435  << std::left << setw(opt_name_w) << "echo-command-line"
436  << std::left << setw(opt_type_w) << " "
437 #else
438  << std::setiosflags(std::ios::left) << setw(opt_name_w) << "echo-command-line"
439  << std::setiosflags(std::ios::left) << setw(opt_type_w) << " "
440 #endif
441  << "Echo the command-line but continue as normal"
442  << std::endl;
443  for(
444  itr = options_documentation_list_.begin();
445  itr != options_documentation_list_.end();
446  ++itr )
447  {
448  // print top line with option name, type and short documentation string
449  out
450  << spc_chars
451  << "--"
452 #ifdef HAVE_STD_IOS_BASE_FMTFLAGS
453  << std::left << setw(opt_name_w) << itr->opt_name
454  << std::left << setw(opt_type_w) << opt_type_str(itr->opt_type)
455 #else
456  << std::setiosflags(std::ios::left) << setw(opt_name_w) << itr->opt_name
457  << std::setiosflags(std::ios::left) << setw(opt_type_w) << opt_type_str(itr->opt_type)
458 #endif
459  << ( itr->documentation.length() ? itr->documentation.c_str() : "No documentation" )
460  << std::endl;
461  // If an enumeration option then the next line is the value options
462  if( itr->opt_type == OPT_ENUM_INT ) {
463  out
464  << spc_chars
465  << " "
466  << setw(opt_name_w) << ""
467  << setw(opt_type_w) << "";
468  print_enum_opt_names( any_cast<int>(itr->default_val), out );
469  out
470  << std::endl;
471  }
472  // Now print the line that contains the default values
473  if( itr->opt_type == OPT_BOOL_TRUE ) {
474  out
475  << spc_chars
476  << "--"
477  << setw(opt_name_w) << itr->opt_name_false;
478  }
479  else {
480  out
481  << spc_chars
482  << " "
483  << setw(opt_name_w) << " ";
484  }
485  out
486  << setw(opt_type_w) << " "
487  << "(default: ";
488  switch( itr->opt_type ) {
489  case OPT_BOOL_TRUE:
490  out << "--" << ( (*(any_cast<bool*>(itr->default_val))) ?
491  itr->opt_name : itr->opt_name_false );
492  break;
493  case OPT_INT:
494  case OPT_LONG_INT:
495 #ifdef HAVE_TEUCHOS_LONG_LONG_INT
496  case OPT_LONG_LONG_INT:
497 #endif
498  case OPT_DOUBLE:
499  case OPT_STRING:
500  case OPT_ENUM_INT:
501  out << "--" << itr->opt_name;
502  break;
503  default:
504  TEUCHOS_TEST_FOR_EXCEPT(true); // Local programming error only
505  }
506  switch( itr->opt_type ) {
507  case OPT_BOOL_TRUE:
508  break;
509  case OPT_INT:
510  out << "=" << (*(any_cast<int*>(itr->default_val)));
511  break;
512  case OPT_LONG_INT:
513  out << "=" << (*(any_cast<long int*>(itr->default_val)));
514  break;
515 #ifdef HAVE_TEUCHOS_LONG_LONG_INT
516  case OPT_LONG_LONG_INT:
517  out << "=" << (*(any_cast<long long int*>(itr->default_val)));
518  break;
519 #endif
520  case OPT_DOUBLE:
521  out << "=" << (*(any_cast<double*>(itr->default_val)));
522  break;
523  case OPT_STRING:
524  out << "=" << add_quotes(*(any_cast<std::string*>(itr->default_val)));
525  break;
526  case OPT_ENUM_INT:
527  out << "=" << add_quotes(
528  enum_opt_default_val_name(itr->opt_name,any_cast<int>(itr->default_val),&out));
529  break;
530  default:
531  TEUCHOS_TEST_FOR_EXCEPT(true); // Local programming error only
532  }
533  out << ")\n";
534  }
535  if(doc_string_.length()) {
536  out << "\nDETAILED DOCUMENTATION:\n\n" << doc_string_ << std::endl << std::endl;
537  }
538  if(throwExceptions_)
539  TEUCHOS_TEST_FOR_EXCEPTION( true, HelpPrinted, "Help message was printed" );
540  }
541 }
542 
543 
545  const Ptr<std::ostream> &out_inout
546  )
547 {
548  if (!printed_timer_summary_ && show_timer_summary_on_exit_) {
549  RCP<std::ostream> out;
550  if (nonnull(out_inout)) {
551  out = rcpFromPtr(out_inout);
552  }
553  else {
555  }
556  getTimeMonitorSurrogate()->summarize(*out << "\n");
557  printed_timer_summary_ = true;
558  }
559 }
560 
561 
562 // private
563 
564 
565 void CommandLineProcessor::add_extra_output_setup_options() const
566 {
567  if(
568  // Are we in this function already and calling it recursively?
569  in_add_extra_output_setup_options_
570  ||
571  // Have we already setup these options?
572  added_extra_output_setup_options_
573  ||
574  // Are we not supposed to setup these options?
575  !addOutputSetupOptions_
576  )
577  {
578  return; // If any of the above is true, we need to return right away!
579  }
580  // Set the commandline options for this ...
582  *clp = const_cast<CommandLineProcessor*>(this);
583  clp->in_add_extra_output_setup_options_ = true;
584  clp->setOption(
585  "output-all-front-matter","output-no-front-matter",&clp->output_all_front_matter_
586  ,"Set if all front matter is printed to the default FancyOStream or not"
587  );
588  clp->setOption(
589  "output-show-line-prefix","output-no-show-line-prefix",&clp->output_show_line_prefix_
590  ,"Set if the line prefix matter is printed to the default FancyOStream or not"
591  );
592  clp->setOption(
593  "output-show-tab-count","output-no-show-tab-count",&clp->output_show_tab_count_
594  ,"Set if the tab count is printed to the default FancyOStream or not"
595  );
596  clp->setOption(
597  "output-show-proc-rank","output-no-show-proc-rank",&clp->output_show_proc_rank_
598  ,"Set if the processor rank is printed to the default FancyOStream or not"
599  );
600  clp->setOption(
601  "output-to-root-rank-only",&clp->output_to_root_rank_only_
602  ,"Set which processor (the root) gets the output. If < 0, then all processors get output."
603  );
604  clp->setOption(
605  "print-rcpnode-statistics-on-exit", "no-print-rcpnode-statistics-on-exit",
606  &clp->print_rcpnode_statistics_on_exit_,
607  "Set if the RCPNode usage statistics will be printed on exit or not. Warning,"
608  " this prints to std::cerr or every process so do not turn this on for very large"
609  " parallel runs."
610  );
611  if (nonnull(getTimeMonitorSurrogate())) {
612  clp->setOption(
613  "show-timer-summary", "no-show-timer-sumary", &clp->show_timer_summary_on_exit_,
614  "If true, then Teuchos::TimeMonitor::summarize() is called in"
615  " CommandLineProcessor's destructor (usually at the end of main)."
616  );
617  }
618 
619  clp->added_extra_output_setup_options_ = true;
620  clp->in_add_extra_output_setup_options_ = false;
621 }
622 
623 
624 void CommandLineProcessor::setEnumOption(
625  const char enum_option_name[]
626  ,int *enum_option_val
627  ,const int num_enum_opt_values
628  ,const int enum_opt_values[]
629  ,const char* enum_opt_names[]
630  ,const char documentation[]
631  ,const bool required
632  )
633 {
634  add_extra_output_setup_options();
635 
636  TEUCHOS_TEST_FOR_EXCEPT(enum_option_val==NULL);
637  TEUCHOS_TEST_FOR_EXCEPT(num_enum_opt_values<=0);
638  TEUCHOS_TEST_FOR_EXCEPT(enum_opt_values==NULL);
639  TEUCHOS_TEST_FOR_EXCEPT(enum_opt_names==NULL);
640 
641  enum_opt_data_list_.push_back(
642  enum_opt_data_t(enum_option_val,num_enum_opt_values,enum_opt_values,enum_opt_names)
643  );
644  const int opt_id = enum_opt_data_list_.size()-1;
645  options_list_[std::string(enum_option_name)]
646  = opt_val_val_t(OPT_ENUM_INT,any(opt_id),required);
647  options_documentation_list_.push_back(
648  opt_doc_t(OPT_ENUM_INT,enum_option_name, "",
649  std::string(documentation?documentation:""), any(opt_id))
650  );
651 }
652 
653 
654 bool CommandLineProcessor::set_enum_value(
655  int argv_i
656  ,char* argv[]
657  ,const std::string &enum_opt_name
658  ,const int enum_id
659  ,const std::string &enum_str_val
660  ,std::ostream *errout
661  ) const
662 {
663  const enum_opt_data_t
664  &enum_opt_data = enum_opt_data_list_.at(enum_id);
665  std::vector<std::string>::const_iterator
666  itr_begin = enum_opt_data.enum_opt_names.begin(),
667  itr_end = enum_opt_data.enum_opt_names.end(),
668  itr = std::find( itr_begin, itr_end, enum_str_val );
669  if( itr == itr_end ) {
670  const int j = argv_i;
671 #define CLP_ERR_MSG \
672  "Error, the value \"" << enum_str_val << "\" for the " \
673  << j<<(j==1?"st":(j==2?"nd":(j==3?"rd":"th"))) << " option --" \
674  << enum_opt_name << " was not recognized (use --help)!"
675  if(errout)
676  *errout << std::endl << argv[0] << " : " << CLP_ERR_MSG << std::endl;
677  if( throwExceptions() ) {
678  TEUCHOS_TEST_FOR_EXCEPTION( true, UnrecognizedOption, CLP_ERR_MSG );
679  }
680  else {
681  return false;
682  }
683 #undef CLP_ERR_MSG
684  }
685  const int enum_opt_val_index = itr - itr_begin;
686  *enum_opt_data.enum_option_val = enum_opt_data.enum_opt_values.at(enum_opt_val_index);
687  return true;
688 }
689 
690 
691 void CommandLineProcessor::print_enum_opt_names(
692  const int enum_id
693  ,std::ostream &out
694  ) const
695 {
696  const enum_opt_data_t
697  &enum_opt_data = enum_opt_data_list_.at(enum_id);
698  typedef std::vector<std::string>::const_iterator itr_t;
699  out << "Valid options:";
700  for(
701  itr_t itr = enum_opt_data.enum_opt_names.begin();
702  itr != enum_opt_data.enum_opt_names.end();
703  ++itr
704  )
705  {
706  if( itr != enum_opt_data.enum_opt_names.begin() ) out << ",";
707  out << " " << add_quotes(*itr);
708  }
709 }
710 
711 
712 std::string
713 CommandLineProcessor::enum_opt_default_val_name(
714  const std::string &enum_name
715  ,const int enum_id
716  ,std::ostream *errout
717  ) const
718 {
719  const enum_opt_data_t
720  &enum_opt_data = enum_opt_data_list_.at(enum_id);
721  return enum_opt_data.enum_opt_names.at(
722  find_enum_opt_index(
723  enum_name,*enum_opt_data.enum_option_val,enum_opt_data,errout
724  )
725  );
726 }
727 
728 
729 int CommandLineProcessor::find_enum_opt_index(
730  const std::string &enum_opt_name
731  ,const int opt_value
732  ,const enum_opt_data_t &enum_data
733  ,std::ostream *errout
734  ) const
735 {
736  std::vector<int>::const_iterator
737  itr_begin = enum_data.enum_opt_values.begin(),
738  itr_end = enum_data.enum_opt_values.end(),
739  itr = std::find( itr_begin, itr_end, opt_value );
740  if( itr == itr_end ) {
741 #define CLP_ERR_MSG \
742  ( recogniseAllOptions() ? "Error" : "Warning" ) \
743  << ", option --" << enum_opt_name << " was given an invalid " \
744  "initial option value of " << opt_value << "!"
745  if(errout)
746  *errout << CLP_ERR_MSG << std::endl;
747  if( throwExceptions() )
748  TEUCHOS_TEST_FOR_EXCEPTION( true, std::invalid_argument, CLP_ERR_MSG );
749 #undef CLP_ERR_MSG
750  }
751  return itr - itr_begin;
752 }
753 
754 
755 bool CommandLineProcessor::get_opt_val(
756  const char str[]
757  ,std::string *opt_name
758  ,std::string *opt_val_str
759  ) const
760 {
761  const int len = std::strlen(str);
762  if( len < 3 )
763  return false; // Can't be an option with '--' followed by at least one char
764  if( str[0] != '-' || str[1] != '-' )
765  return false; // Not a recognised option
766  // Find the '='
767  int equ_i;
768  for( equ_i = 2; equ_i < len && str[equ_i] != '='; ++equ_i );
769  // Set opt_name
770  opt_name->assign( str + 2, equ_i-2 );
771  // Set opt_val_str
772  if( equ_i == len ) {
773  *opt_val_str = "";
774  }
775  else {
776  opt_val_str->assign( str + equ_i + 1, len - equ_i - 1 );
777  }
778  return true;
779 }
780 
781 void CommandLineProcessor::print_bad_opt(
782  int argv_i
783  ,char* argv[]
784  ,std::ostream *errout
785  ) const
786 {
787  const int j = argv_i;
788 #define CLP_ERR_MSG \
789  ( recogniseAllOptions() ? "Error" : "Warning" ) \
790  << ", the " << j<<(j==1?"st":(j==2?"nd":(j==3?"rd":"th"))) \
791  << " option \'" << argv[argv_i] << "\' was not recognized (use --help)!"
792  if(errout)
793  *errout << std::endl << argv[0] << " : " << CLP_ERR_MSG << std::endl;
795  TEUCHOS_TEST_FOR_EXCEPTION( true, UnrecognizedOption, CLP_ERR_MSG );
796 #undef CLP_ERR_MSG
797 }
798 
799 
800 // Hidden stuff
801 
802 
803 void CommandLineProcessor::setTimeMonitorSurrogate(
804  const RCP<CommandLineProcessor::TimeMonitorSurrogate> &timeMonitorSurrogate)
805 {
806  getRawTimeMonitorSurrogate() = timeMonitorSurrogate;
807 }
808 
809 
811 CommandLineProcessor::getTimeMonitorSurrogate()
812 {
813  return getRawTimeMonitorSurrogate();
814 }
815 
816 
818 CommandLineProcessor::getRawTimeMonitorSurrogate()
819 {
820  static RCP<TimeMonitorSurrogate> timeMonitorSurrogate;
821  return timeMonitorSurrogate;
822 }
823 
824 
825 } // end namespace Teuchos
static int getRank()
Returns the process rank relative to MPI_COMM_WORLD
Thrown if –help was specified and throwExceptions==true.
void printHelpMessage(const char program_name[], std::ostream &out) const
Print the help message.
bool throwExceptions() const
Returns true if an std::exception is thrown, there is a parse error, or help is printed.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
static void setPrintRCPNodeStatisticsOnExit(bool printRCPNodeStatisticsOnExit)
Set if RCPNode usage statistics will be printed when the program ends or not.
T * get() const
Get the raw C++ pointer to the underlying object.
CommandLineProcessor(bool throwExceptions=true, bool recogniseAllOptions=true, bool addOutputSetupOptions=false)
Default Constructor.
Modified boost::any class, which is a container for a templated value.
Definition: Teuchos_any.hpp:86
Thrown if an unrecognized option was found and throwExceptions==true.
void setOption(const char option_true[], const char option_false[], bool *option_val, const char documentation[]=NULL)
Set a boolean option.
static RCP< FancyOStream > getDefaultOStream()
Get the default output stream object.
EParseCommandLineReturn parse(int argc, char *argv[], std::ostream *errout=&std::cerr) const
Parse a command line.
EParseCommandLineReturn
Return value for CommandLineProcessor::parse(). Note: These enums are all given non-negative values s...
bool nonnull(const boost::shared_ptr< T > &p)
Returns true if p.get()!=NULL.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos, as well as a number of utility routines.
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.
static void barrier()
Perform a barrier so that all processes line up.
bool recogniseAllOptions() const
Returns true if all options must be recognized by the parser.
Smart reference counting pointer class for automatic garbage collection.
void printFinalTimerSummary(const Ptr< std::ostream > &out=null)
Call to print timers so that they don&#39;t get printed in the destructor.
Definition of Teuchos::as, for conversions between types.
Thrown if a parse std::exception occurs and throwExceptions==true.
Class that helps parse command line input arguments from (argc,argv[]) and set options.
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
This macro is designed to be a short version of TEUCHOS_TEST_FOR_EXCEPTION() that is easier to call...
Simple wrapper class for raw pointers to single objects where no persisting relationship exists...