34#ifndef SOMAIO_GETOPT_GETOPT_H
35#define SOMAIO_GETOPT_GETOPT_H
37#include <cartobase/type/types.h>
38#include <cartobase/type/string_conversion.h>
39#include <cartobase/exception/format.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;
154 OptionSeries( T &serie,
const std::string &
name,
const std::string &info,
155 unsigned numMin,
unsigned numMax );
156 virtual inline ~OptionSeries();
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;
171inline OptionSeries<T>::~OptionSeries() {}
174template <
typename T>
inline
175OptionSeries<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 )
188template <
typename T>
inline
189bool OptionSeries<T>::recognizeName(
const std::string &name )
191 if( _nameInList( name ) ) {
192 if ( _numMax && _numValuesStored >= _numMax ) {
201template <
typename T>
inline
202bool 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() );
208 _storeNextValue = _values.end();
220template <
typename T>
inline
221void OptionSeries<T>::check()
227template <
typename T>
inline
228std::string OptionSeries<T>::info()
const
233 return std::string(
"[ " ) + names() +
" <"
258 const std::string &
info,
bool optional );
262 virtual bool feed(
const std::string & );
274 const std::string &
info,
310template <
typename T>
inline
319template <
typename T>
inline
323 return std::string(
"[ " ) +
names() +
" <"
351template <
class NumberType>
366template <
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;
384template <
typename NumberType>
387 const std::string &
name,
388 const std::string &
info,
bool optional ) :
392template <
typename NumberType>
396template <
typename NumberType>
398recognizeName(
const std::string &n )
404 this->
_value = NumberType( 1 );
412template <
typename NumberType>
430template <
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 );
549template <
typename T>
inline
551 const std::string &info,
bool optional )
557template <
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)
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)
friend class OptionsParser
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
friend class OptionsParser
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
virtual bool feed(const std::string &)
SingleOption(BoolOrNumber< NumberType > &value, const std::string &name, const std::string &info, bool optional)
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)
user_interruption(const std::string &message)
void stringTo(const std::string &value, T &result)