Teuchos_CommandLineProcessor.hpp

Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 //                    Teuchos: Common Tools Package
00005 //                 Copyright (2004) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 // 
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 
00025 // 
00026 // ***********************************************************************
00027 // @HEADER
00028 
00029 // //////////////////////////////////////////////////
00030 // Teuchos_CommandLineProcessor.hpp
00031 
00032 #ifndef TEUCHOS_COMMAND_LINE_PROCESSOR_HPP
00033 #define TEUCHOS_COMMAND_LINE_PROCESSOR_HPP
00034 
00038 #include "Teuchos_map.hpp"
00039 #include "Teuchos_ConfigDefs.hpp"
00040 
00041 namespace Teuchos {
00042 
00044 
00057 /* ToDo: RAB: 2003/10/02: Add support for required options as per KRL's suggestion
00058  *
00059  * ToDo:  Finish documentation.
00060  */
00061 
00062 class CommandLineProcessor {
00063 public:
00064 
00068   enum EParseCommandLineReturn {
00069     PARSE_SUCCESSFUL              =  0 
00070     ,PARSE_HELP_PRINTED            =  1 
00071     ,PARSE_UNRECOGNIZED_OPTION     = -1 
00072   };
00073 
00075 
00076 
00085   CommandLineProcessor(
00086     bool    throwExceptions      = true
00087     ,bool   recogniseAllOptions  = true
00088     );
00089 
00091 
00093 
00095 
00107   void setOption(
00108     const char     option_true[]
00109     ,const char    option_false[]
00110     ,bool          *option_val
00111     ,const char    documentation[] = NULL
00112     );
00113 
00115 
00125   void setOption(
00126     const char     option_name[]
00127     ,int           *option_val
00128     ,const char    documentation[] = NULL
00129     );
00130 
00132 
00142   void setOption(
00143     const char     option_name[]
00144     ,double        *option_val
00145     ,const char    documentation[] = NULL
00146     );
00147 
00149 
00159   void setOption(
00160     const char     option_name[]
00161     ,std::string   *option_val
00162     ,const char    documentation[] = NULL
00163     );
00164 
00166 
00168 
00170 
00199   EParseCommandLineReturn  parse(
00200     int             argc
00201     ,char*          argv[]
00202     ,std::ostream   *errout    = &std::cerr
00203     ) const;
00204 
00206 
00208 
00210   void throwExceptions ( const bool & throwExceptions ) { throwExceptions_ = throwExceptions; };
00211   
00213   const bool& throwExceptions() const { return throwExceptions_; };
00214 
00216   void recogniseAllOptions ( const bool & recogniseAllOptions ) { recogniseAllOptions_ = recogniseAllOptions; };
00217 
00219   const bool& recogniseAllOptions() const { return recogniseAllOptions_; };
00220 
00222 
00224 
00226   class ParseError : public std::logic_error
00227   {public: ParseError(const std::string& what_arg) : std::logic_error(what_arg) {}};
00228 
00230   class HelpPrinted : public ParseError
00231   {public: HelpPrinted(const std::string& what_arg) : ParseError(what_arg) {}};
00232 
00234   class UnrecognizedOption : public ParseError
00235   {public: UnrecognizedOption(const std::string& what_arg) : ParseError(what_arg) {}};
00236 
00238 
00239 public:
00240   //
00241   enum EOptType { OPT_NONE, OPT_BOOL_TRUE, OPT_BOOL_FALSE, OPT_INT, OPT_DOUBLE, OPT_STRING };
00242   // RAB: 2003/10/10: Note: I had to move this out of the private section since
00243   // the sun compiler (version 7) complained (rightly it now appears after looking
00244   // up what the ISO/ANSI C++ standard says) about the declaration for opt_val_val_t
00245   // not being able to access a private member of CommandLineProcessor.
00246 
00247 private:
00248 
00249   // /////////////////////////////////
00250   // Private types
00251 
00252   //
00253   struct opt_val_val_t {
00254     opt_val_val_t()
00255       :opt_type(OPT_NONE),opt_val(NULL)
00256       {}
00257     opt_val_val_t( EOptType opt_type_in, void* opt_val_in )
00258       :opt_type(opt_type_in),opt_val(opt_val_in)
00259       {}
00260     EOptType     opt_type;
00261     void         *opt_val; // Will be bool*, int* or double*
00262   };
00263 
00264   //
00265   typedef Teuchos::map<std::string,opt_val_val_t>   options_list_t;
00266 
00267   //
00268   struct opt_doc_t {
00269     opt_doc_t()
00270       :opt_type(OPT_NONE)
00271       {}
00272     opt_doc_t(EOptType opt_type_in, const std::string& opt_name_in, const std::string& opt_name_false_in
00273             ,const std::string &documentation_in, void* default_val_in )
00274       :opt_type(opt_type_in),opt_name(opt_name_in),opt_name_false(opt_name_false_in)
00275       ,documentation(documentation_in),default_val(default_val_in)
00276       {}
00277     EOptType     opt_type;
00278     std::string  opt_name;
00279     std::string  opt_name_false; // only for bool
00280     std::string  documentation;
00281     void         *default_val;
00282   };
00283   
00284   //
00285   typedef std::vector<opt_doc_t>   options_documentation_list_t;
00286 
00287   // /////////////////////////////////
00288   // Private data members
00289 
00290   bool        throwExceptions_;
00291   bool        recogniseAllOptions_;
00292   options_list_t                   options_list_;
00293   options_documentation_list_t     options_documentation_list_;
00294 
00295   // /////////////////////////////////
00296   // Private member functions
00297 
00298   // Get the option and the value from an entry in argv[].
00299   // Will return false if entry is not formated properly.
00300   bool get_opt_val(
00301     const char     str[]
00302     ,std::string   *opt_name
00303     ,std::string   *opt_val_str // May be empty on return
00304     ) const;
00305 
00306   // Print help message
00307   void print_help_msg(
00308     int             argc
00309     ,char*          argv[]
00310     ,std::ostream   *errout
00311     ) const;
00312 
00313   // String for option type
00314   std::string opt_type_str( EOptType ) const;
00315 
00316   // Print bad option
00317   void print_bad_opt(
00318     int             argv_i
00319     ,char*          argv[]
00320     ,std::ostream   *errout
00321     ) const;
00322   
00323 
00324 }; // end class CommandLineProcessor
00325 
00326 // /////////////////////////
00327 // Inline members
00328 
00329 inline
00330 std::string CommandLineProcessor::opt_type_str( EOptType opt_type ) const
00331 {
00332   std::string str;
00333   switch( opt_type ) {
00334     case OPT_BOOL_TRUE:
00335       str = "bool";
00336       break;
00337     case OPT_INT:
00338       str = "int";
00339       break;
00340     case OPT_DOUBLE:
00341       str = "double";
00342       break;
00343     case OPT_STRING:
00344       str = "string";
00345       break;
00346     default:
00347       assert(0); // Local programming error only
00348   } 
00349   return str;
00350 }
00351 
00352 } // end namespace Teuchos
00353 
00354 #endif // TEUCHOS_COMMAND_LINE_PROCESSOR_HPP

Generated on Thu Sep 18 12:42:50 2008 for Teuchos - Trilinos Tools Package by doxygen 1.3.9.1