16#include "Teuchos_VerboseObject.hpp"
18#include "Teuchos_Assert.hpp"
29inline int my_max(
int a,
int b ) {
return a > b ? a : b; }
32std::string remove_quotes(
const std::string& str )
34 if (str[0] ==
'\"' && str[str.size()-1] ==
'\"')
35 return str.substr(1,str.size()-2);
36 else if (str[0] ==
'\"')
37 return str.substr(1,str.size()-1);
38 else if (str[str.size()-1] ==
'\"')
39 return str.substr(0,str.size()-1);
44std::string add_quotes(
const std::string& str )
48 return "\"" + str +
"\"";
58const bool CommandLineProcessor::output_all_front_matter_default_(
false);
59const bool CommandLineProcessor::output_show_line_prefix_default_(
false);
60const bool CommandLineProcessor::output_show_tab_count_default_(
false);
61const bool CommandLineProcessor::output_show_proc_rank_default_(
false);
62const int CommandLineProcessor::output_to_root_rank_only_default_(0);
63const bool CommandLineProcessor::print_rcpnode_statistics_on_exit_default_(
false);
64const bool CommandLineProcessor::show_timer_summary_on_exit_default_(
false);
65const bool CommandLineProcessor::print_system_info_default_(
false);
76 ,output_all_front_matter_(output_all_front_matter_default_)
77 ,output_show_line_prefix_(output_show_line_prefix_default_)
78 ,output_show_tab_count_(output_show_tab_count_default_)
79 ,output_show_proc_rank_(output_show_proc_rank_default_)
80 ,output_to_root_rank_only_(output_to_root_rank_only_default_)
81 ,print_rcpnode_statistics_on_exit_(print_rcpnode_statistics_on_exit_default_)
82 ,show_timer_summary_on_exit_(show_timer_summary_on_exit_default_)
83 ,print_system_info_(print_system_info_default_)
84 ,printed_timer_summary_(
false)
85 ,added_extra_output_setup_options_(
false)
86 ,in_add_extra_output_setup_options_(
false)
88 SystemInformation::initializeCollection();
111 ,
const char documentation[]
114 add_extra_output_setup_options();
120 options_documentation_list_.push_back(
122 std::string(documentation?documentation:
""),
any(
option_val))
130 ,
const char documentation[]
134 add_extra_output_setup_options();
138 options_documentation_list_.push_back(
139 opt_doc_t(OPT_INT,
option_name,
"", std::string(documentation?documentation:
""),
148 ,
const char documentation[]
152 add_extra_output_setup_options();
156 options_documentation_list_.push_back(
157 opt_doc_t(OPT_LONG_INT,
option_name,
"", std::string(documentation?documentation:
""),
166 ,
const char documentation[]
170 add_extra_output_setup_options();
174 options_documentation_list_.push_back(
175 opt_doc_t(OPT_SIZE_T,
option_name,
"", std::string(documentation?documentation:
""),
183 ,
const char documentation[]
187 add_extra_output_setup_options();
191 options_documentation_list_.push_back(
192 opt_doc_t(OPT_LONG_LONG_INT,
option_name,
"", std::string(documentation?documentation:
""),
200 ,
const char documentation[]
204 add_extra_output_setup_options();
208 options_documentation_list_.push_back(
209 opt_doc_t(OPT_DOUBLE,
option_name,
"", std::string(documentation?documentation:
""),
217 ,
const char documentation[]
221 add_extra_output_setup_options();
225 options_documentation_list_.push_back(
226 opt_doc_t(OPT_FLOAT,
option_name,
"", std::string(documentation?documentation:
""),
234 ,
const char documentation[]
238 add_extra_output_setup_options();
242 options_documentation_list_.push_back(
243 opt_doc_t(OPT_STRING,
option_name,
"", std::string(documentation?documentation:
""),
259 add_extra_output_setup_options();
261 if (options_list_.find(
"print-system-info") == options_list_.end()) {
263 clp->
setOption(
"print-system-info",
"no-print-system-info",
264 &clp->print_system_info_,
265 "If true, then collect and print information about the system "
266 "we are running on.");
270 std::string opt_name;
272 const std::string
echo_cl_opt =
"echo-command-line";
273 const std::string
help_opt =
"help";
274 const std::string
pause_opt =
"pause-for-debugging";
279 for(
int i = 1;
i <
argc; ++
i ) {
287 for(
int i = 1;
i <
argc; ++
i ) {
300 *
errout <<
"\nEchoing the command-line:\n\n";
301 for(
int j = 0;
j <
argc; ++
j )
315 std::cerr <<
"Rank " <<
k <<
" has PID " <<
pids[
k] << std::endl;
318 std::cerr <<
"\nType 0 and press enter to continue : ";
326 options_list_t::iterator
itr = options_list_.find(opt_name);
327 if(
itr == options_list_.end() ) {
355 case OPT_LONG_LONG_INT:
380 options_list_t::const_iterator
itr = options_list_.begin();
381 itr != options_list_.end();
389 "Error, the option --"<<opt_val_name<<" was required but was not set!"
391 *
errout << std::endl <<
argv[0] <<
" : " << CLP_ERR_MSG << std::endl;
403 if (output_all_front_matter_ != output_all_front_matter_default_)
404 defaultOut->setShowAllFrontMatter(output_all_front_matter_);
405 if (output_show_line_prefix_ != output_show_line_prefix_default_)
406 defaultOut->setShowLinePrefix(output_show_line_prefix_);
407 if (output_show_tab_count_ != output_show_tab_count_default_)
408 defaultOut->setShowTabCount(output_show_tab_count_);
409 if (output_show_proc_rank_ != output_show_proc_rank_default_)
410 defaultOut->setShowProcRank(output_show_proc_rank_);
411 if (output_to_root_rank_only_ != output_to_root_rank_only_default_)
412 defaultOut->setOutputToRootOnly(output_to_root_rank_only_);
419 auto systemInfo = SystemInformation::collectSystemInformation();
421 std::cout <<
e.first <<
": " <<
e.second << std::endl;
433 std::ostream &out )
const
435 add_extra_output_setup_options();
446 options_documentation_list_t::const_iterator
itr;
448 itr = options_documentation_list_.begin();
449 itr != options_documentation_list_.end();
465#ifdef HAVE_STD_IOS_BASE_FMTFLAGS
472 <<
"Prints this help message"
476#ifdef HAVE_STD_IOS_BASE_FMTFLAGS
480 << std::setiosflags(std::ios::left) <<
setw(
opt_name_w) <<
"pause-for-debugging"
483 <<
"Pauses for user input to allow attaching a debugger"
487#ifdef HAVE_STD_IOS_BASE_FMTFLAGS
491 << std::setiosflags(std::ios::left) <<
setw(
opt_name_w) <<
"echo-command-line"
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
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 ) {
526 if(
itr->opt_type == OPT_BOOL_TRUE ) {
541 switch(
itr->opt_type ) {
544 itr->opt_name :
itr->opt_name_false );
549 case OPT_LONG_LONG_INT:
554 out <<
"--" <<
itr->opt_name;
559 switch(
itr->opt_type ) {
571 case OPT_LONG_LONG_INT:
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_) {
613 getTimeMonitorSurrogate()->summarize(*out <<
"\n");
614 printed_timer_summary_ =
true;
622void CommandLineProcessor::add_extra_output_setup_options()
const
626 in_add_extra_output_setup_options_
629 added_extra_output_setup_options_
632 !addOutputSetupOptions_
639 *clp =
const_cast<CommandLineProcessor*
>(
this);
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;
681void 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 *
const 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))
711bool 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;
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);
748void 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);
770CommandLineProcessor::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
786int 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 ) {
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);
812bool 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 );
838void CommandLineProcessor::print_bad_opt(
841 ,std::ostream *errout
844 const int j = argv_i;
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;
860void CommandLineProcessor::setTimeMonitorSurrogate(
861 const RCP<CommandLineProcessor::TimeMonitorSurrogate> &timeMonitorSurrogate)
863 getRawTimeMonitorSurrogate() = timeMonitorSurrogate;
867RCP<CommandLineProcessor::TimeMonitorSurrogate>
868CommandLineProcessor::getTimeMonitorSurrogate()
870 return getRawTimeMonitorSurrogate();
874RCP<CommandLineProcessor::TimeMonitorSurrogate>&
875CommandLineProcessor::getRawTimeMonitorSurrogate()
877 static RCP<TimeMonitorSurrogate> timeMonitorSurrogate;
878 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.
Class that helps parse command line input arguments from (argc,argv[]) and set options.
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.
Smart reference counting pointer class for automatic garbage collection.
T * get() const
Get the raw C++ pointer to the underlying object.
static void setPrintRCPNodeStatisticsOnExit(bool printRCPNodeStatisticsOnExit)
Set if RCPNode usage statistics will be printed when the program ends or not.
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,...