00001 #ifndef ARGUMENTPARSER_H
00002 #define ARGUMENTPARSER_H
00003
00011 #include <iostream>
00012 #include <string>
00013 #include <cstdlib>
00014 #include <sstream>
00015 #include <map>
00016 #include <vector>
00017
00021 class ArgumentParser
00022 {
00023 public:
00024
00025 typedef std::vector<std::string> StringContainer;
00026
00030 ArgumentParser ( );
00031
00035 virtual ~ArgumentParser ( );
00036
00043 bool parseArguments (int argc, char** argv );
00044
00050 char* getCstringValue (std::string name );
00051
00056 void setDescription (const char* description );
00057
00062 void setVersion( const char* version );
00063
00069 template <class T> T getValue( std::string name )
00070 {
00071 T tmp;
00072 std::istringstream iss( this->getCstringValue( name ) );
00073
00074 if ( ( iss >> tmp ).fail() )
00075 {
00076 showHelp( true, "Bad argument value type" );
00077 }
00078
00079 return tmp;
00080 }
00081
00085 void clean ( );
00086
00087 protected:
00093 virtual void showHelp ( bool quit=true, const char* custMessage=0 );
00094
00098 virtual void showHelpAppendix ()=0;
00099
00104 void showError( StringContainer parts );
00105
00110 void showError( const char* message );
00111
00116 void showVersion( bool quit=false );
00117
00123 void setDelimiter( const char* new_var );
00124
00130 char* getDelimiter( );
00131
00139 void addArgument (const char* name, const char* description, bool required, const char* def);
00140
00141 private:
00142
00143 const char* helpC_;
00144 const char* versC_;
00145
00146 char* delimiter_;
00147
00148 char* description_;
00149
00150 char* version_;
00151
00152 std::map<std::string,char*> arguments_;
00153
00154 std::map<std::string,char*> descriptions_;
00155
00156 std::map<std::string,bool> requireds_;
00157 std::map<unsigned long,bool> malloced_;
00158
00164 char* copyString (const char* original );
00165
00171 char* copyStringDesc (const char* original );
00172
00177 void freeMap (std::map<std::string,char*>* map );
00178
00179 void myFree( void** pointer );
00180
00184
00185 };
00186
00187 #endif // ARGUMENTPARSER_H