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 )
65 if (str[0] ==
'\"' && str[str.size()-1] ==
'\"')
66 return str.substr(1,str.size()-2);
67 else if (str[0] ==
'\"')
68 return str.substr(1,str.size()-1);
69 else if (str[str.size()-1] ==
'\"')
70 return str.substr(0,str.size()-1);
75 std::string add_quotes(
const std::string& str )
79 return "\"" + str +
"\"";
89 const bool CommandLineProcessor::output_all_front_matter_default_(
false);
90 const bool CommandLineProcessor::output_show_line_prefix_default_(
false);
91 const bool CommandLineProcessor::output_show_tab_count_default_(
false);
92 const bool CommandLineProcessor::output_show_proc_rank_default_(
false);
93 const int CommandLineProcessor::output_to_root_rank_only_default_(0);
94 const bool CommandLineProcessor::print_rcpnode_statistics_on_exit_default_(
false);
95 const bool CommandLineProcessor::show_timer_summary_on_exit_default_(
false);
99 bool throwExceptions_in
100 ,
bool recogniseAllOptions_in
101 ,
bool addOutputSetupOptions_in
103 :throwExceptions_(throwExceptions_in)
104 ,recogniseAllOptions_(recogniseAllOptions_in)
105 ,addOutputSetupOptions_(addOutputSetupOptions_in)
106 ,output_all_front_matter_(output_all_front_matter_default_)
107 ,output_show_line_prefix_(output_show_line_prefix_default_)
108 ,output_show_tab_count_(output_show_tab_count_default_)
109 ,output_show_proc_rank_(output_show_proc_rank_default_)
110 ,output_to_root_rank_only_(output_to_root_rank_only_default_)
111 ,print_rcpnode_statistics_on_exit_(print_rcpnode_statistics_on_exit_default_)
112 ,show_timer_summary_on_exit_(show_timer_summary_on_exit_default_)
113 ,printed_timer_summary_(false)
114 ,added_extra_output_setup_options_(false)
115 ,in_add_extra_output_setup_options_(false)
130 doc_string_ = doc_string;
135 const char option_true[]
136 ,
const char option_false[]
138 ,
const char documentation[]
141 add_extra_output_setup_options();
143 options_list_[std::string(option_true)]
144 = opt_val_val_t(OPT_BOOL_TRUE,
any(option_val),
false);
145 options_list_[std::string(option_false)]
146 = opt_val_val_t(OPT_BOOL_FALSE,
any(option_val),
false);
147 options_documentation_list_.push_back(
148 opt_doc_t(OPT_BOOL_TRUE, option_true, option_false,
149 std::string(documentation?documentation:
""),
any(option_val))
155 const char option_name[]
157 ,
const char documentation[]
161 add_extra_output_setup_options();
163 options_list_[std::string(option_name)]
164 = opt_val_val_t(OPT_INT,
any(option_val),required);
165 options_documentation_list_.push_back(
166 opt_doc_t(OPT_INT, option_name,
"", std::string(documentation?documentation:
""),
173 const char option_name[]
174 ,
long int *option_val
175 ,
const char documentation[]
179 add_extra_output_setup_options();
181 options_list_[std::string(option_name)]
182 = opt_val_val_t(OPT_LONG_INT,
any(option_val),required);
183 options_documentation_list_.push_back(
184 opt_doc_t(OPT_LONG_INT, option_name,
"", std::string(documentation?documentation:
""),
191 const char option_name[]
193 ,
const char documentation[]
197 add_extra_output_setup_options();
199 options_list_[std::string(option_name)]
200 = opt_val_val_t(OPT_SIZE_T,
any(option_val),required);
201 options_documentation_list_.push_back(
202 opt_doc_t(OPT_SIZE_T, option_name,
"", std::string(documentation?documentation:
""),
208 const char option_name[]
209 ,
long long int *option_val
210 ,
const char documentation[]
214 add_extra_output_setup_options();
216 options_list_[std::string(option_name)]
217 = opt_val_val_t(OPT_LONG_LONG_INT,
any(option_val),required);
218 options_documentation_list_.push_back(
219 opt_doc_t(OPT_LONG_LONG_INT, option_name,
"", std::string(documentation?documentation:
""),
225 const char option_name[]
227 ,
const char documentation[]
231 add_extra_output_setup_options();
233 options_list_[std::string(option_name)]
234 = opt_val_val_t(OPT_DOUBLE,
any(option_val),required);
235 options_documentation_list_.push_back(
236 opt_doc_t(OPT_DOUBLE, option_name,
"", std::string(documentation?documentation:
""),
242 const char option_name[]
244 ,
const char documentation[]
248 add_extra_output_setup_options();
250 options_list_[std::string(option_name)]
251 = opt_val_val_t(OPT_FLOAT,
any(option_val),required);
252 options_documentation_list_.push_back(
253 opt_doc_t(OPT_FLOAT, option_name,
"", std::string(documentation?documentation:
""),
259 const char option_name[]
260 ,std::string *option_val
261 ,
const char documentation[]
265 add_extra_output_setup_options();
267 options_list_[std::string(option_name)]
268 = opt_val_val_t(OPT_STRING,
any(option_val),required);
269 options_documentation_list_.push_back(
270 opt_doc_t(OPT_STRING, option_name,
"", std::string(documentation?documentation:
""),
283 ,std::ostream *errout
286 add_extra_output_setup_options();
287 std::string opt_name;
288 std::string opt_val_str;
289 const std::string echo_cl_opt =
"echo-command-line";
290 const std::string help_opt =
"help";
291 const std::string pause_opt =
"pause-for-debugging";
296 for(
int i = 1; i < argc; ++i ) {
297 bool gov_return = get_opt_val( argv[i], &opt_name, &opt_val_str );
298 if( gov_return && opt_name == help_opt ) {
304 for(
int i = 1; i < argc; ++i ) {
305 bool gov_return = get_opt_val( argv[i], &opt_name, &opt_val_str );
308 print_bad_opt(i,argv,errout);
315 if( opt_name == echo_cl_opt ) {
316 if(errout && procRank == 0) {
317 *errout <<
"\nEchoing the command-line:\n\n";
318 for(
int j = 0; j < argc; ++j )
319 *errout << argv[j] <<
" ";
324 if( opt_name == pause_opt ) {
328 int rank_pid = getpid();
332 std::cerr <<
"Rank " << k <<
" has PID " << pids[k] << std::endl;
335 std::cerr <<
"\nType 0 and press enter to continue : ";
337 std::cin >> dummy_int;
343 options_list_t::iterator itr = options_list_.find(opt_name);
344 if( itr == options_list_.end() ) {
346 print_bad_opt(i,argv,errout);
354 opt_val_val_t &opt_val_val = (*itr).second;
355 opt_val_val.was_read =
true;
356 switch( opt_val_val.opt_type ) {
358 *(any_cast<
bool*>(opt_val_val.opt_val)) =
true;
361 *(any_cast<
bool*>(opt_val_val.opt_val)) =
false;
364 *(any_cast<
int*>(opt_val_val.opt_val)) = asSafe<int> (opt_val_str);
367 *(any_cast<
long int*>(opt_val_val.opt_val)) = asSafe<long int> (opt_val_str);
370 *(any_cast<
size_t *>(opt_val_val.opt_val)) = asSafe<size_t> (opt_val_str);
372 case OPT_LONG_LONG_INT:
373 *(any_cast<
long long int*>(opt_val_val.opt_val)) = asSafe<long long int> (opt_val_str);
376 *(any_cast<
double*>(opt_val_val.opt_val)) = asSafe<double> (opt_val_str);
379 *(any_cast<
float*>(opt_val_val.opt_val)) = asSafe<float> (opt_val_str);
382 *(any_cast<std::string*>(opt_val_val.opt_val)) = remove_quotes(opt_val_str);
385 if( !set_enum_value( i, argv, opt_name, any_cast<int>(opt_val_val.opt_val),
386 remove_quotes(opt_val_str), errout ) )
397 options_list_t::const_iterator itr = options_list_.begin();
398 itr != options_list_.end();
402 const opt_val_val_t &opt_val_val = (*itr).second;
403 if( opt_val_val.required && !opt_val_val.was_read ) {
404 const std::string &opt_val_name = (*itr).first;
405 #define CLP_ERR_MSG \
406 "Error, the option --"<<opt_val_name<<" was required but was not set!"
408 *errout << std::endl << argv[0] <<
" : " << CLP_ERR_MSG << std::endl;
419 if (defaultOut.
get() && addOutputSetupOptions_) {
420 if (output_all_front_matter_ != output_all_front_matter_default_)
421 defaultOut->setShowAllFrontMatter(output_all_front_matter_);
422 if (output_show_line_prefix_ != output_show_line_prefix_default_)
423 defaultOut->setShowLinePrefix(output_show_line_prefix_);
424 if (output_show_tab_count_ != output_show_tab_count_default_)
425 defaultOut->setShowTabCount(output_show_tab_count_);
426 if (output_show_proc_rank_ != output_show_proc_rank_default_)
427 defaultOut->setShowProcRank(output_show_proc_rank_);
428 if (output_to_root_rank_only_ != output_to_root_rank_only_default_)
429 defaultOut->setOutputToRootOnly(output_to_root_rank_only_);
437 std::ostream &out )
const
439 add_extra_output_setup_options();
445 const int opt_type_w = 14;
446 const char spc_chars[] =
" ";
450 options_documentation_list_t::const_iterator itr;
452 itr = options_documentation_list_.begin();
453 itr != options_documentation_list_.end();
457 opt_name_w = my_max(opt_name_w,static_cast<int>(itr->opt_name.length()));
459 opt_name_w = my_max(opt_name_w,static_cast<int>(itr->opt_name_false.length()));
465 <<
"Usage: " << program_name <<
" [options]\n"
466 << spc_chars <<
"options:\n"
469 #ifdef HAVE_STD_IOS_BASE_FMTFLAGS
470 << std::left << setw(opt_name_w) <<
"help"
471 << std::left << setw(opt_type_w) <<
" "
473 << std::setiosflags(std::ios::left) << setw(opt_name_w) <<
"help"
474 << std::setiosflags(std::ios::left) << setw(opt_type_w) <<
" "
476 <<
"Prints this help message"
480 #ifdef HAVE_STD_IOS_BASE_FMTFLAGS
481 << std::left << setw(opt_name_w) <<
"pause-for-debugging"
482 << std::left << setw(opt_type_w) <<
" "
484 << std::setiosflags(std::ios::left) << setw(opt_name_w) <<
"pause-for-debugging"
485 << std::setiosflags(std::ios::left) << setw(opt_type_w) <<
" "
487 <<
"Pauses for user input to allow attaching a debugger"
491 #ifdef HAVE_STD_IOS_BASE_FMTFLAGS
492 << std::left << setw(opt_name_w) <<
"echo-command-line"
493 << std::left << setw(opt_type_w) <<
" "
495 << std::setiosflags(std::ios::left) << setw(opt_name_w) <<
"echo-command-line"
496 << std::setiosflags(std::ios::left) << setw(opt_type_w) <<
" "
498 <<
"Echo the command-line but continue as normal"
501 itr = options_documentation_list_.begin();
502 itr != options_documentation_list_.end();
509 #ifdef HAVE_STD_IOS_BASE_FMTFLAGS
510 << std::left << setw(opt_name_w) << itr->opt_name
511 << std::left << setw(opt_type_w) << opt_type_str(itr->opt_type)
513 << std::setiosflags(std::ios::left) << setw(opt_name_w) << itr->opt_name
514 << std::setiosflags(std::ios::left) << setw(opt_type_w) << opt_type_str(itr->opt_type)
516 << ( itr->documentation.length() ? itr->documentation.c_str() :
"No documentation" )
519 if( itr->opt_type == OPT_ENUM_INT ) {
523 << setw(opt_name_w) <<
""
524 << setw(opt_type_w) <<
"";
525 print_enum_opt_names( any_cast<int>(itr->default_val), out );
530 if( itr->opt_type == OPT_BOOL_TRUE ) {
534 << setw(opt_name_w) << itr->opt_name_false;
540 << setw(opt_name_w) <<
" ";
543 << setw(opt_type_w) <<
" "
545 switch( itr->opt_type ) {
547 out <<
"--" << ( (*(any_cast<
bool*>(itr->default_val))) ?
548 itr->opt_name : itr->opt_name_false );
553 case OPT_LONG_LONG_INT:
558 out <<
"--" << itr->opt_name;
563 switch( itr->opt_type ) {
567 out <<
"=" << (*(any_cast<
int*>(itr->default_val)));
570 out <<
"=" << (*(any_cast<
long int*>(itr->default_val)));
573 out <<
"=" << (*(any_cast<
size_t*>(itr->default_val)));
575 case OPT_LONG_LONG_INT:
576 out <<
"=" << (*(any_cast<
long long int*>(itr->default_val)));
579 out <<
"=" << (*(any_cast<
double*>(itr->default_val)));
582 out <<
"=" << (*(any_cast<
float*>(itr->default_val)));
585 out <<
"=" << add_quotes(*(any_cast<std::string*>(itr->default_val)));
588 out <<
"=" << add_quotes(
589 enum_opt_default_val_name(itr->opt_name,any_cast<int>(itr->default_val),&out));
596 if(doc_string_.length()) {
597 out <<
"\nDETAILED DOCUMENTATION:\n\n" << doc_string_ << std::endl << std::endl;
609 if (!printed_timer_summary_ && show_timer_summary_on_exit_) {
612 out = rcpFromPtr(out_inout);
617 getTimeMonitorSurrogate()->summarize(*out <<
"\n");
618 printed_timer_summary_ =
true;
626 void CommandLineProcessor::add_extra_output_setup_options()
const
630 in_add_extra_output_setup_options_
633 added_extra_output_setup_options_
636 !addOutputSetupOptions_
644 clp->in_add_extra_output_setup_options_ =
true;
646 "output-all-front-matter",
"output-no-front-matter",&clp->output_all_front_matter_
647 ,
"Set if all front matter is printed to the default FancyOStream or not"
650 "output-show-line-prefix",
"output-no-show-line-prefix",&clp->output_show_line_prefix_
651 ,
"Set if the line prefix matter is printed to the default FancyOStream or not"
654 "output-show-tab-count",
"output-no-show-tab-count",&clp->output_show_tab_count_
655 ,
"Set if the tab count is printed to the default FancyOStream or not"
658 "output-show-proc-rank",
"output-no-show-proc-rank",&clp->output_show_proc_rank_
659 ,
"Set if the processor rank is printed to the default FancyOStream or not"
662 "output-to-root-rank-only",&clp->output_to_root_rank_only_
663 ,
"Set which processor (the root) gets the output. If < 0, then all processors get output."
666 "print-rcpnode-statistics-on-exit",
"no-print-rcpnode-statistics-on-exit",
667 &clp->print_rcpnode_statistics_on_exit_,
668 "Set if the RCPNode usage statistics will be printed on exit or not. Warning,"
669 " this prints to std::cerr or every process so do not turn this on for very large"
672 if (
nonnull(getTimeMonitorSurrogate())) {
674 "show-timer-summary",
"no-show-timer-sumary", &clp->show_timer_summary_on_exit_,
675 "If true, then Teuchos::TimeMonitor::summarize() is called in"
676 " CommandLineProcessor's destructor (usually at the end of main)."
680 clp->added_extra_output_setup_options_ =
true;
681 clp->in_add_extra_output_setup_options_ =
false;
685 void CommandLineProcessor::setEnumOption(
686 const char enum_option_name[],
687 int* enum_option_val,
688 const int num_enum_opt_values,
689 const int enum_opt_values[],
690 const char *
const enum_opt_names[],
691 const char documentation[],
695 add_extra_output_setup_options();
702 enum_opt_data_list_.push_back(
703 enum_opt_data_t(enum_option_val,num_enum_opt_values,enum_opt_values,enum_opt_names)
705 const int opt_id =
static_cast<int>(enum_opt_data_list_.size())-1;
706 options_list_[std::string(enum_option_name)]
707 = opt_val_val_t(OPT_ENUM_INT,any(opt_id),required);
708 options_documentation_list_.push_back(
709 opt_doc_t(OPT_ENUM_INT,enum_option_name,
"",
710 std::string(documentation?documentation:
""), any(opt_id))
715 bool CommandLineProcessor::set_enum_value(
718 ,
const std::string &enum_opt_name
720 ,
const std::string &enum_str_val
721 ,std::ostream *errout
724 const enum_opt_data_t
725 &enum_opt_data = enum_opt_data_list_.at(enum_id);
726 std::vector<std::string>::const_iterator
727 itr_begin = enum_opt_data.enum_opt_names.begin(),
728 itr_end = enum_opt_data.enum_opt_names.end(),
729 itr = std::find( itr_begin, itr_end, enum_str_val );
730 if( itr == itr_end ) {
731 const int j = argv_i;
732 #define CLP_ERR_MSG \
733 "Error, the value \"" << enum_str_val << "\" for the " \
734 << j<<(j==1?"st":(j==2?"nd":(j==3?"rd":"th"))) << " option --" \
735 << enum_opt_name << " was not recognized (use --help)!"
737 *errout << std::endl << argv[0] <<
" : " << CLP_ERR_MSG << std::endl;
746 const int enum_opt_val_index =
static_cast<int>(itr - itr_begin);
747 *enum_opt_data.enum_option_val = enum_opt_data.enum_opt_values.at(enum_opt_val_index);
752 void CommandLineProcessor::print_enum_opt_names(
757 const enum_opt_data_t
758 &enum_opt_data = enum_opt_data_list_.at(enum_id);
759 typedef std::vector<std::string>::const_iterator itr_t;
760 out <<
"Valid options:";
762 itr_t itr = enum_opt_data.enum_opt_names.begin();
763 itr != enum_opt_data.enum_opt_names.end();
767 if( itr != enum_opt_data.enum_opt_names.begin() ) out <<
",";
768 out <<
" " << add_quotes(*itr);
774 CommandLineProcessor::enum_opt_default_val_name(
775 const std::string &enum_name
777 ,std::ostream *errout
780 const enum_opt_data_t
781 &enum_opt_data = enum_opt_data_list_.at(enum_id);
782 return enum_opt_data.enum_opt_names.at(
784 enum_name,*enum_opt_data.enum_option_val,enum_opt_data,errout
790 int CommandLineProcessor::find_enum_opt_index(
791 const std::string &enum_opt_name
793 ,
const enum_opt_data_t &enum_data
794 ,std::ostream *errout
797 std::vector<int>::const_iterator
798 itr_begin = enum_data.enum_opt_values.begin(),
799 itr_end = enum_data.enum_opt_values.end(),
800 itr = std::find( itr_begin, itr_end, opt_value );
801 if( itr == itr_end ) {
802 #define CLP_ERR_MSG \
803 ( recogniseAllOptions() ? "Error" : "Warning" ) \
804 << ", option --" << enum_opt_name << " was given an invalid " \
805 "initial option value of " << opt_value << "!"
807 *errout << CLP_ERR_MSG << std::endl;
812 return static_cast<int>(itr - itr_begin);
816 bool CommandLineProcessor::get_opt_val(
818 ,std::string *opt_name
819 ,std::string *opt_val_str
822 const int len =
static_cast<int>(std::strlen(str));
825 if( str[0] !=
'-' || str[1] !=
'-' )
829 for( equ_i = 2; equ_i < len && str[equ_i] !=
'='; ++equ_i );
831 opt_name->assign( str + 2, equ_i-2 );
837 opt_val_str->assign( str + equ_i + 1, len - equ_i - 1 );
842 void CommandLineProcessor::print_bad_opt(
845 ,std::ostream *errout
848 const int j = argv_i;
849 #define CLP_ERR_MSG \
850 ( recogniseAllOptions() ? "Error" : "Warning" ) \
851 << ", the " << j<<(j==1?"st":(j==2?"nd":(j==3?"rd":"th"))) \
852 << " option \'" << argv[argv_i] << "\' was not recognized (use --help)!"
854 *errout << std::endl << argv[0] <<
" : " << CLP_ERR_MSG << std::endl;
864 void CommandLineProcessor::setTimeMonitorSurrogate(
865 const RCP<CommandLineProcessor::TimeMonitorSurrogate> &timeMonitorSurrogate)
867 getRawTimeMonitorSurrogate() = timeMonitorSurrogate;
871 RCP<CommandLineProcessor::TimeMonitorSurrogate>
872 CommandLineProcessor::getTimeMonitorSurrogate()
874 return getRawTimeMonitorSurrogate();
878 RCP<CommandLineProcessor::TimeMonitorSurrogate>&
879 CommandLineProcessor::getRawTimeMonitorSurrogate()
881 static RCP<TimeMonitorSurrogate> timeMonitorSurrogate;
882 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...