Teuchos - Trilinos Tools Package  Version of the Day
Teuchos_CommandLineProcessor.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Teuchos: Common Tools Package
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #ifndef TEUCHOS_COMMAND_LINE_PROCESSOR_HPP
43 #define TEUCHOS_COMMAND_LINE_PROCESSOR_HPP
44 
53 #include "Teuchos_map.hpp"
54 #include "Teuchos_any.hpp"
56 #include "Teuchos_Ptr.hpp"
57 
73 namespace Teuchos {
74 
75 class TEUCHOSCORE_LIB_DLL_EXPORT CommandLineProcessor {
76 public:
77 
79 
80 
82  class ParseError : public std::logic_error
83  {public: ParseError(const std::string& what_arg) : std::logic_error(what_arg) {}};
84 
86  class HelpPrinted : public ParseError
87  {public: HelpPrinted(const std::string& what_arg) : ParseError(what_arg) {}};
88 
91  {public: UnrecognizedOption(const std::string& what_arg) : ParseError(what_arg) {}};
92 
98  PARSE_SUCCESSFUL = 0
99  ,PARSE_HELP_PRINTED = 1
100  ,PARSE_UNRECOGNIZED_OPTION = 2
101  ,PARSE_ERROR = 3
102  };
103 
105 
107 
108 
125  bool throwExceptions = true
126  ,bool recogniseAllOptions = true
127  ,bool addOutputSetupOptions = false
128  );
129 
133 
135 
137 
138 
140  void throwExceptions( const bool & throwExceptions );
141 
143  bool throwExceptions() const;
144 
146  void recogniseAllOptions( const bool & recogniseAllOptions );
147 
149  bool recogniseAllOptions() const;
150 
152  void addOutputSetupOptions( const bool &addOutputSetupOptions );
153 
155  bool addOutputSetupOptions() const;
156 
158 
160 
161 
164  void setDocString( const char doc_string[] );
165 
178  void setOption(
179  const char option_true[]
180  ,const char option_false[]
181  ,bool *option_val
182  ,const char documentation[] = NULL
183  );
184 
195  void setOption(
196  const char option_name[]
197  ,int *option_val
198  ,const char documentation[] = NULL
199  ,const bool required = false
200  );
201 
212  void setOption(
213  const char option_name[]
214  ,long int *option_val
215  ,const char documentation[] = NULL
216  ,const bool required = false
217  );
218 
219 #ifdef HAVE_TEUCHOS_LONG_LONG_INT
220 
230  void setOption(
231  const char option_name[]
232  ,long long int *option_val
233  ,const char documentation[] = NULL
234  ,const bool required = false
235  );
236 #endif
237 
248  void setOption(
249  const char option_name[]
250  ,double *option_val
251  ,const char documentation[] = NULL
252  ,const bool required = false
253  );
254 
265  void setOption(
266  const char option_name[]
267  ,std::string *option_val
268  ,const char documentation[] = NULL
269  ,const bool required = false
270  );
271 
300  template <class EType>
301  void setOption(
302  const char enum_option_name[]
303  ,EType *enum_option_val
304  ,const int num_enum_opt_values
305  ,const EType enum_opt_values[]
306  ,const char* enum_opt_names[]
307  ,const char documentation[] = NULL
308  ,const bool required = false
309  );
310 
312 
314 
315 
376  int argc
377  ,char* argv[]
378  ,std::ostream *errout = &std::cerr
379  ) const;
380 
382 
384 
385 
394  void printHelpMessage( const char program_name[], std::ostream &out ) const;
395 
401  void printFinalTimerSummary(const Ptr<std::ostream> &out = null);
402 
404 
405 public:
406  //
407  enum EOptType { OPT_NONE, OPT_BOOL_TRUE, OPT_BOOL_FALSE, OPT_INT, OPT_LONG_INT,
408 #ifdef HAVE_TEUCHOS_LONG_LONG_INT
409  OPT_LONG_LONG_INT,
410 #endif
411  OPT_DOUBLE, OPT_STRING, OPT_ENUM_INT };
412 
413  // RAB: 2003/10/10: Note: I had to move this out of the private section since
414  // the sun compiler (version 7) complained (rightly it now appears after looking
415  // up what the ISO/ANSI C++ standard says) about the declaration for opt_val_val_t
416  // not being able to access a private member of CommandLineProcessor.
417 
418 private:
419 
420  // /////////////////////////////////
421  // Private types
422 
423  // ToDo: RAB: 2004/05/25: Clean up these data structures and add
424  // support for a templated enum type. This will clean up usage
425  // quite a bit.
426 
427  //
428  struct opt_val_val_t {
429  opt_val_val_t()
430  :opt_type(OPT_NONE)
431  {}
432  opt_val_val_t( EOptType opt_type_in, const any& opt_val_in, bool required_in )
433  :opt_type(opt_type_in),opt_val(opt_val_in),required(required_in),was_read(false)
434  {}
435  EOptType opt_type;
436  any opt_val; // Will be bool*, int*, double*, std::string* or a small int (for OPT_ENUM_INT)
437  bool required;
438  bool was_read;
439  };
440 
441  //
442  typedef Teuchos::map<std::string,opt_val_val_t> options_list_t;
443 
444  //
445  struct opt_doc_t {
446  opt_doc_t()
447  :opt_type(OPT_NONE)
448  {}
449  opt_doc_t(EOptType opt_type_in, const std::string& opt_name_in, const std::string& opt_name_false_in
450  ,const std::string &documentation_in, const any &default_val_in )
451  :opt_type(opt_type_in),opt_name(opt_name_in),opt_name_false(opt_name_false_in)
452  ,documentation(documentation_in),default_val(default_val_in)
453  {}
454  EOptType opt_type;
455  std::string opt_name;
456  std::string opt_name_false; // only for bool
457  std::string documentation;
458  any default_val;
459  };
460 
461  //
462  typedef std::vector<opt_doc_t> options_documentation_list_t;
463 
464  //
465  struct enum_opt_data_t {
466  enum_opt_data_t()
467  :enum_option_val(NULL), num_enum_opt_values(0)
468  {}
469  enum_opt_data_t(
470  int *_enum_option_val
471  ,const int _num_enum_opt_values
472  ,const int _enum_opt_values[]
473  ,const char* _enum_opt_names[]
474  )
475  :enum_option_val(_enum_option_val)
476  ,num_enum_opt_values(_num_enum_opt_values)
477  ,enum_opt_values(_enum_opt_values,_enum_opt_values+_num_enum_opt_values)
478  {
479  for( int k = 0; k < num_enum_opt_values; ++k )
480  enum_opt_names.push_back(std::string(_enum_opt_names[k]));
481  }
482  int *enum_option_val;
483  int num_enum_opt_values;
484  std::vector<int> enum_opt_values;
485  std::vector<std::string> enum_opt_names;
486  };
487 
488  //
489  typedef std::vector<enum_opt_data_t> enum_opt_data_list_t;
490 
491  // /////////////////////////////////
492  // Private data members
493 
494  bool throwExceptions_;
495  bool recogniseAllOptions_;
496  bool addOutputSetupOptions_;
497  std::string doc_string_;
498 
499  //use pragmas to disable some false positive warnings in windows sharedlib exports
500 #ifdef _MSC_VER
501 #pragma warning(push)
502 #pragma warning(disable:4251)
503 #endif
504  mutable options_list_t options_list_;
505  options_documentation_list_t options_documentation_list_;
506  enum_opt_data_list_t enum_opt_data_list_;
507 #ifdef _MSC_VER
508 #pragma warning(pop)
509 #endif
510 
511  bool output_all_front_matter_;
512  bool output_show_line_prefix_;
513  bool output_show_tab_count_;
514  bool output_show_proc_rank_;
515  int output_to_root_rank_only_;
516  bool print_rcpnode_statistics_on_exit_;
517  bool show_timer_summary_on_exit_;
518 
519  bool printed_timer_summary_;
520 
521  bool added_extra_output_setup_options_;
522  bool in_add_extra_output_setup_options_;
523 
524  static const bool output_all_front_matter_default_;
525  static const bool output_show_line_prefix_default_;
526  static const bool output_show_tab_count_default_;
527  static const bool output_show_proc_rank_default_;
528  static const int output_to_root_rank_only_default_;
529  static const bool print_rcpnode_statistics_on_exit_default_;
530  static const bool show_timer_summary_on_exit_default_;
531 
532  // /////////////////////////////////
533  // Private member functions
534 
535  // Set the extra output setup options
536  void add_extra_output_setup_options() const;
537 
538  // Set an integer enumeration option
539  void setEnumOption(
540  const char enum_option_name[]
541  ,int *enum_option_val
542  ,const int num_enum_opt_values
543  ,const int enum_opt_values[]
544  ,const char* enum_opt_names[]
545  ,const char documentation[]
546  ,const bool required
547  );
548 
549  // Set an enum int option
550  bool set_enum_value(
551  int argv_i
552  ,char* argv[]
553  ,const std::string &enum_opt_name
554  ,const int enum_id
555  ,const std::string &enum_str_val
556  ,std::ostream *errout
557  ) const;
558 
559  // Print the valid enum values
560  void print_enum_opt_names(
561  const int enum_id
562  ,std::ostream &out
563  ) const;
564 
565  // Return the name of the default value for an enum
566  std::string enum_opt_default_val_name(
567  const std::string &enum_name
568  ,const int enum_id
569  ,std::ostream *errout
570  ) const;
571 
572  // Return the index given and option value
573  int find_enum_opt_index(
574  const std::string &enum_opt_name
575  ,const int opt_value
576  ,const enum_opt_data_t &enum_data
577  ,std::ostream *errout
578  ) const;
579 
580  // Get the option and the value from an entry in argv[].
581  // Will return false if entry is not formated properly.
582  bool get_opt_val(
583  const char str[]
584  ,std::string *opt_name
585  ,std::string *opt_val_str // May be empty on return
586  ) const;
587 
588  // String for option type
589  std::string opt_type_str( EOptType ) const;
590 
591  // Print bad option
592  void print_bad_opt(
593  int argv_i
594  ,char* argv[]
595  ,std::ostream *errout
596  ) const;
597 
598 public: // Hidden implementation stuff that clients should never see
599 
635  public:
639  virtual void summarize(std::ostream &out=std::cout) = 0;
640  };
641 
642  static void setTimeMonitorSurrogate(const RCP<TimeMonitorSurrogate> &timeMonitorSurrogate);
643 
644  static RCP<TimeMonitorSurrogate> getTimeMonitorSurrogate();
645 
646 private:
647 
648  static RCP<TimeMonitorSurrogate>& getRawTimeMonitorSurrogate();
649 
650 }; // end class CommandLineProcessor
651 
652 
653 // /////////////////////////
654 // Inline members
655 
656 
657 // Behavior modes
658 
659 
660 inline
661 void CommandLineProcessor::throwExceptions( const bool & throwExceptions_in )
662 { throwExceptions_ = throwExceptions_in; }
663 
664 
665 inline
667 { return throwExceptions_; }
668 
669 
670 inline
671 void CommandLineProcessor::recogniseAllOptions( const bool & recogniseAllOptions_in )
672 { recogniseAllOptions_ = recogniseAllOptions_in; }
673 
674 
675 inline
677 { return recogniseAllOptions_; }
678 
679 
680 inline
681 void CommandLineProcessor::addOutputSetupOptions( const bool &addOutputSetupOptions_in )
682 { addOutputSetupOptions_ = addOutputSetupOptions_in; }
683 
684 
685 inline
687 { return addOutputSetupOptions_; }
688 
689 
690 template <class EType>
691 inline
693  const char enum_option_name[]
694  ,EType *enum_option_val
695  ,const int num_enum_opt_values
696  ,const EType enum_opt_values[]
697  ,const char* enum_opt_names[]
698  ,const char documentation[]
699  ,const bool required
700  )
701 {
702  // RAB: 2004/05/25: Every C++ implementation that I know of just
703  // represents enumerations as int's and therefore this will compile
704  // just fine. However, the ISO/ANSI C++ standard says that
705  // compilers are allowed to use a smaller storage type for an enum
706  // but must not require storage any larger than an 'int'. If the
707  // below compile-time assertion does not compile then we need to do
708  // something different but it will be a lot of work!
709  CompileTimeAssert<sizeof(int)-sizeof(EType)>();
710  //CompileTimeAssert<sizeof(int)-sizeof(EType)-1>(); // Uncomment to see compilation error
711  setEnumOption(
712  enum_option_name
713  ,reinterpret_cast<int*>(enum_option_val)
714  ,num_enum_opt_values
715  ,reinterpret_cast<const int*>(enum_opt_values)
716  ,enum_opt_names
717  ,documentation
718  ,required
719  );
720 }
721 
722 
723 inline
724 std::string CommandLineProcessor::opt_type_str( EOptType opt_type ) const
725 {
726  std::string str;
727  switch( opt_type ) {
728  case OPT_BOOL_TRUE:
729  str = "bool";
730  break;
731  case OPT_INT:
732  str = "int";
733  break;
734  case OPT_LONG_INT:
735  str = "long int";
736  break;
737 #ifdef HAVE_TEUCHOS_LONG_LONG_INT
738  case OPT_LONG_LONG_INT:
739  str = "long long int";
740  break;
741 #endif
742  case OPT_DOUBLE:
743  str = "double";
744  break;
745  case OPT_STRING:
746  str = "string";
747  break;
748  case OPT_ENUM_INT:
749  str = "enum";
750  break;
751  default:
752  assert(0); // Local programming error only
753  }
754  return str;
755 }
756 
757 
758 } // end namespace Teuchos
759 
760 
761 #endif // TEUCHOS_COMMAND_LINE_PROCESSOR_HPP
Modified boost::any class for holding a templated value.
Thrown if –help was specified and throwExceptions==true.
Template classes for testing assertions at compile time.
bool throwExceptions() const
Returns true if an std::exception is thrown, there is a parse error, or help is printed.
Modified boost::any class, which is a container for a templated value.
Definition: Teuchos_any.hpp:86
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.
Interface by which CommandLineProcessor may use TimeMonitor.
bool addOutputSetupOptions() const
Returns true options will be automatically added to setup Teuchos::VerboseObjectBase::getDefaultOStre...
Provides std::map class for deficient platforms.
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.
bool recogniseAllOptions() const
Returns true if all options must be recognized by the parser.
Smart reference counting pointer class for automatic garbage collection.
If instantiated (for Test!=0) then this should not compile!
Thrown if a parse std::exception occurs and throwExceptions==true.
Class that helps parse command line input arguments from (argc,argv[]) and set options.
Simple wrapper class for raw pointers to single objects where no persisting relationship exists...