00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <string>
00019 #include <vector>
00020 #include <sstream>
00021
00022
00023
00024
00025 #ifdef USE_GCC_NEW_VERSION
00026 #include <ext/functional>
00027 #else
00028 #include <functional>
00029
00030 #endif
00031
00048 namespace cmdl {
00049
00050
00059 class CmdLineException : public std::exception
00060 {
00061 public:
00080 enum error_t
00081 {
00082 unknown_option = 0,
00083 invalid_argument = 1,
00084 too_many_arguments = 2,
00085 no_multiple_use = 3,
00086 argument_expected = 4,
00087 option_expected = 5
00088 };
00089
00100 CmdLineException(const std::string& strOption, error_t nError)
00101 : m_strOption(strOption), m_nError(nError) { }
00102
00103
00104 virtual ~CmdLineException() throw();
00105
00110 error_t get_error_code() const { return m_nError; }
00111
00115 const std::string& get_option(){ return m_strOption; }
00116
00124 virtual const char *what() const throw();
00125
00126 private:
00130 std::string m_strOption;
00134 error_t m_nError;
00138 mutable std::string m_strError;
00139 };
00140
00141
00142
00143
00144
00145
00146
00147
00158 class CmdLine
00159 {
00160 public:
00173 enum option_t
00174 { cmdLineDefault = 0,
00175 cmdMultiple = 1
00176 };
00177
00182 CmdLine( char c_option_char = '-' );
00183
00196 void Init(int argc, char *argv[]);
00197
00205 void Done();
00206
00207
00208
00209
00210
00233 template <class T>
00234 bool GetSingleValue( const char* pszOpt, T& tDest)
00235 {
00236 const char *arg = getSingleArgForOption( pszOpt );
00237 if (arg)
00238 {
00239 transform( arg, tDest, pszOpt );
00240 return true;
00241 }
00242 else
00243 return false;
00244 }
00245
00251 bool GetSingleValue( const char* pszOpt, bool &bDest)
00252 {
00253 bool bOptionSet = OptionSetNoArgs( pszOpt );
00254
00255 if ( OptionSetNoArgs( pszOpt ) )
00256 throw CmdLineException(pszOpt, CmdLineException::no_multiple_use);
00257
00258 if (bOptionSet) bDest = true;
00259
00260 return bOptionSet;
00261 }
00262
00263
00264
00265
00266
00283 unsigned short Call( const char* pszOpt, void (*function)(), char options = cmdLineDefault)
00284 {
00285 unsigned short argc = 0;
00286 while (OptionSetNoArgs( pszOpt ))
00287 {
00288 function();
00289 ++argc;
00290 if ( !( options & cmdMultiple ) && OptionSetNoArgs( pszOpt ) )
00291 throw CmdLineException(pszOpt, CmdLineException::no_multiple_use);
00292 }
00293 return argc;
00294 }
00295
00323 template <class T>
00324 unsigned short Call( const char *pszOpt, void (*function)(const T& strDest), char options = cmdLineDefault)
00325 {
00326 unsigned short argc=0;
00327 CharPtrList_t::iterator found = m_CharPtrList.begin();
00328 const char *arg=GetNextArgForOption(pszOpt, found);
00329 while ( arg )
00330 {
00331 T targ;
00332 transform<T>( arg, targ, pszOpt);
00333 function( targ );
00334
00335 arg=GetNextArgForOption(pszOpt, found);
00336 ++argc;
00337
00338 if ( arg && !( options & cmdMultiple ))
00339 throw CmdLineException(pszOpt, CmdLineException::too_many_arguments);
00340 }
00341 return argc;
00342 }
00343
00354 template <class C, class T>
00355 unsigned short Call( const char *pszOpt, C& object, void (C::*function)(const T& strDest), char options = cmdLineDefault)
00356 {
00357 unsigned short argc=0;
00358 CharPtrList_t::iterator found = m_CharPtrList.begin();
00359 const char *arg=GetNextArgForOption(pszOpt, found);
00360 while ( arg )
00361 {
00362 T targ;
00363 transform<T>( arg, targ, pszOpt);
00364
00365 #ifdef USE_GCC_NEW_VERSION
00366 __gnu_cxx::mem_fun1_ref( function )( object, targ );
00367 #else
00368 mem_fun1_ref( function )( object, targ );
00369
00370 #endif
00371
00372 arg=GetNextArgForOption(pszOpt, found);
00373 ++argc;
00374
00375 if ( arg && !( options & cmdMultiple ))
00376 throw CmdLineException(pszOpt, CmdLineException::too_many_arguments);
00377 }
00378 return argc;
00379 }
00380
00381
00382 private:
00385 typedef std::vector <const char*> CharPtrList_t;
00386
00392 CharPtrList_t m_CharPtrList;
00393
00398 char m_c_option_char;
00399
00402 void transform( const char * from, std::string& to, const char * )
00403 {
00404 to = from;
00405 }
00406
00409 template <class T>
00410 void transform( const char * from, T& to, const char * pszOpt)
00411 {
00412 std::string strValue( from );
00413 std::istringstream in(strValue);
00414 in >> to;
00415 if ( in.fail() || in.bad() || in.peek() > 0)
00416 throw CmdLineException(pszOpt, CmdLineException::invalid_argument);
00417 }
00418
00426 bool IsOption( CharPtrList_t::iterator it ){ return !*it || **it == m_c_option_char; }
00427
00435 const char *FindFirstArgForOption( const char *pszOpt, CharPtrList_t::iterator &);
00436
00439 const char* GetNextArgForOption (const char *pszOpt, CharPtrList_t::iterator &);
00440
00447 bool OptionSetNoArgs(const char *pszOpt );
00448
00452 const char *getSingleArgForOption( const char *pszOpt );
00453 };
00454
00455
00456
00457
00458 };