28 inline int my_max(
int a,
int b ) {
return a > b ? a : b; }
31 std::string remove_quotes(
const std::string& str )
33 if (str[0] ==
'\"' && str[str.size()-1] ==
'\"')
34 return str.substr(1,str.size()-2);
35 else if (str[0] ==
'\"')
36 return str.substr(1,str.size()-1);
37 else if (str[str.size()-1] ==
'\"')
38 return str.substr(0,str.size()-1);
43 std::string add_quotes(
const std::string& str )
47 return "\"" + str +
"\"";
67 bool throwExceptions_in
68 ,
bool recogniseAllOptions_in
69 ,
bool addOutputSetupOptions_in
71 :throwExceptions_(throwExceptions_in)
72 ,recogniseAllOptions_(recogniseAllOptions_in)
73 ,addOutputSetupOptions_(addOutputSetupOptions_in)
74 ,output_all_front_matter_(output_all_front_matter_default_)
75 ,output_show_line_prefix_(output_show_line_prefix_default_)
76 ,output_show_tab_count_(output_show_tab_count_default_)
77 ,output_show_proc_rank_(output_show_proc_rank_default_)
78 ,output_to_root_rank_only_(output_to_root_rank_only_default_)
79 ,print_rcpnode_statistics_on_exit_(print_rcpnode_statistics_on_exit_default_)
80 ,show_timer_summary_on_exit_(show_timer_summary_on_exit_default_)
81 ,printed_timer_summary_(false)
82 ,added_extra_output_setup_options_(false)
83 ,in_add_extra_output_setup_options_(false)
103 const char option_true[]
104 ,
const char option_false[]
106 ,
const char documentation[]
117 std::string(documentation?documentation:
""),
any(option_val))
123 const char option_name[]
125 ,
const char documentation[]
134 opt_doc_t(
OPT_INT, option_name,
"", std::string(documentation?documentation:
""),
141 const char option_name[]
142 ,
long int *option_val
143 ,
const char documentation[]
159 const char option_name[]
161 ,
const char documentation[]
176 const char option_name[]
177 ,
long long int *option_val
178 ,
const char documentation[]
193 const char option_name[]
195 ,
const char documentation[]
210 const char option_name[]
212 ,
const char documentation[]
227 const char option_name[]
228 ,std::string *option_val
229 ,
const char documentation[]
251 ,std::ostream *errout
255 std::string opt_name;
256 std::string opt_val_str;
257 const std::string echo_cl_opt =
"echo-command-line";
258 const std::string help_opt =
"help";
259 const std::string pause_opt =
"pause-for-debugging";
264 for(
int i = 1; i < argc; ++i ) {
265 bool gov_return =
get_opt_val( argv[i], &opt_name, &opt_val_str );
266 if( gov_return && opt_name == help_opt ) {
272 for(
int i = 1; i < argc; ++i ) {
273 bool gov_return =
get_opt_val( argv[i], &opt_name, &opt_val_str );
283 if( opt_name == echo_cl_opt ) {
284 if(errout && procRank == 0) {
285 *errout <<
"\nEchoing the command-line:\n\n";
286 for(
int j = 0; j < argc; ++j )
287 *errout << argv[j] <<
" ";
292 if( opt_name == pause_opt ) {
296 int rank_pid = getpid();
300 std::cerr <<
"Rank " << k <<
" has PID " << pids[k] << std::endl;
303 std::cerr <<
"\nType 0 and press enter to continue : ";
305 std::cin >> dummy_int;
326 *(any_cast<
bool*>(opt_val_val.
opt_val)) =
true;
329 *(any_cast<
bool*>(opt_val_val.
opt_val)) =
false;
332 *(any_cast<
int*>(opt_val_val.
opt_val)) = asSafe<int> (opt_val_str);
335 *(any_cast<
long int*>(opt_val_val.
opt_val)) = asSafe<long int> (opt_val_str);
338 *(any_cast<
size_t *>(opt_val_val.
opt_val)) = asSafe<size_t> (opt_val_str);
341 *(any_cast<
long long int*>(opt_val_val.
opt_val)) = asSafe<long long int> (opt_val_str);
344 *(any_cast<
double*>(opt_val_val.
opt_val)) = asSafe<double> (opt_val_str);
347 *(any_cast<
float*>(opt_val_val.
opt_val)) = asSafe<float> (opt_val_str);
350 *(any_cast<std::string*>(opt_val_val.
opt_val)) = remove_quotes(opt_val_str);
354 remove_quotes(opt_val_str), errout ) )
372 const std::string &opt_val_name = (*itr).first;
373 #define CLP_ERR_MSG \
374 "Error, the option --"<<opt_val_name<<" was required but was not set!"
376 *errout << std::endl << argv[0] <<
" : " <<
CLP_ERR_MSG << std::endl;
405 std::ostream &out )
const
413 const int opt_type_w = 14;
414 const char spc_chars[] =
" ";
418 options_documentation_list_t::const_iterator itr;
425 opt_name_w = my_max(opt_name_w,static_cast<int>(itr->opt_name.length()));
427 opt_name_w = my_max(opt_name_w,static_cast<int>(itr->opt_name_false.length()));
433 <<
"Usage: " << program_name <<
" [options]\n"
434 << spc_chars <<
"options:\n"
437 #ifdef HAVE_STD_IOS_BASE_FMTFLAGS
438 << std::left << setw(opt_name_w) <<
"help"
439 << std::left << setw(opt_type_w) <<
" "
441 << std::setiosflags(std::ios::left) << setw(opt_name_w) <<
"help"
442 << std::setiosflags(std::ios::left) << setw(opt_type_w) <<
" "
444 <<
"Prints this help message"
448 #ifdef HAVE_STD_IOS_BASE_FMTFLAGS
449 << std::left << setw(opt_name_w) <<
"pause-for-debugging"
450 << std::left << setw(opt_type_w) <<
" "
452 << std::setiosflags(std::ios::left) << setw(opt_name_w) <<
"pause-for-debugging"
453 << std::setiosflags(std::ios::left) << setw(opt_type_w) <<
" "
455 <<
"Pauses for user input to allow attaching a debugger"
459 #ifdef HAVE_STD_IOS_BASE_FMTFLAGS
460 << std::left << setw(opt_name_w) <<
"echo-command-line"
461 << std::left << setw(opt_type_w) <<
" "
463 << std::setiosflags(std::ios::left) << setw(opt_name_w) <<
"echo-command-line"
464 << std::setiosflags(std::ios::left) << setw(opt_type_w) <<
" "
466 <<
"Echo the command-line but continue as normal"
477 #ifdef HAVE_STD_IOS_BASE_FMTFLAGS
478 << std::left << setw(opt_name_w) << itr->opt_name
479 << std::left << setw(opt_type_w) <<
opt_type_str(itr->opt_type)
481 << std::setiosflags(std::ios::left) << setw(opt_name_w) << itr->opt_name
482 << std::setiosflags(std::ios::left) << setw(opt_type_w) <<
opt_type_str(itr->opt_type)
484 << ( itr->documentation.length() ? itr->documentation.c_str() :
"No documentation" )
491 << setw(opt_name_w) <<
""
492 << setw(opt_type_w) <<
"";
502 << setw(opt_name_w) << itr->opt_name_false;
508 << setw(opt_name_w) <<
" ";
511 << setw(opt_type_w) <<
" "
513 switch( itr->opt_type ) {
515 out <<
"--" << ( (*(any_cast<
bool*>(itr->default_val))) ?
516 itr->opt_name : itr->opt_name_false );
526 out <<
"--" << itr->opt_name;
531 switch( itr->opt_type ) {
535 out <<
"=" << (*(any_cast<
int*>(itr->default_val)));
538 out <<
"=" << (*(any_cast<
long int*>(itr->default_val)));
541 out <<
"=" << (*(any_cast<
size_t*>(itr->default_val)));
544 out <<
"=" << (*(any_cast<
long long int*>(itr->default_val)));
547 out <<
"=" << (*(any_cast<
double*>(itr->default_val)));
550 out <<
"=" << (*(any_cast<
float*>(itr->default_val)));
553 out <<
"=" << add_quotes(*(any_cast<std::string*>(itr->default_val)));
556 out <<
"=" << add_quotes(
565 out <<
"\nDETAILED DOCUMENTATION:\n\n" <<
doc_string_ << std::endl << std::endl;
580 out = rcpFromPtr(out_inout);
615 ,
"Set if all front matter is printed to the default FancyOStream or not"
619 ,
"Set if the line prefix matter is printed to the default FancyOStream or not"
623 ,
"Set if the tab count is printed to the default FancyOStream or not"
627 ,
"Set if the processor rank is printed to the default FancyOStream or not"
631 ,
"Set which processor (the root) gets the output. If < 0, then all processors get output."
634 "print-rcpnode-statistics-on-exit",
"no-print-rcpnode-statistics-on-exit",
636 "Set if the RCPNode usage statistics will be printed on exit or not. Warning,"
637 " this prints to std::cerr or every process so do not turn this on for very large"
643 "If true, then Teuchos::TimeMonitor::summarize() is called in"
644 " CommandLineProcessor's destructor (usually at the end of main)."
654 const char enum_option_name[],
655 int* enum_option_val,
656 const int num_enum_opt_values,
657 const int enum_opt_values[],
658 const char *
const enum_opt_names[],
659 const char documentation[],
671 enum_opt_data_t(enum_option_val,num_enum_opt_values,enum_opt_values,enum_opt_names)
678 std::string(documentation?documentation:
""),
any(opt_id))
686 ,
const std::string &enum_opt_name
688 ,
const std::string &enum_str_val
689 ,std::ostream *errout
694 std::vector<std::string>::const_iterator
697 itr = std::find( itr_begin, itr_end, enum_str_val );
698 if( itr == itr_end ) {
699 const int j = argv_i;
700 #define CLP_ERR_MSG \
701 "Error, the value \"" << enum_str_val << "\" for the " \
702 << j<<(j==1?"st":(j==2?"nd":(j==3?"rd":"th"))) << " option --" \
703 << enum_opt_name << " was not recognized (use --help)!"
705 *errout << std::endl << argv[0] <<
" : " <<
CLP_ERR_MSG << std::endl;
714 const int enum_opt_val_index =
static_cast<int>(itr - itr_begin);
727 typedef std::vector<std::string>::const_iterator itr_t;
728 out <<
"Valid options:";
736 out <<
" " << add_quotes(*itr);
743 const std::string &enum_name
745 ,std::ostream *errout
759 const std::string &enum_opt_name
762 ,std::ostream *errout
765 std::vector<int>::const_iterator
768 itr = std::find( itr_begin, itr_end, opt_value );
769 if( itr == itr_end ) {
770 #define CLP_ERR_MSG \
771 ( recogniseAllOptions() ? "Error" : "Warning" ) \
772 << ", option --" << enum_opt_name << " was given an invalid " \
773 "initial option value of " << opt_value << "!"
780 return static_cast<int>(itr - itr_begin);
786 ,std::string *opt_name
787 ,std::string *opt_val_str
790 const int len =
static_cast<int>(std::strlen(str));
793 if( str[0] !=
'-' || str[1] !=
'-' )
797 for( equ_i = 2; equ_i < len && str[equ_i] !=
'='; ++equ_i );
799 opt_name->assign( str + 2, equ_i-2 );
805 opt_val_str->assign( str + equ_i + 1, len - equ_i - 1 );
813 ,std::ostream *errout
816 const int j = argv_i;
817 #define CLP_ERR_MSG \
818 ( recogniseAllOptions() ? "Error" : "Warning" ) \
819 << ", the " << j<<(j==1?"st":(j==2?"nd":(j==3?"rd":"th"))) \
820 << " option \'" << argv[argv_i] << "\' was not recognized (use --help)!"
822 *errout << std::endl << argv[0] <<
" : " <<
CLP_ERR_MSG << std::endl;
850 return timeMonitorSurrogate;
bool output_show_proc_rank_
static const bool output_all_front_matter_default_
static int getRank()
The rank of the calling process in MPI_COMM_WORLD.
Thrown if –help was specified and throwExceptions==true.
int output_to_root_rank_only_
static int getNProc()
The number of processes in MPI_COMM_WORLD.
static const bool show_timer_summary_on_exit_default_
void print_enum_opt_names(const int enum_id, std::ostream &out) const
void printHelpMessage(const char program_name[], std::ostream &out) const
Print the help message.
bool get_opt_val(const char str[], std::string *opt_name, std::string *opt_val_str) const
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.
bool in_add_extra_output_setup_options_
#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.
void print_bad_opt(int argv_i, char *argv[], std::ostream *errout) const
enum_opt_data_list_t enum_opt_data_list_
void setEnumOption(const char enum_option_name[], int *enum_option_val, const int num_enum_opt_values, const int enum_opt_values[], const char *const enum_opt_names[], const char documentation[], const bool required)
~CommandLineProcessor()
Destructor.
static void setTimeMonitorSurrogate(const RCP< TimeMonitorSurrogate > &timeMonitorSurrogate)
Thrown if an unrecognized option was found and throwExceptions==true.
options_documentation_list_t options_documentation_list_
static const bool output_show_tab_count_default_
static const bool output_show_proc_rank_default_
static const bool output_show_line_prefix_default_
void setOption(const char option_true[], const char option_false[], bool *option_val, const char documentation[]=NULL)
Set a boolean option.
static const int output_to_root_rank_only_default_
std::string opt_type_str(EOptType) const
static RCP< FancyOStream > getDefaultOStream()
Get the default output stream object.
bool output_show_tab_count_
bool output_all_front_matter_
EParseCommandLineReturn parse(int argc, char *argv[], std::ostream *errout=&std::cerr) const
Parse a command line.
static RCP< TimeMonitorSurrogate > getTimeMonitorSurrogate()
void resize(size_type new_size, const value_type &x=value_type())
static const bool print_rcpnode_statistics_on_exit_default_
std::vector< std::string > enum_opt_names
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.
bool printed_timer_summary_
options_list_t options_list_
bool addOutputSetupOptions_
A MPI utilities class, providing methods for initializing, finalizing, and querying the global MPI se...
bool show_timer_summary_on_exit_
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 print_rcpnode_statistics_on_exit_
void add_extra_output_setup_options() const
std::string enum_opt_default_val_name(const std::string &enum_name, const int enum_id, std::ostream *errout) const
static void barrier()
Call MPI_Barrier() on MPI_COMM_WORLD.
bool recogniseAllOptions() const
Returns true if all options must be recognized by the parser.
int find_enum_opt_index(const std::string &enum_opt_name, const int opt_value, const enum_opt_data_t &enum_data, std::ostream *errout) const
bool output_show_line_prefix_
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.
bool set_enum_value(int argv_i, char *argv[], const std::string &enum_opt_name, const int enum_id, const std::string &enum_str_val, std::ostream *errout) const
static RCP< TimeMonitorSurrogate > & getRawTimeMonitorSurrogate()
bool added_extra_output_setup_options_
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.
std::vector< int > enum_opt_values
#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...