| pktools 2.6.7 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 >:
[legend]
Collaboration diagram for Optionpk< T >:
[legend]

## Public Member Functions
| Optionpk ()
| default constructor More...
| 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 More...
void | setHelp (const std::string &helpInfo)
| set help information More...
void | setHide (short hide)
| hide option from short help -h (1) or make invisible to short and long help –help (2) More...
bool | retrieveOption (int argc, const std::vector< std::string > &argv)
| read option from command line (use for all options!), std::string implementation More...
bool | retrieveOption (int argc, char **argv)
| read option from command line (use for all options!) More...
void | setAll (const std::string &shortName, const std::string &longName, const std::string &helpInfo)
| set all attributes of the option, except default and hide More...
void | setAll (const std::string &shortName, const std::string &longName, const std::string &helpInfo, const T &defaultValue, short hide)
| set all attributes of the option More...
void | setDefault (const T &defaultValue)
| set a default value for the option
std::string | getDefaultValue () const
void | setShortName (const std::string &shortName)
| set the short name to be used as -shortName
void | setLongName (const std::string &longName)
| set the long name to be used as –longName
std::string | getShortName () const
| get the short name to be used as -shortName More...
std::string | getLongName () const
| get the long name to be used as –longName More...
std::string | getHelp () const
| get help info stored in m_help More...
std::vector< T >::const_iterator | findSubstring (const T &argument) const
void | setAll (const std::string &shortName, const std::string &longName, const std::string &helpInfo)
| specialization for bool More...
void | setAll (const std::string &shortName, const std::string &longName, const std::string &helpInfo, const bool &defaultValue, short hide)
| specialization for bool More...
| Optionpk (const std::string &shortName, const std::string &longName, const std::string &helpInfo)
| specialization for bool More...
| Optionpk (const std::string &shortName, const std::string &longName, const std::string &helpInfo, const bool &defaultValue, short hide)
| specialization for bool More...
std::vector< std::string >::const_iterator | findSubstring (const std::string &argument) const

## Static Public Member Functions
static std::string | getGPLv3License ()
| get license info More...

## Friends
template<class T1 >
std::ostream & | operator<< (std::ostream &os, const Optionpk< T1 > &theOption)
| print values for this option More...

## 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:

- `-shortOption value`
- `-shortOption=value`
- `--longOption value`
- `--longOption=value`
- `-shortOption` (no value for boolean options, which are automatically set by invoking the option)
- `--longOption` (no value for boolean options, which are automatically set by invoking the option)

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

- short option `h` or long option `help`: shows usage
- long option `help` shows long help info
- long option `license`: shows license info
- long option `version`: shows current version of pktools
- long option `doxygen`: shows help info in table format, ready to be included in doxygen

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

- `license`
- `version` is used, an exception of type std::string is thrown. This can be caught with a catch(string predefinedString) right after the try block, where the message can be sent to stdout and the program can be ended.

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

## ◆ Optionpk() [1/5]

template<class T >

Optionpk< T >::Optionpk
| inline

default constructor

Definition at line 200 of file Optionpk.h.

201: m_hasDefault(false)
202{
203}

## ◆ Optionpk() [2/5]

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 211 of file Optionpk.h.

212: m_hasDefault(false)
213{
214 setAll(shortName,longName,helpInfo);
215}
Optionpk::setAll
void setAll(const std::string &shortName, const std::string &longName, const std::string &helpInfo)
set all attributes of the option, except default and hide
Definition: Optionpk.h:283

## ◆ Optionpk() [3/5]

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 227 of file Optionpk.h.

228{
229 setAll(shortName,longName,helpInfo,defaultValue, hide);
230}

## ◆ ~Optionpk()

template<class T >

Optionpk< T >::~Optionpk
| inline

default destructor

Definition at line 304 of file Optionpk.h.

305{
306}

## ◆ Optionpk() [4/5]

Optionpk< bool >::Optionpk | ( | const std::string & | shortName,
|  | const std::string & | longName,
|  | const std::string & | helpInfo
| ) |  |
| inline

specialization for bool

Definition at line 549 of file Optionpk.h.

550{
551 setAll(shortName,longName,helpInfo);
552}

## ◆ Optionpk() [5/5]

Optionpk< bool >::Optionpk | ( | const std::string & | shortName,
|  | const std::string & | longName,
|  | const std::string & | helpInfo,
|  | const bool & | defaultValue,
|  | short | hide
| ) |  |
| inline

specialization for bool

Definition at line 555 of file Optionpk.h.

556{
557 setAll(shortName,longName,helpInfo,defaultValue, hide);
558}

## Member Function Documentation

## ◆ findSubstring() [1/2]

std::vector< std::string >::const_iterator Optionpk< std::string >::findSubstring | ( | const std::string & | argument | ) | const
| inline

Definition at line 562 of file Optionpk.h.

562 {
563 std::vector<std::string>::const_iterator opit=this->begin();
564 while(opit!=this->end()){
565 if(opit->find(argument)!=std::string::npos)
566 break;
567 ++opit;
568 }
569 return opit;
570}

## ◆ findSubstring() [2/2]

template<class T >

std::vector< T >::const_iterator Optionpk< T >::findSubstring | ( | const T & | argument | ) | const
| inline

find substring in options of type string (e.g., -co INTERLEAVE=BAND) this template function only makes sense for T=std::string (implemented via a specialization)

Definition at line 162 of file Optionpk.h.

162{std::string errorString="Error: findSubstring only defined for options of type std::string"; throw(errorString);};

## ◆ getDefaultValue()

template<class T >

std::string Optionpk< T >::getDefaultValue | ( |  | ) | const
| inline

Definition at line 133 of file Optionpk.h.

133{return m_defaultValue;};

## ◆ getGPLv3License()

template<class T >

static std::string Optionpk< T >::getGPLv3License | ( |  | ) |
| inline static

get license info

Definition at line 145 of file Optionpk.h.

145 {
146 return static_cast<std::string>("\n\
147 This program is free software: you can redistribute it and/or modify\n\
148 it under the terms of the GNU General Public License as published by\n\
149 the Free Software Foundation, either version 3 of the License, or\n\
150 (at your option) any later version.\n\
151 \n\
152 This program is distributed in the hope that it will be useful,\n\
153 but WITHOUT ANY WARRANTY; without even the implied warranty of\n\
154 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n\
155 GNU General Public License for more details.\n\
156 \n\
157 You should have received a copy of the GNU General Public License\n\
158 along with this program. If not, see <http://www.gnu.org/licenses/>.\n");};

## ◆ getHelp()

template<class T >

std::string Optionpk< T >::getHelp | ( |  | ) | const
| inline

get help info stored in m_help

Definition at line 143 of file Optionpk.h.

143{return m_help;};

## ◆ getLongName()

template<class T >

std::string Optionpk< T >::getLongName | ( |  | ) | const
| inline

get the long name to be used as –longName

Definition at line 141 of file Optionpk.h.

141{return m_longName;};

## ◆ getShortName()

template<class T >

std::string Optionpk< T >::getShortName | ( |  | ) | const
| inline

get the short name to be used as -shortName

Definition at line 139 of file Optionpk.h.

139{return m_shortName;};

## ◆ retrieveOption() [1/2]

template<class T >

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

read option from command line (use for all options!)

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

Definition at line 382 of file Optionpk.h.

382 {
383 bool noHelp=true;//return value, alert main program that hard coded option (help, version, license, doxygen) was invoked
384 std::string helpStringShort="-h";//short option for help (hard coded)
385 std::string helpStringLong="--help";//long option for help (hard coded)
386 std::string helpStringDoxygen="--doxygen";//option to create table of options ready to use for doxygen
387 std::string versionString="--version";//option to show current version
388 std::string licenseString="--license";//option to show current version
389 for(int i = 1; i < argc; ++i ){
390 std::string currentArgument;
391 std::string currentOption=argv[i];
392 std::string shortOption=m_shortName;
393 std::string longOption=m_longName;
394 shortOption.insert(0,"-");
395 longOption.insert(0,"--");
396 size_t foundEqual=currentOption.rfind("=");
397 if(foundEqual!=std::string::npos){
398 currentArgument=currentOption.substr(foundEqual+1);
399 currentOption=currentOption.substr(0,foundEqual);
400 }
401 if(!helpStringShort.compare(currentOption)){
402 if(m_hide<1)
403 std::cout << usage() << std::endl;
404 noHelp=false;
405 }
406 else if(!helpStringLong.compare(currentOption)){
407 if(m_hide<2)
408 std::cout << usage() << std::endl;
409 noHelp=false;
410 }
411 else if(!helpStringDoxygen.compare(currentOption)){
412 if(m_hide<2)
413 std::cout << usageDoxygen() << std::endl;
414 noHelp=false;
415 }
416 else if(!versionString.compare(currentOption)){
417 std::string theVersion="version ";
418 theVersion+=VERSION;
419 theVersion+=", Copyright (C) Pieter Kempeneers.\n\
420 This program comes with ABSOLUTELY NO WARRANTY; for details type use option -h.\n \
421 This is free software, and you are welcome to redistribute it\n \
422 under certain conditions; use option --license for details.";
423 throw(theVersion);//no need to continue registering (break prevents from multiplication of version info)
424 }
425 else if(!licenseString.compare(currentOption)){
426 throw(getGPLv3License());
427 }
428 if(hasShortOption()&&!(shortOption.compare(currentOption))){//for -option
429 if(foundEqual!=std::string::npos)
430 this->push_back(string2type<T>(currentArgument));
431 else if(m_hasArgument && i < argc-1)
432 this->push_back(string2type<T>(argv[++i]));
433 else
434 this->push_back(string2type<T>("1"));
435 }
436 else if(hasLongOption()&&!(longOption.compare(currentOption))){//for --option
437 if(foundEqual!=std::string::npos)
438 this->push_back(string2type<T>(currentArgument));
439 else if(m_hasArgument && i < argc-1)
440 this->push_back(string2type<T>(argv[++i]));
441 else
442 this->push_back(string2type<T>("1"));
443 }
444 }
445 if(!(this->size())&&m_hasDefault)//only set default value if no options were given
446 this->push_back(m_defaultValue);
447 return(noHelp);
448}
Optionpk::getGPLv3License
static std::string getGPLv3License()
get license info
Definition: Optionpk.h:145

## ◆ retrieveOption() [2/2]

template<class T >

bool Optionpk< T >::retrieveOption | ( | int | argc,
|  | const std::vector< std::string > & | argv
| ) |  |
| inline

read option from command line (use for all options!), std::string implementation

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

Definition at line 311 of file Optionpk.h.

311 {
312 bool noHelp=true;//return value, alert main program that hard coded option (help, version, license, doxygen) was invoked
313 std::string helpStringShort="-h";//short option for help (hard coded)
314 std::string helpStringLong="--help";//long option for help (hard coded)
315 std::string helpStringDoxygen="--doxygen";//option to create table of options ready to use for doxygen
316 std::string versionString="--version";//option to show current version
317 std::string licenseString="--license";//option to show current version
318 for(int i = 1; i < argc; ++i ){
319 std::string currentArgument;
320 std::string currentOption=argv[i];
321 std::string shortOption=m_shortName;
322 std::string longOption=m_longName;
323 shortOption.insert(0,"-");
324 longOption.insert(0,"--");
325 size_t foundEqual=currentOption.rfind("=");
326 if(foundEqual!=std::string::npos){
327 currentArgument=currentOption.substr(foundEqual+1);
328 currentOption=currentOption.substr(0,foundEqual);
329 }
330 if(!helpStringShort.compare(currentOption)){
331 if(m_hide<1)
332 std::cout << usage() << std::endl;
333 noHelp=false;
334 }
335 else if(!helpStringLong.compare(currentOption)){
336 if(m_hide<2)
337 std::cout << usage() << std::endl;
338 noHelp=false;
339 }
340 else if(!helpStringDoxygen.compare(currentOption)){
341 if(m_hide<2)
342 std::cout << usageDoxygen() << std::endl;
343 noHelp=false;
344 }
345 else if(!versionString.compare(currentOption)){
346 std::string theVersion="version ";
347 theVersion+=VERSION;
348 theVersion+=", Copyright (C) Pieter Kempeneers.\n\
349 This program comes with ABSOLUTELY NO WARRANTY; for details type use option -h.\n \
350 This is free software, and you are welcome to redistribute it\n \
351 under certain conditions; use option --license for details.";
352 throw(theVersion);//no need to continue registering (break prevents from multiplication of version info)
353 }
354 else if(!licenseString.compare(currentOption)){
355 throw(getGPLv3License());
356 }
357 if(hasShortOption()&&!(shortOption.compare(currentOption))){//for -option
358 if(foundEqual!=std::string::npos)
359 this->push_back(string2type<T>(currentArgument));
360 else if(m_hasArgument && i < argc-1)
361 this->push_back(string2type<T>(argv[++i]));
362 else
363 this->push_back(string2type<T>("1"));
364 }
365 else if(hasLongOption()&&!(longOption.compare(currentOption))){//for --option
366 if(foundEqual!=std::string::npos)
367 this->push_back(string2type<T>(currentArgument));
368 else if(m_hasArgument && i < argc-1)
369 this->push_back(string2type<T>(argv[++i]));
370 else
371 this->push_back(string2type<T>("1"));
372 }
373 }
374 if(!(this->size())&&m_hasDefault)//only set default value if no options were given
375 this->push_back(m_defaultValue);
376 return(noHelp);
377}

## ◆ setAll() [1/4]

template<class T >

void Optionpk< T >::setAll | ( | const std::string & | shortName,
|  | const std::string & | longName,
|  | const std::string & | helpInfo
| ) |  |
| inline

set all attributes of the option, except default and hide

Definition at line 283 of file Optionpk.h.

284{
285 m_shortName=shortName;
286 m_longName=longName;
287 m_hasArgument=true;
288 m_help=helpInfo;
289 m_hide=0;
290}

## ◆ setAll() [2/4]

void Optionpk< bool >::setAll | ( | const std::string & | shortName,
|  | const std::string & | longName,
|  | const std::string & | helpInfo
| ) |  |
| inline

specialization for bool

Definition at line 527 of file Optionpk.h.

528{
529 m_shortName=shortName;
530 m_longName=longName;
531 m_hasArgument=false;
532 m_help=helpInfo;
533 m_hide=0;
534}

## ◆ setAll() [3/4]

void Optionpk< bool >::setAll | ( | const std::string & | shortName,
|  | const std::string & | longName,
|  | const std::string & | helpInfo,
|  | const bool & | defaultValue,
|  | short | hide
| ) |  |
| inline

specialization for bool

Definition at line 537 of file Optionpk.h.

538{
539 m_shortName=shortName;
540 m_longName=longName;
541 m_hasArgument=false;
542 m_help=helpInfo;
543 m_defaultValue=defaultValue;
544 m_hasDefault=true;
545 m_hide=hide;
546}

## ◆ setAll() [4/4]

template<class T >

void Optionpk< T >::setAll | ( | const std::string & | shortName,
|  | const std::string & | longName,
|  | const std::string & | helpInfo,
|  | const T & | defaultValue,
|  | short | hide
| ) |  |
| inline

set all attributes of the option

Definition at line 292 of file Optionpk.h.

293{
294 m_shortName=shortName;
295 m_longName=longName;
296 m_hasArgument=true;
297 m_help=helpInfo;
298 m_defaultValue=defaultValue;
299 m_hasDefault=true;
300 m_hide=hide;
301}

## ◆ setHelp()

template<class T >

void Optionpk< T >::setHelp | ( | const std::string & | helpInfo | ) |
| inline

set help information

Definition at line 118 of file Optionpk.h.

118{m_help=helpInfo;};

## ◆ setHide()

template<class T >

void Optionpk< T >::setHide | ( | short | hide | ) |
| inline

hide option from short help -h (1) or make invisible to short and long help –help (2)

Definition at line 120 of file Optionpk.h.

120{m_hide=hide;};

## Friends And Related Function Documentation

## ◆ operator<<

template<class T >
template<class T1 >

std::ostream & operator<< | ( | std::ostream & | os,
|  | const Optionpk< T1 > & | theOption
| ) |  |
| friend

print values for this option

Definition at line 191 of file Optionpk.h.

192{
193 os << theOption.getLongName() << ": ";
194 for(int index=0;index<theOption.size();++index)
195 os << type2string<T1>(theOption[index]) << " ";
196 os << std::endl;
197 return os;
198}
Optionpk::getLongName
std::string getLongName() const
get the long name to be used as –longName
Definition: Optionpk.h:141

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

- /tmp/slackdce/slackrepo.NvCr9x/build_pktools/PKTOOLS-2.6.7.6/src/base/Optionpk.h