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 +
"\"";
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)
135 const char option_true[]
136 ,
const char option_false[]
138 ,
const char documentation[]
149 std::string(documentation?documentation:
""),
any(option_val))
155 const char option_name[]
157 ,
const char documentation[]
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[]
191 const char option_name[]
193 ,
const char documentation[]
208 const char option_name[]
209 ,
long long int *option_val
210 ,
const char documentation[]
225 const char option_name[]
227 ,
const char documentation[]
242 const char option_name[]
244 ,
const char documentation[]
259 const char option_name[]
260 ,std::string *option_val
261 ,
const char documentation[]
283 ,std::ostream *errout
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 );
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;
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);
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);
386 remove_quotes(opt_val_str), errout ) )
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;
437 std::ostream &out )
const
445 const int opt_type_w = 14;
446 const char spc_chars[] =
" ";
450 options_documentation_list_t::const_iterator itr;
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"
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" )
523 << setw(opt_name_w) <<
""
524 << setw(opt_type_w) <<
"";
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 );
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)));
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(
597 out <<
"\nDETAILED DOCUMENTATION:\n\n" <<
doc_string_ << std::endl << std::endl;
612 out = rcpFromPtr(out_inout);
647 ,
"Set if all front matter is printed to the default FancyOStream or not"
651 ,
"Set if the line prefix matter is printed to the default FancyOStream or not"
655 ,
"Set if the tab count is printed to the default FancyOStream or not"
659 ,
"Set if the processor rank is printed to the default FancyOStream or not"
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",
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"
675 "If true, then Teuchos::TimeMonitor::summarize() is called in"
676 " CommandLineProcessor's destructor (usually at the end of main)."
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[],
703 enum_opt_data_t(enum_option_val,num_enum_opt_values,enum_opt_values,enum_opt_names)
710 std::string(documentation?documentation:
""),
any(opt_id))
718 ,
const std::string &enum_opt_name
720 ,
const std::string &enum_str_val
721 ,std::ostream *errout
726 std::vector<std::string>::const_iterator
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);
759 typedef std::vector<std::string>::const_iterator itr_t;
760 out <<
"Valid options:";
768 out <<
" " << add_quotes(*itr);
775 const std::string &enum_name
777 ,std::ostream *errout
791 const std::string &enum_opt_name
794 ,std::ostream *errout
797 std::vector<int>::const_iterator
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 << "!"
812 return static_cast<int>(itr - itr_begin);
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 );
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;
882 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...