Generated on Mon Jun 23 16:24:56 2008 for BIU-1.7.0 by doxygen 1.5.1

src/biu/OptionParser.hh

Go to the documentation of this file.
00001 // OptionParser.h: Schnittstelle für die Klasse COptionParser.
00002 /*
00003 
00004     <author>    Martin Mann                 </author>
00005     <created>   9.10.2005                   </created>
00006 
00007     <info>                                  </info>
00008 */
00009 //
00011 
00012 #if !defined(IT_OPTIONPARSER_H__INCLUDED)
00013 #define IT_OPTIONPARSER_H__INCLUDED
00014 
00015 #include <map>
00016 #include <vector>
00017 #include <string>
00018 #include <sstream>
00019 
00020 
00021 namespace biu {
00022 
00028 class COption {
00029 
00030 public:
00031 
00032     static std::string DEF_INIT;
00033 
00034     std::string option;         
00035     bool optional;              
00036     int retType;                
00037     std::string description;    
00038     std::string strValue;       
00039     std::string defValue;       
00040     bool exist;                 
00041 
00043     enum TYPES { STRING, CHAR, INT, FLOAT, DOUBLE, BOOL , TYPES_SIZE};
00044     
00046     static std::vector<std::string> initTypeNames() {
00047         std::vector<std::string> ret = std::vector<std::string>(TYPES_SIZE, "TYPE");
00048 
00049         ret[STRING] = "str ";
00050         ret[CHAR]   = "char";
00051         ret[INT]    = "int ";
00052         ret[FLOAT]  = "flt ";
00053         ret[DOUBLE] = "dbl ";
00054         ret[BOOL]   = "bool";
00055 
00056         return ret;
00057     }
00059     static std::vector<std::string> TYPE_NAME;
00060 
00062     COption (std::string _option, bool _optional, int _retType, std::string _description, std::string _defaultValue=DEF_INIT) :
00063         option(_option),
00064         optional(_optional),
00065         retType(_retType),
00066         description(_description),
00067         strValue(_defaultValue),
00068         defValue(_defaultValue),
00069         exist(false)
00070         {}
00071 
00072     ~COption() {}
00073 };
00074 
00075 
00077 // TYPEDEFS
00079 
00081 typedef std::vector<COption> OptionMap;
00083 typedef OptionMap::const_iterator OptionMapIt;
00084 
00091 class COptionParser {
00092     
00093 private:
00094 
00095     OptionMap opt;              
00096     std::string programName;    
00097     std::string infoText;       
00098 
00099     bool errorOccured;          
00100     unsigned int maxOName;
00101     std::map<std::string,int> mopt; 
00102 
00104     enum ERRORS { ERR_NO_OPT, ERR_WR_USE, ERR_WR_VAL, ERR_NO_ARG };
00105 
00110     void coutError(int error, std::string optionName, std::string errormsg);
00111 
00116     bool isCastable(std::string val, int type) const;
00117 
00119     void parseOpt(int argc, char** argv);
00120 
00122     void coutLineBreaking(std::string text, std::ostream &os,const int emptyHeadSize,const int lineLength) const;
00123 
00124 public:
00125 
00127     static int OUTPUT_LINE_LENGTH;
00128 
00130     COptionParser(OptionMap _options, int argc, char** argv, std::string infoText);
00131 
00133     bool noErrors();
00134 
00136     void coutUsage() const;
00137 
00140     bool argExist(std::string option);
00141 
00142     // Parse-Funktionen
00143 
00144     std::string getStrVal(std::string arg);
00145     char    getCharVal(std::string arg);
00146     int     getIntVal(std::string arg);
00147     float   getFloatVal(std::string arg);
00148     double  getDoubleVal(std::string arg);
00149     bool    getBoolVal(std::string arg);
00150 
00151 };
00152 
00153 } // namespace biu
00154 
00155 
00156 #endif // !defined(IT_OPTIONPARSER_H__INCLUDED)
00157 
00158 /*  Beispiel
00159 
00160 int main(int argc, char ** argv) {
00161 
00162     OptionMap allowedArgs;
00163     allowedArgs.push_back(COption("text", false, COption::STRING, "input sequence file with given degeneration"));
00164     allowedArgs.push_back(COption("max", false, COption::INT, "maximal degeneration that is used"));
00165     ...
00166 
00167     COptionParser opts = COptionParser(allowedArgs, argc, argv, "infotext");
00168 
00169     if (opts.noErrors()) {
00170         std::string text = opts.getStrVal("text");
00171 
00172         ...
00173 
00174         return 0;
00175 
00176     } else {
00177 
00178         ...
00179 
00180         return -1;
00181     }
00182 }
00183 
00184 */