00001 #include "OBTOptionArg.h"
00002 #include "OBTOptionArgHandler.h"
00003 #include "OBTArgException.h"
00004 #include "OBT_ASSERT.h"
00005 #include <sstream>
00006 #include <fstream>
00007
00008 namespace OBT
00009 {
00010
00011 class OtherOptionArg : public OptionArg
00012 {
00013 public:
00014 OtherOptionArg() : OptionArg( "", "" ) {}
00015 virtual ~OtherOptionArg() {}
00016 virtual std::string getHelp() const { return "" ; }
00017 virtual bool argMatch() { return false ; }
00018 virtual bool parseArg() { return true ; }
00019 virtual int nbPresent() const
00020 {
00021 int nb = 0 ;
00022 const ListArg& optionList = OptionArgHandler::get()._optionList ;
00023 for( ListArg::const_iterator option = optionList.begin() ;
00024 option != optionList.end() ;
00025 option++ )
00026 {
00027 nb += ( (*option) != this ) && (*option)->isPresent() ? 1 : 0 ;
00028 }
00029 return nb ;
00030 }
00031 } ;
00032 }
00033
00034 using namespace OBT ;
00035
00036 OptionArg& OptionArg::other()
00037 {
00038 static OtherOptionArg other ;
00039 return other ;
00040 }
00041
00042 std::string OptionArg::expandEnvVariable( const std::string& txt )
00043 {
00044 std::string result = txt ;
00045
00046 for( std::size_t posB = result.find( "${" ) ;
00047 std::string::npos != posB ;
00048 posB = result.find( "${", posB ) )
00049 {
00050
00051 std::size_t posE = result.find( "}", posB ) ;
00052 if( std::string::npos == posE )
00053 {
00054 result.clear() ;
00055 break ;
00056 }
00057
00058 char * varValue = ::getenv( result.substr( posB + 2, posE - posB - 2 ).c_str() ) ;
00059 if( 0 == varValue )
00060 {
00061 result.clear() ;
00062 break ;
00063 }
00064
00065 result.replace( posB, posE - posB + 1, varValue ) ;
00066 }
00067 return result ;
00068 }
00069
00070 OptionArg::OptionArg( const std::string& flagName, const std::string& helpMsg )
00071 : _flag( false ),
00072 _flagName( flagName ),
00073 _helpMsg( helpMsg ),
00074 _handler( OptionArgHandler::_currentHandler )
00075 {
00076
00077 OBT_ASSERT( _handler != 0 && "Cannot access to the arguments handler, create it before creating the options" ) ;
00078 _handler->_optionList.push_back( this ) ;
00079 }
00080
00081 OptionArg::~OptionArg()
00082 {
00083 if( _handler != 0 ) _handler->_optionList.remove( this );
00084 }
00085
00086 void OptionArg::moveNextArg() const
00087 {
00088 OBT_ASSERT( _handler != 0 && "Cannot access to the arguments handler, may be it has been destroyed" ) ;
00089 _handler->_argIterator++ ;
00090 if( _handler->_args.size() < _handler->_argIterator )
00091 {
00092 std::stringstream error ;
00093 error << "Unable to move to argument #" << ( _handler->_argIterator ) << ": too few arguments" << std::endl << std:: endl ;
00094 throw ArgException( error.str() ) ;
00095 }
00096 }
00097
00098 std::string OptionArg::getArg( std::size_t pos ) const
00099 {
00100 OBT_ASSERT( _handler != 0 && "Cannot access to the arguments handler, may be it has been destroyed" ) ;
00101 OBT_ASSERT( _handler->_args.size() != 0 && "call OptionArgHandler::parse method to initialise" ) ;
00102 if( _handler->_args.size() <= pos )
00103 {
00104 std::stringstream error ;
00105 error << "Unable to get the argument #" << ( _handler->_argIterator ) << ": too few arguments" << std::endl << std:: endl ;
00106 throw ArgException( error.str() ) ;
00107 }
00108 return _handler->_args[ pos ] ;
00109 }
00110
00111 void OptionArg::throwError( const std::string& msg ) const
00112 {
00113 std::stringstream error ;
00114 error << "Error parsing option " << getHelp() << ":"
00115 << msg << std:: endl << std:: endl ;
00116 throw ArgException( error.str() ) ;
00117 }
00118
00119 bool OptionArg::tryArg()
00120 {
00121 bool flagFound = false ;
00122 if( argMatch() )
00123 {
00124 moveNextArg() ;
00125 parseArg() ;
00126 _flag = true ;
00127 flagFound = true ;
00128 }
00129 return flagFound ;
00130 }
00131
00132 bool OptionArg::atLastArg() const
00133 {
00134 OBT_ASSERT( _handler != 0 && "Cannot access to the arguments handler, may be it has been destroyed" ) ;
00135 return _handler->_argIterator == _handler->_args.size() - 1 ;
00136 }
00137
00138 std::string OptionArg::getNextArg() const
00139 {
00140 OBT_ASSERT( _handler != 0 && "Cannot access to the arguments handler, may be it has been destroyed" ) ;
00141 return getArg( _handler->_argIterator++ ) ;
00142 }
00143
00144 std::string OptionArg::peekArg() const
00145 {
00146 OBT_ASSERT( _handler != 0 && "Cannot access to the arguments handler, may be it has been destroyed" ) ;
00147 return getArg( _handler->_argIterator ) ;
00148 }
00149