48 #include "Teuchos_VerboseObject.hpp"
50 #include "Teuchos_Assert.hpp"
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 +
"\"";
85 const bool CommandLineProcessor::output_all_front_matter_default_(
false);
86 const bool CommandLineProcessor::output_show_line_prefix_default_(
false);
87 const bool CommandLineProcessor::output_show_tab_count_default_(
false);
88 const bool CommandLineProcessor::output_show_proc_rank_default_(
false);
89 const int CommandLineProcessor::output_to_root_rank_only_default_(0);
90 const bool CommandLineProcessor::print_rcpnode_statistics_on_exit_default_(
false);
91 const bool CommandLineProcessor::show_timer_summary_on_exit_default_(
false);
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)
126 doc_string_ = doc_string;
131 const char option_true[]
132 ,
const char option_false[]
134 ,
const char documentation[]
137 add_extra_output_setup_options();
139 options_list_[std::string(option_true)]
140 = opt_val_val_t(OPT_BOOL_TRUE,
any(option_val),
false);
141 options_list_[std::string(option_false)]
142 = opt_val_val_t(OPT_BOOL_FALSE,
any(option_val),
false);
143 options_documentation_list_.push_back(
144 opt_doc_t(OPT_BOOL_TRUE, option_true, option_false,
145 std::string(documentation?documentation:
""),
any(option_val))
151 const char option_name[]
153 ,
const char documentation[]
157 add_extra_output_setup_options();
159 options_list_[std::string(option_name)]
160 = opt_val_val_t(OPT_INT,
any(option_val),required);
161 options_documentation_list_.push_back(
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[]
175 add_extra_output_setup_options();
177 options_list_[std::string(option_name)]
178 = opt_val_val_t(OPT_LONG_INT,
any(option_val),required);
179 options_documentation_list_.push_back(
180 opt_doc_t(OPT_LONG_INT, option_name,
"", std::string(documentation?documentation:
""),
187 const char option_name[]
189 ,
const char documentation[]
193 add_extra_output_setup_options();
195 options_list_[std::string(option_name)]
196 = opt_val_val_t(OPT_SIZE_T,
any(option_val),required);
197 options_documentation_list_.push_back(
198 opt_doc_t(OPT_SIZE_T, option_name,
"", std::string(documentation?documentation:
""),
204 const char option_name[]
205 ,
long long int *option_val
206 ,
const char documentation[]
210 add_extra_output_setup_options();
212 options_list_[std::string(option_name)]
213 = opt_val_val_t(OPT_LONG_LONG_INT,
any(option_val),required);
214 options_documentation_list_.push_back(
215 opt_doc_t(OPT_LONG_LONG_INT, option_name,
"", std::string(documentation?documentation:
""),
221 const char option_name[]
223 ,
const char documentation[]
227 add_extra_output_setup_options();
229 options_list_[std::string(option_name)]
230 = opt_val_val_t(OPT_DOUBLE,
any(option_val),required);
231 options_documentation_list_.push_back(
232 opt_doc_t(OPT_DOUBLE, option_name,
"", std::string(documentation?documentation:
""),
238 const char option_name[]
240 ,
const char documentation[]
244 add_extra_output_setup_options();
246 options_list_[std::string(option_name)]
247 = opt_val_val_t(OPT_FLOAT,
any(option_val),required);
248 options_documentation_list_.push_back(
249 opt_doc_t(OPT_FLOAT, option_name,
"", std::string(documentation?documentation:
""),
255 const char option_name[]
256 ,std::string *option_val
257 ,
const char documentation[]
261 add_extra_output_setup_options();
263 options_list_[std::string(option_name)]
264 = opt_val_val_t(OPT_STRING,
any(option_val),required);
265 options_documentation_list_.push_back(
266 opt_doc_t(OPT_STRING, option_name,
"", std::string(documentation?documentation:
""),
279 ,std::ostream *errout
282 add_extra_output_setup_options();
283 std::string opt_name;
284 std::string opt_val_str;
285 const std::string echo_cl_opt =
"echo-command-line";
286 const std::string help_opt =
"help";
287 const std::string pause_opt =
"pause-for-debugging";
292 for(
int i = 1; i < argc; ++i ) {
293 bool gov_return = get_opt_val( argv[i], &opt_name, &opt_val_str );
294 if( gov_return && opt_name == help_opt ) {
300 for(
int i = 1; i < argc; ++i ) {
301 bool gov_return = get_opt_val( argv[i], &opt_name, &opt_val_str );
304 print_bad_opt(i,argv,errout);
311 if( opt_name == echo_cl_opt ) {
312 if(errout && procRank == 0) {
313 *errout <<
"\nEchoing the command-line:\n\n";
314 for(
int j = 0; j < argc; ++j )
315 *errout << argv[j] <<
" ";
320 if( opt_name == pause_opt ) {
324 int rank_pid = getpid();
328 std::cerr <<
"Rank " << k <<
" has PID " << pids[k] << std::endl;
331 std::cerr <<
"\nType 0 and press enter to continue : ";
333 std::cin >> dummy_int;
339 options_list_t::iterator itr = options_list_.find(opt_name);
340 if( itr == options_list_.end() ) {
342 print_bad_opt(i,argv,errout);
350 opt_val_val_t &opt_val_val = (*itr).second;
351 opt_val_val.was_read =
true;
352 switch( opt_val_val.opt_type ) {
354 *(any_cast<
bool*>(opt_val_val.opt_val)) =
true;
357 *(any_cast<
bool*>(opt_val_val.opt_val)) =
false;
360 *(any_cast<
int*>(opt_val_val.opt_val)) = asSafe<int> (opt_val_str);
363 *(any_cast<
long int*>(opt_val_val.opt_val)) = asSafe<long int> (opt_val_str);
366 *(any_cast<
size_t *>(opt_val_val.opt_val)) = asSafe<size_t> (opt_val_str);
368 case OPT_LONG_LONG_INT:
369 *(any_cast<
long long int*>(opt_val_val.opt_val)) = asSafe<long long int> (opt_val_str);
372 *(any_cast<
double*>(opt_val_val.opt_val)) = asSafe<double> (opt_val_str);
375 *(any_cast<
float*>(opt_val_val.opt_val)) = asSafe<float> (opt_val_str);
378 *(any_cast<std::string*>(opt_val_val.opt_val)) = remove_quotes(opt_val_str);
381 if( !set_enum_value( i, argv, opt_name, any_cast<int>(opt_val_val.opt_val),
382 remove_quotes(opt_val_str), errout ) )
393 options_list_t::const_iterator itr = options_list_.begin();
394 itr != options_list_.end();
398 const opt_val_val_t &opt_val_val = (*itr).second;
399 if( opt_val_val.required && !opt_val_val.was_read ) {
400 const std::string &opt_val_name = (*itr).first;
401 #define CLP_ERR_MSG \
402 "Error, the option --"<<opt_val_name<<" was required but was not set!"
404 *errout << std::endl << argv[0] <<
" : " << CLP_ERR_MSG << std::endl;
415 if (defaultOut.
get() && addOutputSetupOptions_) {
416 if (output_all_front_matter_ != output_all_front_matter_default_)
417 defaultOut->setShowAllFrontMatter(output_all_front_matter_);
418 if (output_show_line_prefix_ != output_show_line_prefix_default_)
419 defaultOut->setShowLinePrefix(output_show_line_prefix_);
420 if (output_show_tab_count_ != output_show_tab_count_default_)
421 defaultOut->setShowTabCount(output_show_tab_count_);
422 if (output_show_proc_rank_ != output_show_proc_rank_default_)
423 defaultOut->setShowProcRank(output_show_proc_rank_);
424 if (output_to_root_rank_only_ != output_to_root_rank_only_default_)
425 defaultOut->setOutputToRootOnly(output_to_root_rank_only_);
433 std::ostream &out )
const
435 add_extra_output_setup_options();
441 const int opt_type_w = 14;
442 const char spc_chars[] =
" ";
446 options_documentation_list_t::const_iterator itr;
448 itr = options_documentation_list_.begin();
449 itr != options_documentation_list_.end();
453 opt_name_w = my_max(opt_name_w,static_cast<int>(itr->opt_name.length()));
455 opt_name_w = my_max(opt_name_w,static_cast<int>(itr->opt_name_false.length()));
461 <<
"Usage: " << program_name <<
" [options]\n"
462 << spc_chars <<
"options:\n"
465 #ifdef HAVE_STD_IOS_BASE_FMTFLAGS
466 << std::left << setw(opt_name_w) <<
"help"
467 << std::left << setw(opt_type_w) <<
" "
469 << std::setiosflags(std::ios::left) << setw(opt_name_w) <<
"help"
470 << std::setiosflags(std::ios::left) << setw(opt_type_w) <<
" "
472 <<
"Prints this help message"
476 #ifdef HAVE_STD_IOS_BASE_FMTFLAGS
477 << std::left << setw(opt_name_w) <<
"pause-for-debugging"
478 << std::left << setw(opt_type_w) <<
" "
480 << std::setiosflags(std::ios::left) << setw(opt_name_w) <<
"pause-for-debugging"
481 << std::setiosflags(std::ios::left) << setw(opt_type_w) <<
" "
483 <<
"Pauses for user input to allow attaching a debugger"
487 #ifdef HAVE_STD_IOS_BASE_FMTFLAGS
488 << std::left << setw(opt_name_w) <<
"echo-command-line"
489 << std::left << setw(opt_type_w) <<
" "
491 << std::setiosflags(std::ios::left) << setw(opt_name_w) <<
"echo-command-line"
492 << std::setiosflags(std::ios::left) << setw(opt_type_w) <<
" "
494 <<
"Echo the command-line but continue as normal"
497 itr = options_documentation_list_.begin();
498 itr != options_documentation_list_.end();
505 #ifdef HAVE_STD_IOS_BASE_FMTFLAGS
506 << std::left << setw(opt_name_w) << itr->opt_name
507 << std::left << setw(opt_type_w) << opt_type_str(itr->opt_type)
509 << std::setiosflags(std::ios::left) << setw(opt_name_w) << itr->opt_name
510 << std::setiosflags(std::ios::left) << setw(opt_type_w) << opt_type_str(itr->opt_type)
512 << ( itr->documentation.length() ? itr->documentation.c_str() :
"No documentation" )
515 if( itr->opt_type == OPT_ENUM_INT ) {
519 << setw(opt_name_w) <<
""
520 << setw(opt_type_w) <<
"";
521 print_enum_opt_names( any_cast<int>(itr->default_val), out );
526 if( itr->opt_type == OPT_BOOL_TRUE ) {
530 << setw(opt_name_w) << itr->opt_name_false;
536 << setw(opt_name_w) <<
" ";
539 << setw(opt_type_w) <<
" "
541 switch( itr->opt_type ) {
543 out <<
"--" << ( (*(any_cast<
bool*>(itr->default_val))) ?
544 itr->opt_name : itr->opt_name_false );
549 case OPT_LONG_LONG_INT:
554 out <<
"--" << itr->opt_name;
559 switch( itr->opt_type ) {
563 out <<
"=" << (*(any_cast<
int*>(itr->default_val)));
566 out <<
"=" << (*(any_cast<
long int*>(itr->default_val)));
569 out <<
"=" << (*(any_cast<
size_t*>(itr->default_val)));
571 case OPT_LONG_LONG_INT:
572 out <<
"=" << (*(any_cast<
long long int*>(itr->default_val)));
575 out <<
"=" << (*(any_cast<
double*>(itr->default_val)));
578 out <<
"=" << (*(any_cast<
float*>(itr->default_val)));
581 out <<
"=" << add_quotes(*(any_cast<std::string*>(itr->default_val)));
584 out <<
"=" << add_quotes(
585 enum_opt_default_val_name(itr->opt_name,any_cast<int>(itr->default_val),&out));
592 if(doc_string_.length()) {
593 out <<
"\nDETAILED DOCUMENTATION:\n\n" << doc_string_ << std::endl << std::endl;
605 if (!printed_timer_summary_ && show_timer_summary_on_exit_) {
608 out = rcpFromPtr(out_inout);
613 getTimeMonitorSurrogate()->summarize(*out <<
"\n");
614 printed_timer_summary_ =
true;
622 void CommandLineProcessor::add_extra_output_setup_options()
const
626 in_add_extra_output_setup_options_
629 added_extra_output_setup_options_
632 !addOutputSetupOptions_
640 clp->in_add_extra_output_setup_options_ =
true;
642 "output-all-front-matter",
"output-no-front-matter",&clp->output_all_front_matter_
643 ,
"Set if all front matter is printed to the default FancyOStream or not"
646 "output-show-line-prefix",
"output-no-show-line-prefix",&clp->output_show_line_prefix_
647 ,
"Set if the line prefix matter is printed to the default FancyOStream or not"
650 "output-show-tab-count",
"output-no-show-tab-count",&clp->output_show_tab_count_
651 ,
"Set if the tab count is printed to the default FancyOStream or not"
654 "output-show-proc-rank",
"output-no-show-proc-rank",&clp->output_show_proc_rank_
655 ,
"Set if the processor rank is printed to the default FancyOStream or not"
658 "output-to-root-rank-only",&clp->output_to_root_rank_only_
659 ,
"Set which processor (the root) gets the output. If < 0, then all processors get output."
662 "print-rcpnode-statistics-on-exit",
"no-print-rcpnode-statistics-on-exit",
663 &clp->print_rcpnode_statistics_on_exit_,
664 "Set if the RCPNode usage statistics will be printed on exit or not. Warning,"
665 " this prints to std::cerr or every process so do not turn this on for very large"
668 if (
nonnull(getTimeMonitorSurrogate())) {
670 "show-timer-summary",
"no-show-timer-sumary", &clp->show_timer_summary_on_exit_,
671 "If true, then Teuchos::TimeMonitor::summarize() is called in"
672 " CommandLineProcessor's destructor (usually at the end of main)."
676 clp->added_extra_output_setup_options_ =
true;
677 clp->in_add_extra_output_setup_options_ =
false;
681 void CommandLineProcessor::setEnumOption(
682 const char enum_option_name[]
683 ,
int *enum_option_val
684 ,
const int num_enum_opt_values
685 ,
const int enum_opt_values[]
686 ,
const char* enum_opt_names[]
687 ,
const char documentation[]
691 add_extra_output_setup_options();
698 enum_opt_data_list_.push_back(
699 enum_opt_data_t(enum_option_val,num_enum_opt_values,enum_opt_values,enum_opt_names)
701 const int opt_id =
static_cast<int>(enum_opt_data_list_.size())-1;
702 options_list_[std::string(enum_option_name)]
703 = opt_val_val_t(OPT_ENUM_INT,any(opt_id),required);
704 options_documentation_list_.push_back(
705 opt_doc_t(OPT_ENUM_INT,enum_option_name,
"",
706 std::string(documentation?documentation:
""), any(opt_id))
711 bool CommandLineProcessor::set_enum_value(
714 ,
const std::string &enum_opt_name
716 ,
const std::string &enum_str_val
717 ,std::ostream *errout
720 const enum_opt_data_t
721 &enum_opt_data = enum_opt_data_list_.at(enum_id);
722 std::vector<std::string>::const_iterator
723 itr_begin = enum_opt_data.enum_opt_names.begin(),
724 itr_end = enum_opt_data.enum_opt_names.end(),
725 itr = std::find( itr_begin, itr_end, enum_str_val );
726 if( itr == itr_end ) {
727 const int j = argv_i;
728 #define CLP_ERR_MSG \
729 "Error, the value \"" << enum_str_val << "\" for the " \
730 << j<<(j==1?"st":(j==2?"nd":(j==3?"rd":"th"))) << " option --" \
731 << enum_opt_name << " was not recognized (use --help)!"
733 *errout << std::endl << argv[0] <<
" : " << CLP_ERR_MSG << std::endl;
742 const int enum_opt_val_index =
static_cast<int>(itr - itr_begin);
743 *enum_opt_data.enum_option_val = enum_opt_data.enum_opt_values.at(enum_opt_val_index);
748 void CommandLineProcessor::print_enum_opt_names(
753 const enum_opt_data_t
754 &enum_opt_data = enum_opt_data_list_.at(enum_id);
755 typedef std::vector<std::string>::const_iterator itr_t;
756 out <<
"Valid options:";
758 itr_t itr = enum_opt_data.enum_opt_names.begin();
759 itr != enum_opt_data.enum_opt_names.end();
763 if( itr != enum_opt_data.enum_opt_names.begin() ) out <<
",";
764 out <<
" " << add_quotes(*itr);
770 CommandLineProcessor::enum_opt_default_val_name(
771 const std::string &enum_name
773 ,std::ostream *errout
776 const enum_opt_data_t
777 &enum_opt_data = enum_opt_data_list_.at(enum_id);
778 return enum_opt_data.enum_opt_names.at(
780 enum_name,*enum_opt_data.enum_option_val,enum_opt_data,errout
786 int CommandLineProcessor::find_enum_opt_index(
787 const std::string &enum_opt_name
789 ,
const enum_opt_data_t &enum_data
790 ,std::ostream *errout
793 std::vector<int>::const_iterator
794 itr_begin = enum_data.enum_opt_values.begin(),
795 itr_end = enum_data.enum_opt_values.end(),
796 itr = std::find( itr_begin, itr_end, opt_value );
797 if( itr == itr_end ) {
798 #define CLP_ERR_MSG \
799 ( recogniseAllOptions() ? "Error" : "Warning" ) \
800 << ", option --" << enum_opt_name << " was given an invalid " \
801 "initial option value of " << opt_value << "!"
803 *errout << CLP_ERR_MSG << std::endl;
808 return static_cast<int>(itr - itr_begin);
812 bool CommandLineProcessor::get_opt_val(
814 ,std::string *opt_name
815 ,std::string *opt_val_str
818 const int len =
static_cast<int>(std::strlen(str));
821 if( str[0] !=
'-' || str[1] !=
'-' )
825 for( equ_i = 2; equ_i < len && str[equ_i] !=
'='; ++equ_i );
827 opt_name->assign( str + 2, equ_i-2 );
833 opt_val_str->assign( str + equ_i + 1, len - equ_i - 1 );
838 void CommandLineProcessor::print_bad_opt(
841 ,std::ostream *errout
844 const int j = argv_i;
845 #define CLP_ERR_MSG \
846 ( recogniseAllOptions() ? "Error" : "Warning" ) \
847 << ", the " << j<<(j==1?"st":(j==2?"nd":(j==3?"rd":"th"))) \
848 << " option \'" << argv[argv_i] << "\' was not recognized (use --help)!"
850 *errout << std::endl << argv[0] <<
" : " << CLP_ERR_MSG << std::endl;
860 void CommandLineProcessor::setTimeMonitorSurrogate(
861 const RCP<CommandLineProcessor::TimeMonitorSurrogate> &timeMonitorSurrogate)
863 getRawTimeMonitorSurrogate() = timeMonitorSurrogate;
867 RCP<CommandLineProcessor::TimeMonitorSurrogate>
868 CommandLineProcessor::getTimeMonitorSurrogate()
870 return getRawTimeMonitorSurrogate();
874 RCP<CommandLineProcessor::TimeMonitorSurrogate>&
875 CommandLineProcessor::getRawTimeMonitorSurrogate()
877 static RCP<TimeMonitorSurrogate> timeMonitorSurrogate;
878 return timeMonitorSurrogate;
static int getRank()
The rank of the calling process in MPI_COMM_WORLD.
Thrown if –help was specified and throwExceptions==true.
static int getNProc()
The number of processes in MPI_COMM_WORLD.
void printHelpMessage(const char program_name[], std::ostream &out) const
Print the help message.
bool nonnull(const std::shared_ptr< T > &p)
Returns true if p.get()!=NULL.
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.
~CommandLineProcessor()
Destructor.
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.
void resize(size_type new_size, const value_type &x=value_type())
EParseCommandLineReturn
Return value for CommandLineProcessor::parse(). Note: These enums are all given non-negative values s...
Templated array class derived from the STL std::vector.
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()
Call MPI_Barrier() on MPI_COMM_WORLD.
bool recogniseAllOptions() const
Returns true if all options must be recognized by the parser.
static void allGather(int localVal, const ArrayView< int > &allVals)
Global all-to-all of a set of integers across processes.
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't get printed in the destructor.
Definition of Teuchos::as, for conversions between types.
Thrown if a parse std::exception occurs and throwExceptions==true.
#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...