60 inline int my_max(
int a,
int b ) {
return a > b ? a : b; }
63 std::string remove_quotes(
const std::string& str )
67 return str.substr(1,str.size()-2);
71 std::string add_quotes(
const std::string& str )
75 return "\"" + str +
"\"";
95 bool throwExceptions_in
96 ,
bool recogniseAllOptions_in
97 ,
bool addOutputSetupOptions_in
99 :throwExceptions_(throwExceptions_in)
100 ,recogniseAllOptions_(recogniseAllOptions_in)
101 ,addOutputSetupOptions_(addOutputSetupOptions_in)
102 ,output_all_front_matter_(output_all_front_matter_default_)
103 ,output_show_line_prefix_(output_show_line_prefix_default_)
104 ,output_show_tab_count_(output_show_tab_count_default_)
105 ,output_show_proc_rank_(output_show_proc_rank_default_)
106 ,output_to_root_rank_only_(output_to_root_rank_only_default_)
107 ,print_rcpnode_statistics_on_exit_(print_rcpnode_statistics_on_exit_default_)
108 ,show_timer_summary_on_exit_(show_timer_summary_on_exit_default_)
109 ,printed_timer_summary_(false)
110 ,added_extra_output_setup_options_(false)
111 ,in_add_extra_output_setup_options_(false)
131 const char option_true[]
132 ,
const char option_false[]
134 ,
const char documentation[]
145 std::string(documentation?documentation:
""),
any(option_val))
151 const char option_name[]
153 ,
const char documentation[]
162 opt_doc_t(
OPT_INT, option_name,
"", std::string(documentation?documentation:
""),
169 const char option_name[]
170 ,
long int *option_val
171 ,
const char documentation[]
187 const char option_name[]
189 ,
const char documentation[]
204 const char option_name[]
205 ,
long long int *option_val
206 ,
const char documentation[]
221 const char option_name[]
223 ,
const char documentation[]
239 const char option_name[]
240 ,std::string *option_val
241 ,
const char documentation[]
263 ,std::ostream *errout
267 std::string opt_name;
268 std::string opt_val_str;
269 const std::string echo_cl_opt =
"echo-command-line";
270 const std::string help_opt =
"help";
271 const std::string pause_opt =
"pause-for-debugging";
276 for(
int i = 1; i < argc; ++i ) {
277 bool gov_return =
get_opt_val( argv[i], &opt_name, &opt_val_str );
278 if( gov_return && opt_name == help_opt ) {
284 for(
int i = 1; i < argc; ++i ) {
285 bool gov_return =
get_opt_val( argv[i], &opt_name, &opt_val_str );
295 if( opt_name == echo_cl_opt ) {
296 if(errout && procRank == 0) {
297 *errout <<
"\nEchoing the command-line:\n\n";
298 for(
int j = 0; j < argc; ++j )
299 *errout << argv[j] <<
" ";
304 if( opt_name == pause_opt ) {
308 int rank_pid = getpid();
312 std::cerr <<
"Rank " << k <<
" has PID " << pids[k] << std::endl;
315 std::cerr <<
"\nType 0 and press enter to continue : ";
317 std::cin >> dummy_int;
338 *(any_cast<bool*>(opt_val_val.
opt_val)) =
true;
341 *(any_cast<bool*>(opt_val_val.
opt_val)) =
false;
344 *(any_cast<int*>(opt_val_val.
opt_val)) = asSafe<int> (opt_val_str);
347 *(any_cast<long int*>(opt_val_val.
opt_val)) = asSafe<long int> (opt_val_str);
350 *(any_cast<size_t *>(opt_val_val.
opt_val)) = asSafe<size_t> (opt_val_str);
353 *(any_cast<long long int*>(opt_val_val.
opt_val)) = asSafe<long long int> (opt_val_str);
356 *(any_cast<double*>(opt_val_val.
opt_val)) = asSafe<double> (opt_val_str);
359 *(any_cast<std::string*>(opt_val_val.
opt_val)) = remove_quotes(opt_val_str);
363 remove_quotes(opt_val_str), errout ) )
381 const std::string &opt_val_name = (*itr).first;
382 #define CLP_ERR_MSG \
383 "Error, the option --"<<opt_val_name<<" was required but was not set!"
385 *errout << std::endl << argv[0] <<
" : " <<
CLP_ERR_MSG << std::endl;
414 std::ostream &out )
const
422 const int opt_type_w = 14;
423 const char spc_chars[] =
" ";
427 options_documentation_list_t::const_iterator itr;
434 opt_name_w = my_max(opt_name_w,static_cast<int>(itr->opt_name.length()));
436 opt_name_w = my_max(opt_name_w,static_cast<int>(itr->opt_name_false.length()));
442 <<
"Usage: " << program_name <<
" [options]\n"
443 << spc_chars <<
"options:\n"
446 #ifdef HAVE_STD_IOS_BASE_FMTFLAGS
447 << std::left << setw(opt_name_w) <<
"help"
448 << std::left << setw(opt_type_w) <<
" "
450 << std::setiosflags(std::ios::left) << setw(opt_name_w) <<
"help"
451 << std::setiosflags(std::ios::left) << setw(opt_type_w) <<
" "
453 <<
"Prints this help message"
457 #ifdef HAVE_STD_IOS_BASE_FMTFLAGS
458 << std::left << setw(opt_name_w) <<
"pause-for-debugging"
459 << std::left << setw(opt_type_w) <<
" "
461 << std::setiosflags(std::ios::left) << setw(opt_name_w) <<
"pause-for-debugging"
462 << std::setiosflags(std::ios::left) << setw(opt_type_w) <<
" "
464 <<
"Pauses for user input to allow attaching a debugger"
468 #ifdef HAVE_STD_IOS_BASE_FMTFLAGS
469 << std::left << setw(opt_name_w) <<
"echo-command-line"
470 << std::left << setw(opt_type_w) <<
" "
472 << std::setiosflags(std::ios::left) << setw(opt_name_w) <<
"echo-command-line"
473 << std::setiosflags(std::ios::left) << setw(opt_type_w) <<
" "
475 <<
"Echo the command-line but continue as normal"
486 #ifdef HAVE_STD_IOS_BASE_FMTFLAGS
487 << std::left << setw(opt_name_w) << itr->opt_name
488 << std::left << setw(opt_type_w) <<
opt_type_str(itr->opt_type)
490 << std::setiosflags(std::ios::left) << setw(opt_name_w) << itr->opt_name
491 << std::setiosflags(std::ios::left) << setw(opt_type_w) <<
opt_type_str(itr->opt_type)
493 << ( itr->documentation.length() ? itr->documentation.c_str() :
"No documentation" )
500 << setw(opt_name_w) <<
""
501 << setw(opt_type_w) <<
"";
511 << setw(opt_name_w) << itr->opt_name_false;
517 << setw(opt_name_w) <<
" ";
520 << setw(opt_type_w) <<
" "
522 switch( itr->opt_type ) {
524 out <<
"--" << ( (*(any_cast<bool*>(itr->default_val))) ?
525 itr->opt_name : itr->opt_name_false );
534 out <<
"--" << itr->opt_name;
539 switch( itr->opt_type ) {
543 out <<
"=" << (*(any_cast<int*>(itr->default_val)));
546 out <<
"=" << (*(any_cast<long int*>(itr->default_val)));
549 out <<
"=" << (*(any_cast<size_t*>(itr->default_val)));
552 out <<
"=" << (*(any_cast<long long int*>(itr->default_val)));
555 out <<
"=" << (*(any_cast<double*>(itr->default_val)));
558 out <<
"=" << add_quotes(*(any_cast<std::string*>(itr->default_val)));
561 out <<
"=" << add_quotes(
570 out <<
"\nDETAILED DOCUMENTATION:\n\n" <<
doc_string_ << std::endl << std::endl;
585 out = rcpFromPtr(out_inout);
616 *clp = const_cast<CommandLineProcessor*>(
this);
620 ,
"Set if all front matter is printed to the default FancyOStream or not"
624 ,
"Set if the line prefix matter is printed to the default FancyOStream or not"
628 ,
"Set if the tab count is printed to the default FancyOStream or not"
632 ,
"Set if the processor rank is printed to the default FancyOStream or not"
636 ,
"Set which processor (the root) gets the output. If < 0, then all processors get output."
639 "print-rcpnode-statistics-on-exit",
"no-print-rcpnode-statistics-on-exit",
641 "Set if the RCPNode usage statistics will be printed on exit or not. Warning,"
642 " this prints to std::cerr or every process so do not turn this on for very large"
648 "If true, then Teuchos::TimeMonitor::summarize() is called in"
649 " CommandLineProcessor's destructor (usually at the end of main)."
659 const char enum_option_name[]
660 ,
int *enum_option_val
661 ,
const int num_enum_opt_values
662 ,
const int enum_opt_values[]
663 ,
const char* enum_opt_names[]
664 ,
const char documentation[]
676 enum_opt_data_t(enum_option_val,num_enum_opt_values,enum_opt_values,enum_opt_names)
683 std::string(documentation?documentation:
""),
any(opt_id))
691 ,
const std::string &enum_opt_name
693 ,
const std::string &enum_str_val
694 ,std::ostream *errout
699 std::vector<std::string>::const_iterator
702 itr = std::find( itr_begin, itr_end, enum_str_val );
703 if( itr == itr_end ) {
704 const int j = argv_i;
705 #define CLP_ERR_MSG \
706 "Error, the value \"" << enum_str_val << "\" for the " \
707 << j<<(j==1?"st":(j==2?"nd":(j==3?"rd":"th"))) << " option --" \
708 << enum_opt_name << " was not recognized (use --help)!"
710 *errout << std::endl << argv[0] <<
" : " <<
CLP_ERR_MSG << std::endl;
719 const int enum_opt_val_index = static_cast<int>(itr - itr_begin);
732 typedef std::vector<std::string>::const_iterator itr_t;
733 out <<
"Valid options:";
741 out <<
" " << add_quotes(*itr);
748 const std::string &enum_name
750 ,std::ostream *errout
764 const std::string &enum_opt_name
767 ,std::ostream *errout
770 std::vector<int>::const_iterator
773 itr = std::find( itr_begin, itr_end, opt_value );
774 if( itr == itr_end ) {
775 #define CLP_ERR_MSG \
776 ( recogniseAllOptions() ? "Error" : "Warning" ) \
777 << ", option --" << enum_opt_name << " was given an invalid " \
778 "initial option value of " << opt_value << "!"
785 return static_cast<int>(itr - itr_begin);
791 ,std::string *opt_name
792 ,std::string *opt_val_str
795 const int len = static_cast<int>(std::strlen(str));
798 if( str[0] !=
'-' || str[1] !=
'-' )
802 for( equ_i = 2; equ_i < len && str[equ_i] !=
'='; ++equ_i );
804 opt_name->assign( str + 2, equ_i-2 );
810 opt_val_str->assign( str + equ_i + 1, len - equ_i - 1 );
818 ,std::ostream *errout
821 const int j = argv_i;
822 #define CLP_ERR_MSG \
823 ( recogniseAllOptions() ? "Error" : "Warning" ) \
824 << ", the " << j<<(j==1?"st":(j==2?"nd":(j==3?"rd":"th"))) \
825 << " option \'" << argv[argv_i] << "\' was not recognized (use --help)!"
827 *errout << std::endl << argv[0] <<
" : " <<
CLP_ERR_MSG << std::endl;
855 return timeMonitorSurrogate;