48 #include "Teuchos_VerboseObject.hpp" 50 #include "Teuchos_Assert.hpp" 57 inline int my_max(
int a,
int b ) {
return a > b ? a : b; }
60 std::string remove_quotes(
const std::string& str )
64 return str.substr(1,str.size()-2);
68 std::string add_quotes(
const std::string& str )
72 return "\"" + str +
"\"";
82 const bool CommandLineProcessor::output_all_front_matter_default_(
false);
83 const bool CommandLineProcessor::output_show_line_prefix_default_(
false);
84 const bool CommandLineProcessor::output_show_tab_count_default_(
false);
85 const bool CommandLineProcessor::output_show_proc_rank_default_(
false);
86 const int CommandLineProcessor::output_to_root_rank_only_default_(0);
87 const bool CommandLineProcessor::print_rcpnode_statistics_on_exit_default_(
false);
88 const bool CommandLineProcessor::show_timer_summary_on_exit_default_(
false);
92 bool throwExceptions_in
93 ,
bool recogniseAllOptions_in
94 ,
bool addOutputSetupOptions_in
96 :throwExceptions_(throwExceptions_in)
97 ,recogniseAllOptions_(recogniseAllOptions_in)
98 ,addOutputSetupOptions_(addOutputSetupOptions_in)
99 ,output_all_front_matter_(output_all_front_matter_default_)
100 ,output_show_line_prefix_(output_show_line_prefix_default_)
101 ,output_show_tab_count_(output_show_tab_count_default_)
102 ,output_show_proc_rank_(output_show_proc_rank_default_)
103 ,output_to_root_rank_only_(output_to_root_rank_only_default_)
104 ,print_rcpnode_statistics_on_exit_(print_rcpnode_statistics_on_exit_default_)
105 ,show_timer_summary_on_exit_(show_timer_summary_on_exit_default_)
106 ,printed_timer_summary_(false)
107 ,added_extra_output_setup_options_(false)
108 ,in_add_extra_output_setup_options_(false)
123 doc_string_ = doc_string;
128 const char option_true[]
129 ,
const char option_false[]
131 ,
const char documentation[]
134 add_extra_output_setup_options();
136 options_list_[std::string(option_true)]
137 = opt_val_val_t(OPT_BOOL_TRUE,
any(option_val),
false);
138 options_list_[std::string(option_false)]
139 = opt_val_val_t(OPT_BOOL_FALSE,
any(option_val),
false);
140 options_documentation_list_.push_back(
141 opt_doc_t(OPT_BOOL_TRUE, option_true, option_false,
142 std::string(documentation?documentation:
""),
any(option_val))
148 const char option_name[]
150 ,
const char documentation[]
154 add_extra_output_setup_options();
156 options_list_[std::string(option_name)]
157 = opt_val_val_t(OPT_INT,
any(option_val),required);
158 options_documentation_list_.push_back(
159 opt_doc_t(OPT_INT, option_name,
"", std::string(documentation?documentation:
""),
166 const char option_name[]
167 ,
long int *option_val
168 ,
const char documentation[]
172 add_extra_output_setup_options();
174 options_list_[std::string(option_name)]
175 = opt_val_val_t(OPT_LONG_INT,
any(option_val),required);
176 options_documentation_list_.push_back(
177 opt_doc_t(OPT_LONG_INT, option_name,
"", std::string(documentation?documentation:
""),
184 const char option_name[]
186 ,
const char documentation[]
190 add_extra_output_setup_options();
192 options_list_[std::string(option_name)]
193 = opt_val_val_t(OPT_SIZE_T,
any(option_val),required);
194 options_documentation_list_.push_back(
195 opt_doc_t(OPT_SIZE_T, option_name,
"", std::string(documentation?documentation:
""),
200 #ifdef HAVE_TEUCHOS_LONG_LONG_INT 202 const char option_name[]
203 ,
long long int *option_val
204 ,
const char documentation[]
208 add_extra_output_setup_options();
210 options_list_[std::string(option_name)]
211 = opt_val_val_t(OPT_LONG_LONG_INT,
any(option_val),required);
212 options_documentation_list_.push_back(
213 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:
""),
239 const char option_name[]
240 ,std::string *option_val
241 ,
const char documentation[]
245 add_extra_output_setup_options();
247 options_list_[std::string(option_name)]
248 = opt_val_val_t(OPT_STRING,
any(option_val),required);
249 options_documentation_list_.push_back(
250 opt_doc_t(OPT_STRING, option_name,
"", std::string(documentation?documentation:
""),
263 ,std::ostream *errout
266 add_extra_output_setup_options();
267 std::string opt_name;
268 std::string opt_val_str;
269 const std::string echo_cl_opt =
"echo-command-line";
270 const std::string help_opt =
"help";
271 const std::string pause_opt =
"pause-for-debugging";
273 for(
int i = 1; i < argc; ++i ) {
274 bool gov_return = get_opt_val( argv[i], &opt_name, &opt_val_str );
277 print_bad_opt(i,argv,errout);
284 if( opt_name == echo_cl_opt ) {
285 if(errout && procRank == 0) {
286 *errout <<
"\nEchoing the command-line:\n\n";
287 for(
int j = 0; j < argc; ++j )
288 *errout << argv[j] <<
" ";
293 if( opt_name == help_opt ) {
297 if( opt_name == pause_opt ) {
299 std::cerr <<
"\nType 0 and press enter to continue : ";
301 std::cin >> dummy_int;
307 options_list_t::iterator itr = options_list_.find(opt_name);
308 if( itr == options_list_.end() ) {
310 print_bad_opt(i,argv,errout);
318 opt_val_val_t &opt_val_val = (*itr).second;
319 opt_val_val.was_read =
true;
320 switch( opt_val_val.opt_type ) {
322 *(any_cast<
bool*>(opt_val_val.opt_val)) =
true;
325 *(any_cast<
bool*>(opt_val_val.opt_val)) =
false;
328 *(any_cast<
int*>(opt_val_val.opt_val)) = asSafe<int> (opt_val_str);
331 *(any_cast<
long int*>(opt_val_val.opt_val)) = asSafe<long int> (opt_val_str);
334 *(any_cast<
size_t *>(opt_val_val.opt_val)) = asSafe<size_t> (opt_val_str);
336 #ifdef HAVE_TEUCHOS_LONG_LONG_INT 337 case OPT_LONG_LONG_INT:
338 *(any_cast<
long long int*>(opt_val_val.opt_val)) = asSafe<long long int> (opt_val_str);
342 *(any_cast<
double*>(opt_val_val.opt_val)) = asSafe<double> (opt_val_str);
345 *(any_cast<std::string*>(opt_val_val.opt_val)) = remove_quotes(opt_val_str);
348 if( !set_enum_value( i, argv, opt_name, any_cast<int>(opt_val_val.opt_val),
349 remove_quotes(opt_val_str), errout ) )
360 options_list_t::const_iterator itr = options_list_.begin();
361 itr != options_list_.end();
365 const opt_val_val_t &opt_val_val = (*itr).second;
366 if( opt_val_val.required && !opt_val_val.was_read ) {
367 const std::string &opt_val_name = (*itr).first;
368 #define CLP_ERR_MSG \ 369 "Error, the option --"<<opt_val_name<<" was required but was not set!" 371 *errout << std::endl << argv[0] <<
" : " << CLP_ERR_MSG << std::endl;
382 if (defaultOut.
get() && addOutputSetupOptions_) {
383 if (output_all_front_matter_ != output_all_front_matter_default_)
384 defaultOut->setShowAllFrontMatter(output_all_front_matter_);
385 if (output_show_line_prefix_ != output_show_line_prefix_default_)
386 defaultOut->setShowLinePrefix(output_show_line_prefix_);
387 if (output_show_tab_count_ != output_show_tab_count_default_)
388 defaultOut->setShowTabCount(output_show_tab_count_);
389 if (output_show_proc_rank_ != output_show_proc_rank_default_)
390 defaultOut->setShowProcRank(output_show_proc_rank_);
391 if (output_to_root_rank_only_ != output_to_root_rank_only_default_)
392 defaultOut->setOutputToRootOnly(output_to_root_rank_only_);
400 std::ostream &out )
const 402 add_extra_output_setup_options();
408 const int opt_type_w = 14;
409 const char spc_chars[] =
" ";
413 options_documentation_list_t::const_iterator itr;
415 itr = options_documentation_list_.begin();
416 itr != options_documentation_list_.end();
420 opt_name_w = my_max(opt_name_w,itr->opt_name.length());
422 opt_name_w = my_max(opt_name_w,itr->opt_name_false.length());
428 <<
"Usage: " << program_name <<
" [options]\n" 429 << spc_chars <<
"options:\n" 432 #ifdef HAVE_STD_IOS_BASE_FMTFLAGS 433 << std::left << setw(opt_name_w) <<
"help" 434 << std::left << setw(opt_type_w) <<
" " 436 << std::setiosflags(std::ios::left) << setw(opt_name_w) <<
"help" 437 << std::setiosflags(std::ios::left) << setw(opt_type_w) <<
" " 439 <<
"Prints this help message" 443 #ifdef HAVE_STD_IOS_BASE_FMTFLAGS 444 << std::left << setw(opt_name_w) <<
"pause-for-debugging" 445 << std::left << setw(opt_type_w) <<
" " 447 << std::setiosflags(std::ios::left) << setw(opt_name_w) <<
"pause-for-debugging" 448 << std::setiosflags(std::ios::left) << setw(opt_type_w) <<
" " 450 <<
"Pauses for user input to allow attaching a debugger" 454 #ifdef HAVE_STD_IOS_BASE_FMTFLAGS 455 << std::left << setw(opt_name_w) <<
"echo-command-line" 456 << std::left << setw(opt_type_w) <<
" " 458 << std::setiosflags(std::ios::left) << setw(opt_name_w) <<
"echo-command-line" 459 << std::setiosflags(std::ios::left) << setw(opt_type_w) <<
" " 461 <<
"Echo the command-line but continue as normal" 464 itr = options_documentation_list_.begin();
465 itr != options_documentation_list_.end();
472 #ifdef HAVE_STD_IOS_BASE_FMTFLAGS 473 << std::left << setw(opt_name_w) << itr->opt_name
474 << std::left << setw(opt_type_w) << opt_type_str(itr->opt_type)
476 << std::setiosflags(std::ios::left) << setw(opt_name_w) << itr->opt_name
477 << std::setiosflags(std::ios::left) << setw(opt_type_w) << opt_type_str(itr->opt_type)
479 << ( itr->documentation.length() ? itr->documentation.c_str() :
"No documentation" )
482 if( itr->opt_type == OPT_ENUM_INT ) {
486 << setw(opt_name_w) <<
"" 487 << setw(opt_type_w) <<
"";
488 print_enum_opt_names( any_cast<int>(itr->default_val), out );
493 if( itr->opt_type == OPT_BOOL_TRUE ) {
497 << setw(opt_name_w) << itr->opt_name_false;
503 << setw(opt_name_w) <<
" ";
506 << setw(opt_type_w) <<
" " 508 switch( itr->opt_type ) {
510 out <<
"--" << ( (*(any_cast<
bool*>(itr->default_val))) ?
511 itr->opt_name : itr->opt_name_false );
516 #ifdef HAVE_TEUCHOS_LONG_LONG_INT 517 case OPT_LONG_LONG_INT:
522 out <<
"--" << itr->opt_name;
527 switch( itr->opt_type ) {
531 out <<
"=" << (*(any_cast<
int*>(itr->default_val)));
534 out <<
"=" << (*(any_cast<
long int*>(itr->default_val)));
537 out <<
"=" << (*(any_cast<
size_t*>(itr->default_val)));
539 #ifdef HAVE_TEUCHOS_LONG_LONG_INT 540 case OPT_LONG_LONG_INT:
541 out <<
"=" << (*(any_cast<
long long int*>(itr->default_val)));
545 out <<
"=" << (*(any_cast<
double*>(itr->default_val)));
548 out <<
"=" << add_quotes(*(any_cast<std::string*>(itr->default_val)));
551 out <<
"=" << add_quotes(
552 enum_opt_default_val_name(itr->opt_name,any_cast<int>(itr->default_val),&out));
559 if(doc_string_.length()) {
560 out <<
"\nDETAILED DOCUMENTATION:\n\n" << doc_string_ << std::endl << std::endl;
572 if (!printed_timer_summary_ && show_timer_summary_on_exit_) {
575 out = rcpFromPtr(out_inout);
580 getTimeMonitorSurrogate()->summarize(*out <<
"\n");
581 printed_timer_summary_ =
true;
589 void CommandLineProcessor::add_extra_output_setup_options()
const 593 in_add_extra_output_setup_options_
596 added_extra_output_setup_options_
599 !addOutputSetupOptions_
607 clp->in_add_extra_output_setup_options_ =
true;
609 "output-all-front-matter",
"output-no-front-matter",&clp->output_all_front_matter_
610 ,
"Set if all front matter is printed to the default FancyOStream or not" 613 "output-show-line-prefix",
"output-no-show-line-prefix",&clp->output_show_line_prefix_
614 ,
"Set if the line prefix matter is printed to the default FancyOStream or not" 617 "output-show-tab-count",
"output-no-show-tab-count",&clp->output_show_tab_count_
618 ,
"Set if the tab count is printed to the default FancyOStream or not" 621 "output-show-proc-rank",
"output-no-show-proc-rank",&clp->output_show_proc_rank_
622 ,
"Set if the processor rank is printed to the default FancyOStream or not" 625 "output-to-root-rank-only",&clp->output_to_root_rank_only_
626 ,
"Set which processor (the root) gets the output. If < 0, then all processors get output." 629 "print-rcpnode-statistics-on-exit",
"no-print-rcpnode-statistics-on-exit",
630 &clp->print_rcpnode_statistics_on_exit_,
631 "Set if the RCPNode usage statistics will be printed on exit or not. Warning," 632 " this prints to std::cerr or every process so do not turn this on for very large" 635 if (
nonnull(getTimeMonitorSurrogate())) {
637 "show-timer-summary",
"no-show-timer-sumary", &clp->show_timer_summary_on_exit_,
638 "If true, then Teuchos::TimeMonitor::summarize() is called in" 639 " CommandLineProcessor's destructor (usually at the end of main)." 643 clp->added_extra_output_setup_options_ =
true;
644 clp->in_add_extra_output_setup_options_ =
false;
648 void CommandLineProcessor::setEnumOption(
649 const char enum_option_name[]
650 ,
int *enum_option_val
651 ,
const int num_enum_opt_values
652 ,
const int enum_opt_values[]
653 ,
const char* enum_opt_names[]
654 ,
const char documentation[]
658 add_extra_output_setup_options();
665 enum_opt_data_list_.push_back(
666 enum_opt_data_t(enum_option_val,num_enum_opt_values,enum_opt_values,enum_opt_names)
668 const int opt_id = enum_opt_data_list_.size()-1;
669 options_list_[std::string(enum_option_name)]
670 = opt_val_val_t(OPT_ENUM_INT,any(opt_id),required);
671 options_documentation_list_.push_back(
672 opt_doc_t(OPT_ENUM_INT,enum_option_name,
"",
673 std::string(documentation?documentation:
""), any(opt_id))
678 bool CommandLineProcessor::set_enum_value(
681 ,
const std::string &enum_opt_name
683 ,
const std::string &enum_str_val
684 ,std::ostream *errout
687 const enum_opt_data_t
688 &enum_opt_data = enum_opt_data_list_.at(enum_id);
689 std::vector<std::string>::const_iterator
690 itr_begin = enum_opt_data.enum_opt_names.begin(),
691 itr_end = enum_opt_data.enum_opt_names.end(),
692 itr = std::find( itr_begin, itr_end, enum_str_val );
693 if( itr == itr_end ) {
694 const int j = argv_i;
695 #define CLP_ERR_MSG \ 696 "Error, the value \"" << enum_str_val << "\" for the " \ 697 << j<<(j==1?"st":(j==2?"nd":(j==3?"rd":"th"))) << " option --" \ 698 << enum_opt_name << " was not recognized (use --help)!" 700 *errout << std::endl << argv[0] <<
" : " << CLP_ERR_MSG << std::endl;
709 const int enum_opt_val_index = itr - itr_begin;
710 *enum_opt_data.enum_option_val = enum_opt_data.enum_opt_values.at(enum_opt_val_index);
715 void CommandLineProcessor::print_enum_opt_names(
720 const enum_opt_data_t
721 &enum_opt_data = enum_opt_data_list_.at(enum_id);
722 typedef std::vector<std::string>::const_iterator itr_t;
723 out <<
"Valid options:";
725 itr_t itr = enum_opt_data.enum_opt_names.begin();
726 itr != enum_opt_data.enum_opt_names.end();
730 if( itr != enum_opt_data.enum_opt_names.begin() ) out <<
",";
731 out <<
" " << add_quotes(*itr);
737 CommandLineProcessor::enum_opt_default_val_name(
738 const std::string &enum_name
740 ,std::ostream *errout
743 const enum_opt_data_t
744 &enum_opt_data = enum_opt_data_list_.at(enum_id);
745 return enum_opt_data.enum_opt_names.at(
747 enum_name,*enum_opt_data.enum_option_val,enum_opt_data,errout
753 int CommandLineProcessor::find_enum_opt_index(
754 const std::string &enum_opt_name
756 ,
const enum_opt_data_t &enum_data
757 ,std::ostream *errout
760 std::vector<int>::const_iterator
761 itr_begin = enum_data.enum_opt_values.begin(),
762 itr_end = enum_data.enum_opt_values.end(),
763 itr = std::find( itr_begin, itr_end, opt_value );
764 if( itr == itr_end ) {
765 #define CLP_ERR_MSG \ 766 ( recogniseAllOptions() ? "Error" : "Warning" ) \ 767 << ", option --" << enum_opt_name << " was given an invalid " \ 768 "initial option value of " << opt_value << "!" 770 *errout << CLP_ERR_MSG << std::endl;
775 return itr - itr_begin;
779 bool CommandLineProcessor::get_opt_val(
781 ,std::string *opt_name
782 ,std::string *opt_val_str
785 const int len = std::strlen(str);
788 if( str[0] !=
'-' || str[1] !=
'-' )
792 for( equ_i = 2; equ_i < len && str[equ_i] !=
'='; ++equ_i );
794 opt_name->assign( str + 2, equ_i-2 );
800 opt_val_str->assign( str + equ_i + 1, len - equ_i - 1 );
805 void CommandLineProcessor::print_bad_opt(
808 ,std::ostream *errout
811 const int j = argv_i;
812 #define CLP_ERR_MSG \ 813 ( recogniseAllOptions() ? "Error" : "Warning" ) \ 814 << ", the " << j<<(j==1?"st":(j==2?"nd":(j==3?"rd":"th"))) \ 815 << " option \'" << argv[argv_i] << "\' was not recognized (use --help)!" 817 *errout << std::endl << argv[0] <<
" : " << CLP_ERR_MSG << std::endl;
827 void CommandLineProcessor::setTimeMonitorSurrogate(
828 const RCP<CommandLineProcessor::TimeMonitorSurrogate> &timeMonitorSurrogate)
830 getRawTimeMonitorSurrogate() = timeMonitorSurrogate;
834 RCP<CommandLineProcessor::TimeMonitorSurrogate>
835 CommandLineProcessor::getTimeMonitorSurrogate()
837 return getRawTimeMonitorSurrogate();
841 RCP<CommandLineProcessor::TimeMonitorSurrogate>&
842 CommandLineProcessor::getRawTimeMonitorSurrogate()
844 static RCP<TimeMonitorSurrogate> timeMonitorSurrogate;
845 return timeMonitorSurrogate;
static int getRank()
The rank of the calling process in MPI_COMM_WORLD.
Thrown if –help was specified and throwExceptions==true.
bool nonnull(const std::shared_ptr< T > &p)
Returns true if p.get()!=NULL.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
bool recogniseAllOptions() const
Returns true if all options must be recognized by the parser.
static void setPrintRCPNodeStatisticsOnExit(bool printRCPNodeStatisticsOnExit)
Set if RCPNode usage statistics will be printed when the program ends or not.
CommandLineProcessor(bool throwExceptions=true, bool recogniseAllOptions=true, bool addOutputSetupOptions=false)
Default Constructor.
Modified boost::any class, which is a container for a templated value.
T * get() const
Get the raw C++ pointer to the underlying object.
~CommandLineProcessor()
Destructor.
void setOption(const char option_true[], const char option_false[], bool *option_val, const char documentation[]=NULL)
Set a boolean option.
EParseCommandLineReturn parse(int argc, char *argv[], std::ostream *errout=&std::cerr) const
Parse a command line.
static RCP< FancyOStream > getDefaultOStream()
Get the default output stream object.
EParseCommandLineReturn
Return value for CommandLineProcessor::parse(). Note: These enums are all given non-negative values s...
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos, as well as a number of utility routines.
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.
bool throwExceptions() const
Returns true if an std::exception is thrown, there is a parse error, or help is printed.
void printHelpMessage(const char program_name[], std::ostream &out) const
Print the help message.
static void barrier()
Call MPI_Barrier() on MPI_COMM_WORLD.
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...