34 #ifndef SOMAIO_GETOPT_GETOPT_H 
   35 #define SOMAIO_GETOPT_GETOPT_H 
   59     std::runtime_error( message ) {}
 
   73     std::runtime_error( message ) {}
 
   86                            const std::string &value );
 
   88     std::runtime_error( message ) {}
 
  107   virtual bool feed( 
const std::string & ) = 0;
 
  115   inline const std::string &
name() 
const;
 
  121   virtual std::string 
info() 
const = 0;
 
  149 template <
typename T>
 
  155                 unsigned numMin, 
unsigned numMax );
 
  158   virtual bool recognizeName( 
const std::string & );
 
  159   virtual bool feed( 
const std::string & );
 
  160   virtual void check();
 
  161   virtual std::string info() 
const;
 
  164   unsigned _numMin, _numMax;
 
  165   typename T::iterator _storeNextValue;
 
  166   unsigned _numValuesStored;
 
  170 template <
typename T>
 
  174 template <
typename T> 
inline  
  175 OptionSeries<T>::OptionSeries( T &serie, 
const std::string &name, 
 
  176                                const std::string &info,
 
  177                                unsigned numMin, 
unsigned numMax ) :
 
  178   OptionBase( name, info ),
 
  182   _storeNextValue( _values.begin() ),
 
  183   _numValuesStored( 0 )
 
  188 template <
typename T> 
inline  
  189 bool OptionSeries<T>::recognizeName( 
const std::string &name )
 
  191   if( _nameInList( name ) ) {
 
  192     if ( _numMax && _numValuesStored >= _numMax ) {
 
  193       throw unexpected_option( name );
 
  201 template <
typename T> 
inline  
  202 bool OptionSeries<T>::feed( 
const std::string &value )
 
  204   if ( _numMax == 0 || _numValuesStored < _numMax ) {
 
  205     if ( _storeNextValue == _values.end() ) {
 
  206       _values.push_back( 
typename T::value_type() );
 
  207       stringTo< typename T::value_type >( value, *(_values.rbegin()) );
 
  208       _storeNextValue = _values.end();
 
  210       stringTo< typename T::value_type >( value, *_storeNextValue );
 
  220 template <
typename T> 
inline  
  221 void OptionSeries<T>::check()
 
  223   if ( _values.size() < _numMin ) 
throw missing_option( name() );
 
  227 template <
typename T> 
inline  
  228 std::string OptionSeries<T>::info()
 const 
  233     return std::string( 
"[ " ) + names() + 
" <"  
  246 template <
typename T>
 
  258                        const std::string &
info, 
bool optional );
 
  262   virtual bool feed( 
const std::string & );
 
  268 template <
typename T>
 
  272 template <
typename T>
 
  274                                       const std::string &info, 
 
  278   _optional( optional ),
 
  284 template <
typename T>  
 
  287   if( _nameInList( n ) ) {
 
  297 template <
typename T> 
 
  303     stringTo< T >( value, _value );
 
  310 template <
typename T> 
inline  
  313   if ( ! _optional && ! _valueRead ) {
 
  319 template <
typename T> 
inline  
  323     return  std::string( 
"[ " ) + names() + 
" <"  
  351 template <
class NumberType>
 
  366 template <
class NumberType>
 
  374                        const std::string &
name, 
 
  375                        const std::string &
info, 
bool optional );
 
  379   virtual bool feed( 
const std::string & );
 
  380   std::string 
info() 
const;
 
  384 template <
typename NumberType>
 
  387               const std::string &name, 
 
  388               const std::string &info, 
bool optional ) :
 
  389   SingleOption<NumberType>( value.value, name, info, optional ) {}
 
  392 template <
typename NumberType>
 
  396 template <
typename NumberType>
 
  398 recognizeName( 
const std::string &n )
 
  400   if( this->_nameInList( n ) ) {
 
  401     if ( this->_valueRead ) {
 
  404     this->_value = NumberType( 1 );
 
  412 template <
typename NumberType>
 
  415   if ( this->_valueRead ) {
 
  419       stringTo< NumberType >( value, this->_value );
 
  424     this->_valueRead = 
true;
 
  430 template <
class NumberType> 
inline  
  433   return  std::string( 
"[ " ) + this->names() + 
" [ <"  
  435     + 
"> ] ]\n" + this->_info;
 
  508   template <
typename T>
 
  509   void addOption( T & param, 
const std::string & token, 
 
  510                   const std::string & description, 
 
  511                   bool optional = 
false );
 
  531   template <
typename T>
 
  533                         const std::string & description, 
unsigned minnum = 0, 
 
  534                         unsigned maxnum = 0 );
 
  536   void alias( 
const std::string &to, 
const std::string &from );
 
  549 template <
typename T> 
inline  
  551                                const std::string &info, 
bool optional )
 
  557 template <
typename T> 
inline  
  559                                      const std::string &info, 
 
  560                                      unsigned numMin, 
unsigned numMax )
 
  583   std::string _documentation;
 
  589                     const std::string &documentation );
 
  614     std::runtime_error( message ) {}
 
BoolOrNumber(NumberType &v)
Cartograph options parser.
virtual ~CartoApplication()
CartoApplication(int argc, const char **argv, const std::string &documentation)
virtual void initialize()
Parse and check all parameters, and handle common options (such as the "--help" option).
std::string names() const
OptionBase(const std::string &name, const std::string &info)
void addName(const std::string &name)
virtual bool recognizeName(const std::string &)=0
const std::string & name() const
bool _nameInList(const std::string &name) const
std::vector< std::string > _names
virtual bool feed(const std::string &)=0
virtual std::string info() const =0
Commandline options parser.
std::vector< std::string > CommandLineArguments
void alias(const std::string &to, const std::string &from)
Gives an alternative name (token) to to the parameter of token from.
void check()
Check all needed parameters are filled in, and series numbers are OK.
void pushOption(OptionBase *)
void addOption(T ¶m, const std::string &token, const std::string &description, bool optional=false)
Adds a single parameter to be parsed on the commandline.
void addOptionSeries(T ¶m, const std::string &token, const std::string &description, unsigned minnum=0, unsigned maxnum=0)
Adds a series of parameters to be parsed on the commandline.
void parse()
Parses the commandline arguments (argc, argv) and fills in the registered parameters variables.
OptionsParser(int argc, const char **argv)
std::vector< OptionBase * > OptionList
SingleOption(T &value, const std::string &name, const std::string &info, bool optional)
virtual bool recognizeName(const std::string &)
virtual bool feed(const std::string &)
missing_option(const std::string &message, bool)
missing_option(const std::string &optionName)
unexpected_option_value(const std::string &optionName, const std::string &value)
unexpected_option_value(const std::string &message, bool)
unexpected_option(const std::string &optionName)
unexpected_option(const std::string &message, bool)
A special exception which is thrown after a command help is requested and displayed in a CartoApplica...
user_interruption(const std::string &message)