pktools  2.6.3
Processing Kernel for geospatial data
Public Member Functions | Static Public Member Functions | Friends | List of all members
Optionpk< T > Class Template Reference

#include <Optionpk.h>

Inheritance diagram for Optionpk< T >:
Inheritance graph
[legend]
Collaboration diagram for Optionpk< T >:
Collaboration graph
[legend]

Public Member Functions

 Optionpk ()
 default constructor
 
 Optionpk (const std::string &shortName, const std::string &longName, const std::string &helpInfo)
 constructor for option without default value More...
 
 Optionpk (const std::string &shortName, const std::string &longName, const std::string &helpInfo, const T &defaultValue, short hide=0)
 constructor for option with default value. Option can be hidden for help info More...
 
 ~Optionpk ()
 default destructor
 
void setHelp (const std::string &helpInfo)
 set help information
 
void setHide (short hide)
 
bool retrieveOption (int argc, char **argv)
 
void setAll (const std::string &shortName, const std::string &longName, const std::string &helpInfo)
 
void setAll (const std::string &shortName, const std::string &longName, const std::string &helpInfo, const T &defaultValue, short hide)
 
void setDefault (const T &defaultValue)
 
std::string getDefaultValue () const
 
void setShortName (const std::string &shortName)
 
void setLongName (const std::string &longName)
 
std::string getShortName () const
 
std::string getLongName () const
 
std::string getHelp () const
 
std::vector< T >::const_iterator findSubstring (const T &argument) const
 
template<>
void setAll (const std::string &shortName, const std::string &longName, const std::string &helpInfo)
 
template<>
void setAll (const std::string &shortName, const std::string &longName, const std::string &helpInfo, const bool &defaultValue, short hide)
 
template<>
 Optionpk (const std::string &shortName, const std::string &longName, const std::string &helpInfo)
 
template<>
 Optionpk (const std::string &shortName, const std::string &longName, const std::string &helpInfo, const bool &defaultValue, short hide)
 
template<>
std::vector< std::string >
::const_iterator 
findSubstring (const std::string &argument) const
 
template<>
void setAll (const std::string &shortName, const std::string &longName, const std::string &helpInfo)
 
template<>
void setAll (const std::string &shortName, const std::string &longName, const std::string &helpInfo)
 
template<>
void setAll (const std::string &shortName, const std::string &longName, const std::string &helpInfo, const bool &defaultValue, short hide)
 
template<>
void setAll (const std::string &shortName, const std::string &longName, const std::string &helpInfo, const bool &defaultValue, short hide)
 
template<>
 Optionpk (const std::string &shortName, const std::string &longName, const std::string &helpInfo, const bool &defaultValue, short hide)
 
template<>
std::vector< std::string >
::const_iterator 
findSubstring (const std::string &argument) const
 

Static Public Member Functions

static std::string getGPLv3License ()
 

Friends

template<class T1 >
std::ostream & operator<< (std::ostream &os, const Optionpk< T1 > &theOption)
 

Detailed Description

template<class T>
class Optionpk< T >

Class to implement command line options. With the constructor you can define an option, in both short - and long -- format, of a specific type, help information and a default value.
This class inherits from std::vector, so the option variable is a vector, supporting multiple inputs for the same option (e.g., –input file1 [–input file2 ...]. Several command line option formats are supported:

Option names should have regular characters and no white space in them. Some names are reserved and can not be used either:

A call to member function retrieveOption reads the command line arguments and initializes the object (vector). Make sure to call this member function before using the option object in your main program (or a segmentation error due to an un-initialized vector will occur).

All calls to retrieveOption should reside in a try{} block. If one of the reserved options

Similarly, if help is invoked with the short option -h or long option --help, the main program is informed by the return value false of retrieveOption (for any option). An example how to use Optionpk is shown in pktestOption.cc

Definition at line 106 of file Optionpk.h.

Constructor & Destructor Documentation

template<class T >
Optionpk< T >::Optionpk ( const std::string &  shortName,
const std::string &  longName,
const std::string &  helpInfo 
)
inline

constructor for option without default value

constructor without default value
shortName is option invoked with -
longName is option invoked with --
helpInfo is the help message that is shown when option -h or –help is invoked

Definition at line 187 of file Optionpk.h.

188 : m_hasDefault(false)
189 {
190  setAll(shortName,longName,helpInfo);
191 }
template<class T >
Optionpk< T >::Optionpk ( const std::string &  shortName,
const std::string &  longName,
const std::string &  helpInfo,
const T &  defaultValue,
short  hide = 0 
)
inline

constructor for option with default value. Option can be hidden for help info

constructor with default value.
shortName is option invoked with -
longName is option invoked with --
helpInfo is the help message that is shown when option -h or –help is invoked
defaultValue is default value of the option (first value of vector: option[0])
hide=0 : option is visible for in both short (-h). Typical use: mandatory options
hide=1 : option is only visible in long help (--help). Typical use: expert options
hide=2 : option is hidden for user. Typical use: Easter eggs or options only known to author

Definition at line 203 of file Optionpk.h.

204 {
205  setAll(shortName,longName,helpInfo,defaultValue, hide);
206 }

Member Function Documentation

template<class T >
bool Optionpk< T >::retrieveOption ( int  argc,
char **  argv 
)
inline

make sure to call this function first before using the option in main program (or segmentation fault will occur...)

Definition at line 283 of file Optionpk.h.

283  {
284  bool noHelp=true;//return value, alert main program that hard coded option (help, version, license, doxygen) was invoked
285  std::string helpStringShort="-h";//short option for help (hard coded)
286  std::string helpStringLong="--help";//long option for help (hard coded)
287  std::string helpStringDoxygen="--doxygen";//option to create table of options ready to use for doxygen
288  std::string versionString="--version";//option to show current version
289  std::string licenseString="--license";//option to show current version
290  for(int i = 1; i < argc; ++i ){
291  std::string currentArgument;
292  std::string currentOption=argv[i];
293  std::string shortOption=m_shortName;
294  std::string longOption=m_longName;
295  shortOption.insert(0,"-");
296  longOption.insert(0,"--");
297  size_t foundEqual=currentOption.rfind("=");
298  if(foundEqual!=std::string::npos){
299  currentArgument=currentOption.substr(foundEqual+1);
300  currentOption=currentOption.substr(0,foundEqual);
301  }
302  if(!helpStringShort.compare(currentOption)){
303  if(m_hide<1)
304  std::cout << usage() << std::endl;
305  noHelp=false;
306  }
307  else if(!helpStringLong.compare(currentOption)){
308  if(m_hide<2)
309  std::cout << usage() << std::endl;
310  noHelp=false;
311  }
312  else if(!helpStringDoxygen.compare(currentOption)){
313  if(m_hide<2)
314  std::cout << usageDoxygen() << std::endl;
315  noHelp=false;
316  }
317  else if(!versionString.compare(currentOption)){
318  std::string theVersion="version ";
319  theVersion+=VERSION;
320  theVersion+=", Copyright (C) Pieter Kempeneers.\n\
321  This program comes with ABSOLUTELY NO WARRANTY; for details type use option -h.\n \
322  This is free software, and you are welcome to redistribute it\n \
323  under certain conditions; use option --license for details.";
324  throw(theVersion);//no need to continue registering (break prevents from multiplication of version info)
325  }
326  else if(!licenseString.compare(currentOption)){
327  throw(getGPLv3License());
328  }
329  if(hasShortOption()&&!(shortOption.compare(currentOption))){//for -option
330  if(foundEqual!=std::string::npos)
331  this->push_back(string2type<T>(currentArgument));
332  else if(m_hasArgument && i < argc-1)
333  this->push_back(string2type<T>(argv[++i]));
334  else
335  this->push_back(string2type<T>("1"));
336  }
337  else if(hasLongOption()&&!(longOption.compare(currentOption))){//for --option
338  if(foundEqual!=std::string::npos)
339  this->push_back(string2type<T>(currentArgument));
340  else if(m_hasArgument && i < argc-1)
341  this->push_back(string2type<T>(argv[++i]));
342  else
343  this->push_back(string2type<T>("1"));
344  }
345  }
346  if(!(this->size())&&m_hasDefault)//only set default value if no options were given
347  this->push_back(m_defaultValue);
348  return(noHelp);
349 }

The documentation for this class was generated from the following file: