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);
69 bool throwExceptions_in
70 ,
bool recogniseAllOptions_in
71 ,
bool addOutputSetupOptions_in
73 :throwExceptions_(throwExceptions_in)
74 ,recogniseAllOptions_(recogniseAllOptions_in)
75 ,addOutputSetupOptions_(addOutputSetupOptions_in)
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();
103 doc_string_ = doc_string;
108 const char option_true[]
109 ,
const char option_false[]
111 ,
const char documentation[]
114 add_extra_output_setup_options();
116 options_list_[std::string(option_true)]
117 = opt_val_val_t(OPT_BOOL_TRUE,
any(option_val),
false);
118 options_list_[std::string(option_false)]
119 = opt_val_val_t(OPT_BOOL_FALSE,
any(option_val),
false);
120 options_documentation_list_.push_back(
121 opt_doc_t(OPT_BOOL_TRUE, option_true, option_false,
122 std::string(documentation?documentation:
""),
any(option_val))
128 const char option_name[]
130 ,
const char documentation[]
134 add_extra_output_setup_options();
136 options_list_[std::string(option_name)]
137 = opt_val_val_t(OPT_INT,
any(option_val),required);
138 options_documentation_list_.push_back(
139 opt_doc_t(OPT_INT, option_name,
"", std::string(documentation?documentation:
""),
146 const char option_name[]
147 ,
long int *option_val
148 ,
const char documentation[]
152 add_extra_output_setup_options();
154 options_list_[std::string(option_name)]
155 = opt_val_val_t(OPT_LONG_INT,
any(option_val),required);
156 options_documentation_list_.push_back(
157 opt_doc_t(OPT_LONG_INT, option_name,
"", std::string(documentation?documentation:
""),
164 const char option_name[]
166 ,
const char documentation[]
170 add_extra_output_setup_options();
172 options_list_[std::string(option_name)]
173 = opt_val_val_t(OPT_SIZE_T,
any(option_val),required);
174 options_documentation_list_.push_back(
175 opt_doc_t(OPT_SIZE_T, option_name,
"", std::string(documentation?documentation:
""),
181 const char option_name[]
182 ,
long long int *option_val
183 ,
const char documentation[]
187 add_extra_output_setup_options();
189 options_list_[std::string(option_name)]
190 = opt_val_val_t(OPT_LONG_LONG_INT,
any(option_val),required);
191 options_documentation_list_.push_back(
192 opt_doc_t(OPT_LONG_LONG_INT, option_name,
"", std::string(documentation?documentation:
""),
198 const char option_name[]
200 ,
const char documentation[]
204 add_extra_output_setup_options();
206 options_list_[std::string(option_name)]
207 = opt_val_val_t(OPT_DOUBLE,
any(option_val),required);
208 options_documentation_list_.push_back(
209 opt_doc_t(OPT_DOUBLE, option_name,
"", std::string(documentation?documentation:
""),
215 const char option_name[]
217 ,
const char documentation[]
221 add_extra_output_setup_options();
223 options_list_[std::string(option_name)]
224 = opt_val_val_t(OPT_FLOAT,
any(option_val),required);
225 options_documentation_list_.push_back(
226 opt_doc_t(OPT_FLOAT, option_name,
"", std::string(documentation?documentation:
""),
232 const char option_name[]
233 ,std::string *option_val
234 ,
const char documentation[]
238 add_extra_output_setup_options();
240 options_list_[std::string(option_name)]
241 = opt_val_val_t(OPT_STRING,
any(option_val),required);
242 options_documentation_list_.push_back(
243 opt_doc_t(OPT_STRING, option_name,
"", std::string(documentation?documentation:
""),
256 ,std::ostream *errout
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;
271 std::string opt_val_str;
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 ) {
280 bool gov_return = get_opt_val( argv[i], &opt_name, &opt_val_str );
281 if( gov_return && opt_name == help_opt ) {
287 for(
int i = 1; i < argc; ++i ) {
288 bool gov_return = get_opt_val( argv[i], &opt_name, &opt_val_str );
291 print_bad_opt(i,argv,errout);
298 if( opt_name == echo_cl_opt ) {
299 if(errout && procRank == 0) {
300 *errout <<
"\nEchoing the command-line:\n\n";
301 for(
int j = 0; j < argc; ++j )
302 *errout << argv[j] <<
" ";
307 if( opt_name == pause_opt ) {
311 int rank_pid = getpid();
315 std::cerr <<
"Rank " << k <<
" has PID " << pids[k] << std::endl;
318 std::cerr <<
"\nType 0 and press enter to continue : ";
320 std::cin >> dummy_int;
326 options_list_t::iterator itr = options_list_.find(opt_name);
327 if( itr == options_list_.end() ) {
329 print_bad_opt(i,argv,errout);
337 opt_val_val_t &opt_val_val = (*itr).second;
338 opt_val_val.was_read =
true;
339 switch( opt_val_val.opt_type ) {
355 case OPT_LONG_LONG_INT:
368 if( !set_enum_value( i, argv, opt_name,
any_cast<int>(opt_val_val.opt_val),
369 remove_quotes(opt_val_str), errout ) )
380 options_list_t::const_iterator itr = options_list_.begin();
381 itr != options_list_.end();
385 const opt_val_val_t &opt_val_val = (*itr).second;
386 if( opt_val_val.required && !opt_val_val.was_read ) {
387 const std::string &opt_val_name = (*itr).first;
389 "Error, the option --"<<opt_val_name<<" was required but was not set!"
391 *errout << std::endl << argv[0] <<
" : " << CLP_ERR_MSG << std::endl;
402 if (defaultOut.
get() && addOutputSetupOptions_) {
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_);
416 static bool alreadyPrintedSystemInfo =
false;
417 if (print_system_info_ && !alreadyPrintedSystemInfo) {
419 auto systemInfo = SystemInformation::collectSystemInformation();
420 for (
const auto &e : systemInfo) {
421 std::cout << e.first <<
": " << e.second << std::endl;
425 alreadyPrintedSystemInfo =
true;
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 ) {
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:
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_) {
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_
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(
863 getRawTimeMonitorSurrogate() = timeMonitorSurrogate;
868CommandLineProcessor::getTimeMonitorSurrogate()
870 return getRawTimeMonitorSurrogate();
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.
Replacement for std::vector that is compatible with the Teuchos Memory Management classes.
void resize(size_type new_size, const value_type &x=value_type())
Thrown if –help was specified and throwExceptions==true.
Thrown if a parse std::exception occurs and throwExceptions==true.
Thrown if an unrecognized option was found 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.
Simple wrapper class for raw pointers to single objects where no persisting relationship exists.
Smart reference counting pointer class for automatic garbage collection.
RCP< T > rcpFromPtr(const Ptr< T > &ptr)
Create an RCP<T> from a Ptr<T> object.
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.
ValueType & any_cast(any &operand)
Used to extract the templated value held in Teuchos::any to a given value type.
#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.
TypeTo asSafe(const TypeFrom &t)
Convert from one value type to another, with validity checks if appropriate.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...