16#include "Teuchos_VerboseObject.hpp"
18#include "Teuchos_Assert.hpp"
28inline int my_max(
int a,
int b ) {
return a > b ? a : b; }
31std::string remove_quotes(
const std::string& str )
33 if (str[0] ==
'\"' && str[str.size()-1] ==
'\"')
34 return str.substr(1,str.size()-2);
35 else if (str[0] ==
'\"')
36 return str.substr(1,str.size()-1);
37 else if (str[str.size()-1] ==
'\"')
38 return str.substr(0,str.size()-1);
43std::string add_quotes(
const std::string& str )
47 return "\"" + str +
"\"";
57const bool CommandLineProcessor::output_all_front_matter_default_(
false);
58const bool CommandLineProcessor::output_show_line_prefix_default_(
false);
59const bool CommandLineProcessor::output_show_tab_count_default_(
false);
60const bool CommandLineProcessor::output_show_proc_rank_default_(
false);
61const int CommandLineProcessor::output_to_root_rank_only_default_(0);
62const bool CommandLineProcessor::print_rcpnode_statistics_on_exit_default_(
false);
63const bool CommandLineProcessor::show_timer_summary_on_exit_default_(
false);
74 ,output_all_front_matter_(output_all_front_matter_default_)
75 ,output_show_line_prefix_(output_show_line_prefix_default_)
76 ,output_show_tab_count_(output_show_tab_count_default_)
77 ,output_show_proc_rank_(output_show_proc_rank_default_)
78 ,output_to_root_rank_only_(output_to_root_rank_only_default_)
79 ,print_rcpnode_statistics_on_exit_(print_rcpnode_statistics_on_exit_default_)
80 ,show_timer_summary_on_exit_(show_timer_summary_on_exit_default_)
81 ,printed_timer_summary_(
false)
82 ,added_extra_output_setup_options_(
false)
83 ,in_add_extra_output_setup_options_(
false)
106 ,
const char documentation[]
109 add_extra_output_setup_options();
115 options_documentation_list_.push_back(
117 std::string(documentation?documentation:
""),
any(
option_val))
125 ,
const char documentation[]
129 add_extra_output_setup_options();
133 options_documentation_list_.push_back(
134 opt_doc_t(OPT_INT,
option_name,
"", std::string(documentation?documentation:
""),
143 ,
const char documentation[]
147 add_extra_output_setup_options();
151 options_documentation_list_.push_back(
152 opt_doc_t(OPT_LONG_INT,
option_name,
"", std::string(documentation?documentation:
""),
161 ,
const char documentation[]
165 add_extra_output_setup_options();
169 options_documentation_list_.push_back(
170 opt_doc_t(OPT_SIZE_T,
option_name,
"", std::string(documentation?documentation:
""),
178 ,
const char documentation[]
182 add_extra_output_setup_options();
186 options_documentation_list_.push_back(
187 opt_doc_t(OPT_LONG_LONG_INT,
option_name,
"", std::string(documentation?documentation:
""),
195 ,
const char documentation[]
199 add_extra_output_setup_options();
203 options_documentation_list_.push_back(
204 opt_doc_t(OPT_DOUBLE,
option_name,
"", std::string(documentation?documentation:
""),
212 ,
const char documentation[]
216 add_extra_output_setup_options();
220 options_documentation_list_.push_back(
221 opt_doc_t(OPT_FLOAT,
option_name,
"", std::string(documentation?documentation:
""),
229 ,
const char documentation[]
233 add_extra_output_setup_options();
237 options_documentation_list_.push_back(
238 opt_doc_t(OPT_STRING,
option_name,
"", std::string(documentation?documentation:
""),
254 add_extra_output_setup_options();
255 std::string opt_name;
257 const std::string
echo_cl_opt =
"echo-command-line";
258 const std::string
help_opt =
"help";
259 const std::string
pause_opt =
"pause-for-debugging";
264 for(
int i = 1;
i <
argc; ++
i ) {
272 for(
int i = 1;
i <
argc; ++
i ) {
285 *
errout <<
"\nEchoing the command-line:\n\n";
286 for(
int j = 0;
j <
argc; ++
j )
300 std::cerr <<
"Rank " <<
k <<
" has PID " <<
pids[
k] << std::endl;
303 std::cerr <<
"\nType 0 and press enter to continue : ";
311 options_list_t::iterator
itr = options_list_.find(opt_name);
312 if(
itr == options_list_.end() ) {
340 case OPT_LONG_LONG_INT:
365 options_list_t::const_iterator
itr = options_list_.begin();
366 itr != options_list_.end();
374 "Error, the option --"<<opt_val_name<<" was required but was not set!"
376 *
errout << std::endl <<
argv[0] <<
" : " << CLP_ERR_MSG << std::endl;
388 if (output_all_front_matter_ != output_all_front_matter_default_)
389 defaultOut->setShowAllFrontMatter(output_all_front_matter_);
390 if (output_show_line_prefix_ != output_show_line_prefix_default_)
391 defaultOut->setShowLinePrefix(output_show_line_prefix_);
392 if (output_show_tab_count_ != output_show_tab_count_default_)
393 defaultOut->setShowTabCount(output_show_tab_count_);
394 if (output_show_proc_rank_ != output_show_proc_rank_default_)
395 defaultOut->setShowProcRank(output_show_proc_rank_);
396 if (output_to_root_rank_only_ != output_to_root_rank_only_default_)
397 defaultOut->setOutputToRootOnly(output_to_root_rank_only_);
405 std::ostream &out )
const
407 add_extra_output_setup_options();
418 options_documentation_list_t::const_iterator
itr;
420 itr = options_documentation_list_.begin();
421 itr != options_documentation_list_.end();
437#ifdef HAVE_STD_IOS_BASE_FMTFLAGS
444 <<
"Prints this help message"
448#ifdef HAVE_STD_IOS_BASE_FMTFLAGS
452 << std::setiosflags(std::ios::left) <<
setw(
opt_name_w) <<
"pause-for-debugging"
455 <<
"Pauses for user input to allow attaching a debugger"
459#ifdef HAVE_STD_IOS_BASE_FMTFLAGS
463 << std::setiosflags(std::ios::left) <<
setw(
opt_name_w) <<
"echo-command-line"
466 <<
"Echo the command-line but continue as normal"
469 itr = options_documentation_list_.begin();
470 itr != options_documentation_list_.end();
477#ifdef HAVE_STD_IOS_BASE_FMTFLAGS
482 << std::setiosflags(std::ios::left) <<
setw(
opt_type_w) << opt_type_str(
itr->opt_type)
484 << (
itr->documentation.length() ?
itr->documentation.c_str() :
"No documentation" )
487 if(
itr->opt_type == OPT_ENUM_INT ) {
498 if(
itr->opt_type == OPT_BOOL_TRUE ) {
513 switch(
itr->opt_type ) {
516 itr->opt_name :
itr->opt_name_false );
521 case OPT_LONG_LONG_INT:
526 out <<
"--" <<
itr->opt_name;
531 switch(
itr->opt_type ) {
543 case OPT_LONG_LONG_INT:
564 if(doc_string_.length()) {
565 out <<
"\nDETAILED DOCUMENTATION:\n\n" << doc_string_ << std::endl << std::endl;
577 if (!printed_timer_summary_ && show_timer_summary_on_exit_) {
585 getTimeMonitorSurrogate()->summarize(*out <<
"\n");
586 printed_timer_summary_ =
true;
594void CommandLineProcessor::add_extra_output_setup_options()
const
598 in_add_extra_output_setup_options_
601 added_extra_output_setup_options_
604 !addOutputSetupOptions_
611 *clp =
const_cast<CommandLineProcessor*
>(
this);
612 clp->in_add_extra_output_setup_options_ =
true;
614 "output-all-front-matter",
"output-no-front-matter",&clp->output_all_front_matter_
615 ,
"Set if all front matter is printed to the default FancyOStream or not"
618 "output-show-line-prefix",
"output-no-show-line-prefix",&clp->output_show_line_prefix_
619 ,
"Set if the line prefix matter is printed to the default FancyOStream or not"
622 "output-show-tab-count",
"output-no-show-tab-count",&clp->output_show_tab_count_
623 ,
"Set if the tab count is printed to the default FancyOStream or not"
626 "output-show-proc-rank",
"output-no-show-proc-rank",&clp->output_show_proc_rank_
627 ,
"Set if the processor rank is printed to the default FancyOStream or not"
630 "output-to-root-rank-only",&clp->output_to_root_rank_only_
631 ,
"Set which processor (the root) gets the output. If < 0, then all processors get output."
634 "print-rcpnode-statistics-on-exit",
"no-print-rcpnode-statistics-on-exit",
635 &clp->print_rcpnode_statistics_on_exit_,
636 "Set if the RCPNode usage statistics will be printed on exit or not. Warning,"
637 " this prints to std::cerr or every process so do not turn this on for very large"
640 if (
nonnull(getTimeMonitorSurrogate())) {
642 "show-timer-summary",
"no-show-timer-sumary", &clp->show_timer_summary_on_exit_,
643 "If true, then Teuchos::TimeMonitor::summarize() is called in"
644 " CommandLineProcessor's destructor (usually at the end of main)."
648 clp->added_extra_output_setup_options_ =
true;
649 clp->in_add_extra_output_setup_options_ =
false;
653void CommandLineProcessor::setEnumOption(
654 const char enum_option_name[],
655 int* enum_option_val,
656 const int num_enum_opt_values,
657 const int enum_opt_values[],
658 const char *
const enum_opt_names[],
659 const char documentation[],
663 add_extra_output_setup_options();
670 enum_opt_data_list_.push_back(
671 enum_opt_data_t(enum_option_val,num_enum_opt_values,enum_opt_values,enum_opt_names)
673 const int opt_id =
static_cast<int>(enum_opt_data_list_.size())-1;
674 options_list_[std::string(enum_option_name)]
675 = opt_val_val_t(OPT_ENUM_INT,any(opt_id),required);
676 options_documentation_list_.push_back(
677 opt_doc_t(OPT_ENUM_INT,enum_option_name,
"",
678 std::string(documentation?documentation:
""), any(opt_id))
683bool CommandLineProcessor::set_enum_value(
686 ,
const std::string &enum_opt_name
688 ,
const std::string &enum_str_val
689 ,std::ostream *errout
692 const enum_opt_data_t
693 &enum_opt_data = enum_opt_data_list_.at(enum_id);
694 std::vector<std::string>::const_iterator
695 itr_begin = enum_opt_data.enum_opt_names.begin(),
696 itr_end = enum_opt_data.enum_opt_names.end(),
697 itr = std::find( itr_begin, itr_end, enum_str_val );
698 if( itr == itr_end ) {
699 const int j = argv_i;
701 "Error, the value \"" << enum_str_val << "\" for the " \
702 << j<<(j==1?"st":(j==2?"nd":(j==3?"rd":"th"))) << " option --" \
703 << enum_opt_name << " was not recognized (use --help)!"
705 *errout << std::endl << argv[0] <<
" : " << CLP_ERR_MSG << std::endl;
714 const int enum_opt_val_index =
static_cast<int>(itr - itr_begin);
715 *enum_opt_data.enum_option_val = enum_opt_data.enum_opt_values.at(enum_opt_val_index);
720void CommandLineProcessor::print_enum_opt_names(
725 const enum_opt_data_t
726 &enum_opt_data = enum_opt_data_list_.at(enum_id);
727 typedef std::vector<std::string>::const_iterator itr_t;
728 out <<
"Valid options:";
730 itr_t itr = enum_opt_data.enum_opt_names.begin();
731 itr != enum_opt_data.enum_opt_names.end();
735 if( itr != enum_opt_data.enum_opt_names.begin() ) out <<
",";
736 out <<
" " << add_quotes(*itr);
742CommandLineProcessor::enum_opt_default_val_name(
743 const std::string &enum_name
745 ,std::ostream *errout
748 const enum_opt_data_t
749 &enum_opt_data = enum_opt_data_list_.at(enum_id);
750 return enum_opt_data.enum_opt_names.at(
752 enum_name,*enum_opt_data.enum_option_val,enum_opt_data,errout
758int CommandLineProcessor::find_enum_opt_index(
759 const std::string &enum_opt_name
761 ,
const enum_opt_data_t &enum_data
762 ,std::ostream *errout
765 std::vector<int>::const_iterator
766 itr_begin = enum_data.enum_opt_values.begin(),
767 itr_end = enum_data.enum_opt_values.end(),
768 itr = std::find( itr_begin, itr_end, opt_value );
769 if( itr == itr_end ) {
771 ( recogniseAllOptions() ? "Error" : "Warning" ) \
772 << ", option --" << enum_opt_name << " was given an invalid " \
773 "initial option value of " << opt_value << "!"
775 *errout << CLP_ERR_MSG << std::endl;
780 return static_cast<int>(itr - itr_begin);
784bool CommandLineProcessor::get_opt_val(
786 ,std::string *opt_name
787 ,std::string *opt_val_str
790 const int len =
static_cast<int>(std::strlen(str));
793 if( str[0] !=
'-' || str[1] !=
'-' )
797 for( equ_i = 2; equ_i < len && str[equ_i] !=
'='; ++equ_i );
799 opt_name->assign( str + 2, equ_i-2 );
805 opt_val_str->assign( str + equ_i + 1, len - equ_i - 1 );
810void CommandLineProcessor::print_bad_opt(
813 ,std::ostream *errout
816 const int j = argv_i;
818 ( recogniseAllOptions() ? "Error" : "Warning" ) \
819 << ", the " << j<<(j==1?"st":(j==2?"nd":(j==3?"rd":"th"))) \
820 << " option \'" << argv[argv_i] << "\' was not recognized (use --help)!"
822 *errout << std::endl << argv[0] <<
" : " << CLP_ERR_MSG << std::endl;
832void CommandLineProcessor::setTimeMonitorSurrogate(
833 const RCP<CommandLineProcessor::TimeMonitorSurrogate> &timeMonitorSurrogate)
835 getRawTimeMonitorSurrogate() = timeMonitorSurrogate;
839RCP<CommandLineProcessor::TimeMonitorSurrogate>
840CommandLineProcessor::getTimeMonitorSurrogate()
842 return getRawTimeMonitorSurrogate();
846RCP<CommandLineProcessor::TimeMonitorSurrogate>&
847CommandLineProcessor::getRawTimeMonitorSurrogate()
849 static RCP<TimeMonitorSurrogate> timeMonitorSurrogate;
850 return timeMonitorSurrogate;
Templated array class derived from the STL std::vector.
Basic command line parser for input from (argc,argv[])
A MPI utilities class, providing methods for initializing, finalizing, and querying the global MPI se...
Definition of Teuchos::as, for conversions between types.
Thrown if –help was specified and throwExceptions==true.
Thrown if a parse std::exception occurs and throwExceptions==true.
bool throwExceptions() const
Returns true if an std::exception is thrown, there is a parse error, or help is printed.
void setOption(const char option_true[], const char option_false[], bool *option_val, const char documentation[]=NULL)
Set a boolean option.
EParseCommandLineReturn
Return value for CommandLineProcessor::parse(). Note: These enums are all given non-negative values s...
@ PARSE_UNRECOGNIZED_OPTION
EParseCommandLineReturn parse(int argc, char *argv[], std::ostream *errout=&std::cerr) const
Parse a command line.
void printHelpMessage(const char program_name[], std::ostream &out) const
Print the help message.
void printFinalTimerSummary(const Ptr< std::ostream > &out=null)
Call to print timers so that they don't get printed in the destructor.
void setDocString(const char doc_string[])
Set a documentation sting for the entire program printed when –help is specified.
~CommandLineProcessor()
Destructor.
CommandLineProcessor(bool throwExceptions=true, bool recogniseAllOptions=true, bool addOutputSetupOptions=false)
Default Constructor.
bool recogniseAllOptions() const
Returns true if all options must be recognized by the parser.
static void barrier()
Call MPI_Barrier() on MPI_COMM_WORLD.
static int getRank()
The rank of the calling process in MPI_COMM_WORLD.
static int getNProc()
The number of processes in MPI_COMM_WORLD.
static void allGather(int localVal, const ArrayView< int > &allVals)
Global all-to-all of a set of integers across processes.
static void setPrintRCPNodeStatisticsOnExit(bool printRCPNodeStatisticsOnExit)
Set if RCPNode usage statistics will be printed when the program ends or not.
Smart reference counting pointer class for automatic garbage collection.
T * get() const
Get the raw C++ pointer to the underlying object.
static RCP< FancyOStream > getDefaultOStream()
Get the default output stream object.
Modified boost::any class, which is a container for a templated value.
#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.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
bool nonnull(const std::shared_ptr< T > &p)
Returns true if p.get()!=NULL.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...