This class handles an argument of a command line. More...
#include <OBTOptionArg.h>
Public Member Functions | |
virtual std::string | getHelp () const |
The help message given for the option. | |
std::string | getName () const |
The option flag. | |
bool | isPresent () const |
Returns true if the option flag is present in the command line. | |
virtual int | nbPresent () const |
Returns the number of time when the option flag is present in the command line. | |
virtual void | reset () |
Reset the presence flag to re-parse the command line. | |
Static Public Member Functions | |
static OptionArg & | other () |
The other options. | |
static std::string | expandEnvVariable (const std::string &txt) |
utility method to expand environment variables. | |
Protected Member Functions | |
OptionArg (const std::string &flagName, const std::string &helpMsg) | |
Protected constructor for sons. | |
virtual | ~OptionArg () |
Protected destructor for sons. | |
virtual bool | argMatch () |
Returns true if the argument matches the option flag. | |
virtual bool | parseArg ()=0 |
The method to parse the arguments. | |
void | throwError (const std::string &msg) const |
This method is called and rises an exception when an error occurs. | |
Parsing methods. | |
bool | atLastArg () const |
Returns true if it is the last argument of the command line. | |
std::string | getNextArg () const |
Returns the current argument of the command line, then moves to the next one. | |
std::string | peekArg () const |
Returns the current argument of the command line. | |
void | moveNextArg () const |
Moves to the next one. | |
Protected Attributes | |
bool | _flag |
This flag indicates if the option argument is present in the command line. | |
std::string | _flagName |
The option text. | |
std::string | _helpMsg |
The help message. | |
Private Member Functions | |
std::string | getArg (std::size_t pos) const |
bool | tryArg () |
Private Attributes | |
OptionArgHandler * | _handler |
Friends | |
class | OBT::OptionArgHandler |
This class handles an argument of a command line.
This class is the base class of all other option argument classes. See OptionFlag::parseArg, OptionNPrm::parseArg, OptionMultiNPrm::parseArg for examples.
Definition at line 20 of file OBTOptionArg.h.
OptionArg::OptionArg | ( | const std::string & | flagName, | |
const std::string & | helpMsg | |||
) | [protected] |
Protected constructor for sons.
Definition at line 70 of file OBTOptionArg.cpp.
References _handler, OBT::OptionArgHandler::_optionList, and OBT_ASSERT.
00071 : _flag( false ), 00072 _flagName( flagName ), 00073 _helpMsg( helpMsg ), 00074 _handler( OptionArgHandler::_currentHandler ) 00075 { 00076 // Registration in the handler 00077 OBT_ASSERT( _handler != 0 && "Cannot access to the arguments handler, create it before creating the options" ) ; 00078 _handler->_optionList.push_back( this ) ; 00079 }
OptionArg::~OptionArg | ( | ) | [protected, virtual] |
Protected destructor for sons.
Definition at line 81 of file OBTOptionArg.cpp.
References _handler, and OBT::OptionArgHandler::_optionList.
00082 { 00083 if( _handler != 0 ) _handler->_optionList.remove( this ); 00084 }
virtual bool OBT::OptionArg::argMatch | ( | ) | [inline, protected, virtual] |
Returns true if the argument matches the option flag.
Reimplemented in OBT::OtherOptionArg, and OBT::OptionLastArg.
Definition at line 69 of file OBTOptionArg.h.
Referenced by tryArg().
bool OptionArg::atLastArg | ( | ) | const [protected] |
Returns true if it is the last argument of the command line.
Definition at line 132 of file OBTOptionArg.cpp.
References OBT::OptionArgHandler::_argIterator, OBT::OptionArgHandler::_args, _handler, and OBT_ASSERT.
Referenced by OBT::OptionLastArg::argMatch().
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 }
std::string OptionArg::expandEnvVariable | ( | const std::string & | txt | ) | [static] |
utility method to expand environment variables.
[in] | txt | the text to expand. |
The syntax for the environment variable is ${ENV_VAR}. This method is recursive until no more environment variable is found in the txt.
Definition at line 42 of file OBTOptionArg.cpp.
00043 { 00044 std::string result = txt ; 00045 // Loop until "${" is found 00046 for( std::size_t posB = result.find( "${" ) ; 00047 std::string::npos != posB ; 00048 posB = result.find( "${", posB ) ) 00049 { 00050 // try to locate the end of the environment variable 00051 std::size_t posE = result.find( "}", posB ) ; 00052 if( std::string::npos == posE ) 00053 { // End not found => error 00054 result.clear() ; 00055 break ; 00056 } 00057 // Get the environment variable value 00058 char * varValue = ::getenv( result.substr( posB + 2, posE - posB - 2 ).c_str() ) ; 00059 if( 0 == varValue ) 00060 { // environment variable not found => error 00061 result.clear() ; 00062 break ; 00063 } 00064 // replace the environment variable by its value 00065 result.replace( posB, posE - posB + 1, varValue ) ; 00066 } 00067 return result ; 00068 }
std::string OptionArg::getArg | ( | std::size_t | pos | ) | const [private] |
Definition at line 98 of file OBTOptionArg.cpp.
References OBT::OptionArgHandler::_argIterator, OBT::OptionArgHandler::_args, _handler, and OBT_ASSERT.
Referenced by getNextArg(), and peekArg().
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 }
virtual std::string OBT::OptionArg::getHelp | ( | ) | const [inline, virtual] |
The help message given for the option.
Returns the option flag following by the help message.
Reimplemented in OBT::OtherOptionArg, OBT::OptionNPrm, and OBT::OptionMultiNPrm.
Definition at line 30 of file OBTOptionArg.h.
Referenced by throwError().
std::string OBT::OptionArg::getName | ( | ) | const [inline] |
The option flag.
See _flagName.
Definition at line 33 of file OBTOptionArg.h.
00033 { return _flagName ; }
std::string OptionArg::getNextArg | ( | ) | const [protected] |
Returns the current argument of the command line, then moves to the next one.
Definition at line 138 of file OBTOptionArg.cpp.
References OBT::OptionArgHandler::_argIterator, _handler, getArg(), and OBT_ASSERT.
Referenced by OBT::OptionMultiNPrm::parseArg(), and OBT::OptionNPrm::parseArg().
00139 { 00140 OBT_ASSERT( _handler != 0 && "Cannot access to the arguments handler, may be it has been destroyed" ) ; 00141 return getArg( _handler->_argIterator++ ) ; 00142 }
bool OBT::OptionArg::isPresent | ( | ) | const [inline] |
Returns true if the option flag is present in the command line.
See _flag.
Definition at line 36 of file OBTOptionArg.h.
00036 { return _flag ; }
void OptionArg::moveNextArg | ( | ) | const [protected] |
Moves to the next one.
Definition at line 86 of file OBTOptionArg.cpp.
References OBT::OptionArgHandler::_argIterator, OBT::OptionArgHandler::_args, _handler, and OBT_ASSERT.
Referenced by tryArg().
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 }
virtual int OBT::OptionArg::nbPresent | ( | ) | const [inline, virtual] |
Returns the number of time when the option flag is present in the command line.
See _flag.
Reimplemented in OBT::OtherOptionArg.
Definition at line 39 of file OBTOptionArg.h.
00039 { return _flag ? 1 : 0 ; }
OptionArg & OptionArg::other | ( | ) | [static] |
The other options.
This option is given for convinience to write rules like
myFlag ^ OBT::OptionArg::other() ;
Which means that no other flag can be in the command line if myFlag is present.
Definition at line 36 of file OBTOptionArg.cpp.
00037 { 00038 static OtherOptionArg other ; 00039 return other ; 00040 }
virtual bool OBT::OptionArg::parseArg | ( | ) | [protected, pure virtual] |
The method to parse the arguments.
Retrieves the argument which can be following the option flag. See OptionFlag::parseArg, OptionNPrm::parseArg, OptionMultiNPrm::parseArg for examples.
Implemented in OBT::OtherOptionArg, OBT::OptionLastArg, OBT::OptionFlag, OBT::OptionNPrm, and OBT::OptionMultiNPrm.
Referenced by tryArg().
std::string OptionArg::peekArg | ( | ) | const [protected] |
Returns the current argument of the command line.
Definition at line 144 of file OBTOptionArg.cpp.
References OBT::OptionArgHandler::_argIterator, _handler, getArg(), and OBT_ASSERT.
Referenced by OBT::OptionLastArg::argMatch().
00145 { 00146 OBT_ASSERT( _handler != 0 && "Cannot access to the arguments handler, may be it has been destroyed" ) ; 00147 return getArg( _handler->_argIterator ) ; 00148 }
virtual void OBT::OptionArg::reset | ( | ) | [inline, virtual] |
Reset the presence flag to re-parse the command line.
Reimplemented in OBT::OptionMultiNPrm.
Definition at line 41 of file OBTOptionArg.h.
00041 { _flag = false ; }
void OptionArg::throwError | ( | const std::string & | msg | ) | const [protected] |
This method is called and rises an exception when an error occurs.
[in] | msg | the message to display. |
Definition at line 111 of file OBTOptionArg.cpp.
References getHelp().
Referenced by OBT::OptionNPrm::parseArg(), and OBT::OptionFlag::parseArg().
00112 { 00113 std::stringstream error ; 00114 error << "Error parsing option " << getHelp() << ":" 00115 << msg << std:: endl << std:: endl ; 00116 throw ArgException( error.str() ) ; 00117 }
bool OptionArg::tryArg | ( | ) | [private] |
Definition at line 119 of file OBTOptionArg.cpp.
References _flag, argMatch(), moveNextArg(), and parseArg().
00120 { 00121 bool flagFound = false ; 00122 if( argMatch() ) 00123 { 00124 moveNextArg() ; 00125 parseArg() ; 00126 _flag = true ; 00127 flagFound = true ; 00128 } 00129 return flagFound ; 00130 }
friend class OBT::OptionArgHandler [friend] |
Definition at line 96 of file OBTOptionArg.h.
bool OBT::OptionArg::_flag [protected] |
This flag indicates if the option argument is present in the command line.
Definition at line 60 of file OBTOptionArg.h.
Referenced by OBT::OptionLastArg::argMatch(), OBT::OptionNPrm::parseArg(), OBT::OptionFlag::parseArg(), and tryArg().
std::string OBT::OptionArg::_flagName [protected] |
OptionArgHandler* OBT::OptionArg::_handler [private] |
Definition at line 98 of file OBTOptionArg.h.
Referenced by atLastArg(), getArg(), getNextArg(), moveNextArg(), OptionArg(), peekArg(), and ~OptionArg().
std::string OBT::OptionArg::_helpMsg [protected] |