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";
276 for(
int i = 1; i < argc; ++i ) {
277 bool gov_return = get_opt_val( argv[i], &opt_name, &opt_val_str );
278 if( gov_return && opt_name == help_opt ) {
284 for(
int i = 1; i < argc; ++i ) {
285 bool gov_return = get_opt_val( argv[i], &opt_name, &opt_val_str );
288 print_bad_opt(i,argv,errout);
295 if( opt_name == echo_cl_opt ) {
296 if(errout && procRank == 0) {
297 *errout <<
"\nEchoing the command-line:\n\n";
298 for(
int j = 0; j < argc; ++j )
299 *errout << argv[j] <<
" ";
304 if( opt_name == pause_opt ) {
306 std::cerr <<
"\nType 0 and press enter to continue : ";
308 std::cin >> dummy_int;
314 options_list_t::iterator itr = options_list_.find(opt_name);
315 if( itr == options_list_.end() ) {
317 print_bad_opt(i,argv,errout);
325 opt_val_val_t &opt_val_val = (*itr).second;
326 opt_val_val.was_read =
true;
327 switch( opt_val_val.opt_type ) {
329 *(any_cast<
bool*>(opt_val_val.opt_val)) =
true;
332 *(any_cast<
bool*>(opt_val_val.opt_val)) =
false;
335 *(any_cast<
int*>(opt_val_val.opt_val)) = asSafe<int> (opt_val_str);
338 *(any_cast<
long int*>(opt_val_val.opt_val)) = asSafe<long int> (opt_val_str);
341 *(any_cast<
size_t *>(opt_val_val.opt_val)) = asSafe<size_t> (opt_val_str);
343 #ifdef HAVE_TEUCHOS_LONG_LONG_INT 344 case OPT_LONG_LONG_INT:
345 *(any_cast<
long long int*>(opt_val_val.opt_val)) = asSafe<long long int> (opt_val_str);
349 *(any_cast<
double*>(opt_val_val.opt_val)) = asSafe<double> (opt_val_str);
352 *(any_cast<std::string*>(opt_val_val.opt_val)) = remove_quotes(opt_val_str);
355 if( !set_enum_value( i, argv, opt_name, any_cast<int>(opt_val_val.opt_val),
356 remove_quotes(opt_val_str), errout ) )
367 options_list_t::const_iterator itr = options_list_.begin();
368 itr != options_list_.end();
372 const opt_val_val_t &opt_val_val = (*itr).second;
373 if( opt_val_val.required && !opt_val_val.was_read ) {
374 const std::string &opt_val_name = (*itr).first;
375 #define CLP_ERR_MSG \ 376 "Error, the option --"<<opt_val_name<<" was required but was not set!" 378 *errout << std::endl << argv[0] <<
" : " << CLP_ERR_MSG << std::endl;
389 if (defaultOut.
get() && addOutputSetupOptions_) {
390 if (output_all_front_matter_ != output_all_front_matter_default_)
391 defaultOut->setShowAllFrontMatter(output_all_front_matter_);
392 if (output_show_line_prefix_ != output_show_line_prefix_default_)
393 defaultOut->setShowLinePrefix(output_show_line_prefix_);
394 if (output_show_tab_count_ != output_show_tab_count_default_)
395 defaultOut->setShowTabCount(output_show_tab_count_);
396 if (output_show_proc_rank_ != output_show_proc_rank_default_)
397 defaultOut->setShowProcRank(output_show_proc_rank_);
398 if (output_to_root_rank_only_ != output_to_root_rank_only_default_)
399 defaultOut->setOutputToRootOnly(output_to_root_rank_only_);
407 std::ostream &out )
const 409 add_extra_output_setup_options();
415 const int opt_type_w = 14;
416 const char spc_chars[] =
" ";
420 options_documentation_list_t::const_iterator itr;
422 itr = options_documentation_list_.begin();
423 itr != options_documentation_list_.end();
427 opt_name_w = my_max(opt_name_w,itr->opt_name.length());
429 opt_name_w = my_max(opt_name_w,itr->opt_name_false.length());
435 <<
"Usage: " << program_name <<
" [options]\n" 436 << spc_chars <<
"options:\n" 439 #ifdef HAVE_STD_IOS_BASE_FMTFLAGS 440 << std::left << setw(opt_name_w) <<
"help" 441 << std::left << setw(opt_type_w) <<
" " 443 << std::setiosflags(std::ios::left) << setw(opt_name_w) <<
"help" 444 << std::setiosflags(std::ios::left) << setw(opt_type_w) <<
" " 446 <<
"Prints this help message" 450 #ifdef HAVE_STD_IOS_BASE_FMTFLAGS 451 << std::left << setw(opt_name_w) <<
"pause-for-debugging" 452 << std::left << setw(opt_type_w) <<
" " 454 << std::setiosflags(std::ios::left) << setw(opt_name_w) <<
"pause-for-debugging" 455 << std::setiosflags(std::ios::left) << setw(opt_type_w) <<
" " 457 <<
"Pauses for user input to allow attaching a debugger" 461 #ifdef HAVE_STD_IOS_BASE_FMTFLAGS 462 << std::left << setw(opt_name_w) <<
"echo-command-line" 463 << std::left << setw(opt_type_w) <<
" " 465 << std::setiosflags(std::ios::left) << setw(opt_name_w) <<
"echo-command-line" 466 << std::setiosflags(std::ios::left) << setw(opt_type_w) <<
" " 468 <<
"Echo the command-line but continue as normal" 471 itr = options_documentation_list_.begin();
472 itr != options_documentation_list_.end();
479 #ifdef HAVE_STD_IOS_BASE_FMTFLAGS 480 << std::left << setw(opt_name_w) << itr->opt_name
481 << std::left << setw(opt_type_w) << opt_type_str(itr->opt_type)
483 << std::setiosflags(std::ios::left) << setw(opt_name_w) << itr->opt_name
484 << std::setiosflags(std::ios::left) << setw(opt_type_w) << opt_type_str(itr->opt_type)
486 << ( itr->documentation.length() ? itr->documentation.c_str() :
"No documentation" )
489 if( itr->opt_type == OPT_ENUM_INT ) {
493 << setw(opt_name_w) <<
"" 494 << setw(opt_type_w) <<
"";
495 print_enum_opt_names( any_cast<int>(itr->default_val), out );
500 if( itr->opt_type == OPT_BOOL_TRUE ) {
504 << setw(opt_name_w) << itr->opt_name_false;
510 << setw(opt_name_w) <<
" ";
513 << setw(opt_type_w) <<
" " 515 switch( itr->opt_type ) {
517 out <<
"--" << ( (*(any_cast<
bool*>(itr->default_val))) ?
518 itr->opt_name : itr->opt_name_false );
523 #ifdef HAVE_TEUCHOS_LONG_LONG_INT 524 case OPT_LONG_LONG_INT:
529 out <<
"--" << itr->opt_name;
534 switch( itr->opt_type ) {
538 out <<
"=" << (*(any_cast<
int*>(itr->default_val)));
541 out <<
"=" << (*(any_cast<
long int*>(itr->default_val)));
544 out <<
"=" << (*(any_cast<
size_t*>(itr->default_val)));
546 #ifdef HAVE_TEUCHOS_LONG_LONG_INT 547 case OPT_LONG_LONG_INT:
548 out <<
"=" << (*(any_cast<
long long int*>(itr->default_val)));
552 out <<
"=" << (*(any_cast<
double*>(itr->default_val)));
555 out <<
"=" << add_quotes(*(any_cast<std::string*>(itr->default_val)));
558 out <<
"=" << add_quotes(
559 enum_opt_default_val_name(itr->opt_name,any_cast<int>(itr->default_val),&out));
566 if(doc_string_.length()) {
567 out <<
"\nDETAILED DOCUMENTATION:\n\n" << doc_string_ << std::endl << std::endl;
579 if (!printed_timer_summary_ && show_timer_summary_on_exit_) {
582 out = rcpFromPtr(out_inout);
587 getTimeMonitorSurrogate()->summarize(*out <<
"\n");
588 printed_timer_summary_ =
true;
596 void CommandLineProcessor::add_extra_output_setup_options()
const 600 in_add_extra_output_setup_options_
603 added_extra_output_setup_options_
606 !addOutputSetupOptions_
614 clp->in_add_extra_output_setup_options_ =
true;
616 "output-all-front-matter",
"output-no-front-matter",&clp->output_all_front_matter_
617 ,
"Set if all front matter is printed to the default FancyOStream or not" 620 "output-show-line-prefix",
"output-no-show-line-prefix",&clp->output_show_line_prefix_
621 ,
"Set if the line prefix matter is printed to the default FancyOStream or not" 624 "output-show-tab-count",
"output-no-show-tab-count",&clp->output_show_tab_count_
625 ,
"Set if the tab count is printed to the default FancyOStream or not" 628 "output-show-proc-rank",
"output-no-show-proc-rank",&clp->output_show_proc_rank_
629 ,
"Set if the processor rank is printed to the default FancyOStream or not" 632 "output-to-root-rank-only",&clp->output_to_root_rank_only_
633 ,
"Set which processor (the root) gets the output. If < 0, then all processors get output." 636 "print-rcpnode-statistics-on-exit",
"no-print-rcpnode-statistics-on-exit",
637 &clp->print_rcpnode_statistics_on_exit_,
638 "Set if the RCPNode usage statistics will be printed on exit or not. Warning," 639 " this prints to std::cerr or every process so do not turn this on for very large" 642 if (
nonnull(getTimeMonitorSurrogate())) {
644 "show-timer-summary",
"no-show-timer-sumary", &clp->show_timer_summary_on_exit_,
645 "If true, then Teuchos::TimeMonitor::summarize() is called in" 646 " CommandLineProcessor's destructor (usually at the end of main)." 650 clp->added_extra_output_setup_options_ =
true;
651 clp->in_add_extra_output_setup_options_ =
false;
655 void CommandLineProcessor::setEnumOption(
656 const char enum_option_name[]
657 ,
int *enum_option_val
658 ,
const int num_enum_opt_values
659 ,
const int enum_opt_values[]
660 ,
const char* enum_opt_names[]
661 ,
const char documentation[]
665 add_extra_output_setup_options();
672 enum_opt_data_list_.push_back(
673 enum_opt_data_t(enum_option_val,num_enum_opt_values,enum_opt_values,enum_opt_names)
675 const int opt_id = enum_opt_data_list_.size()-1;
676 options_list_[std::string(enum_option_name)]
677 = opt_val_val_t(OPT_ENUM_INT,
any(opt_id),required);
678 options_documentation_list_.push_back(
679 opt_doc_t(OPT_ENUM_INT,enum_option_name,
"",
680 std::string(documentation?documentation:
""),
any(opt_id))
685 bool CommandLineProcessor::set_enum_value(
688 ,
const std::string &enum_opt_name
690 ,
const std::string &enum_str_val
691 ,std::ostream *errout
694 const enum_opt_data_t
695 &enum_opt_data = enum_opt_data_list_.at(enum_id);
696 std::vector<std::string>::const_iterator
697 itr_begin = enum_opt_data.enum_opt_names.begin(),
698 itr_end = enum_opt_data.enum_opt_names.end(),
699 itr = std::find( itr_begin, itr_end, enum_str_val );
700 if( itr == itr_end ) {
701 const int j = argv_i;
702 #define CLP_ERR_MSG \ 703 "Error, the value \"" << enum_str_val << "\" for the " \ 704 << j<<(j==1?"st":(j==2?"nd":(j==3?"rd":"th"))) << " option --" \ 705 << enum_opt_name << " was not recognized (use --help)!" 707 *errout << std::endl << argv[0] <<
" : " << CLP_ERR_MSG << std::endl;
716 const int enum_opt_val_index = itr - itr_begin;
717 *enum_opt_data.enum_option_val = enum_opt_data.enum_opt_values.at(enum_opt_val_index);
722 void CommandLineProcessor::print_enum_opt_names(
727 const enum_opt_data_t
728 &enum_opt_data = enum_opt_data_list_.at(enum_id);
729 typedef std::vector<std::string>::const_iterator itr_t;
730 out <<
"Valid options:";
732 itr_t itr = enum_opt_data.enum_opt_names.begin();
733 itr != enum_opt_data.enum_opt_names.end();
737 if( itr != enum_opt_data.enum_opt_names.begin() ) out <<
",";
738 out <<
" " << add_quotes(*itr);
744 CommandLineProcessor::enum_opt_default_val_name(
745 const std::string &enum_name
747 ,std::ostream *errout
750 const enum_opt_data_t
751 &enum_opt_data = enum_opt_data_list_.at(enum_id);
752 return enum_opt_data.enum_opt_names.at(
754 enum_name,*enum_opt_data.enum_option_val,enum_opt_data,errout
760 int CommandLineProcessor::find_enum_opt_index(
761 const std::string &enum_opt_name
763 ,
const enum_opt_data_t &enum_data
764 ,std::ostream *errout
767 std::vector<int>::const_iterator
768 itr_begin = enum_data.enum_opt_values.begin(),
769 itr_end = enum_data.enum_opt_values.end(),
770 itr = std::find( itr_begin, itr_end, opt_value );
771 if( itr == itr_end ) {
772 #define CLP_ERR_MSG \ 773 ( recogniseAllOptions() ? "Error" : "Warning" ) \ 774 << ", option --" << enum_opt_name << " was given an invalid " \ 775 "initial option value of " << opt_value << "!" 777 *errout << CLP_ERR_MSG << std::endl;
782 return itr - itr_begin;
786 bool CommandLineProcessor::get_opt_val(
788 ,std::string *opt_name
789 ,std::string *opt_val_str
792 const int len = std::strlen(str);
795 if( str[0] !=
'-' || str[1] !=
'-' )
799 for( equ_i = 2; equ_i < len && str[equ_i] !=
'='; ++equ_i );
801 opt_name->assign( str + 2, equ_i-2 );
807 opt_val_str->assign( str + equ_i + 1, len - equ_i - 1 );
812 void CommandLineProcessor::print_bad_opt(
815 ,std::ostream *errout
818 const int j = argv_i;
819 #define CLP_ERR_MSG \ 820 ( recogniseAllOptions() ? "Error" : "Warning" ) \ 821 << ", the " << j<<(j==1?"st":(j==2?"nd":(j==3?"rd":"th"))) \ 822 << " option \'" << argv[argv_i] << "\' was not recognized (use --help)!" 824 *errout << std::endl << argv[0] <<
" : " << CLP_ERR_MSG << std::endl;
834 void CommandLineProcessor::setTimeMonitorSurrogate(
837 getRawTimeMonitorSurrogate() = timeMonitorSurrogate;
842 CommandLineProcessor::getTimeMonitorSurrogate()
844 return getRawTimeMonitorSurrogate();
849 CommandLineProcessor::getRawTimeMonitorSurrogate()
852 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.
Thrown if an unrecognized option was found and throwExceptions==true.
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.
Class that helps parse command line input arguments from (argc,argv[]) and set options.
#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...