34 #define mytypeid(T) abi::__cxa_demangle(typeid(T).name(),0,0,&status) 36 #define mytypeid(T) typeid(T).name() 38 #include "ogr_feature.h" 53 template<
typename T>
inline T string2type(std::string
const& s){
54 std::istringstream i(s);
62 template<
typename T>
inline T string2type(std::string
const& s,
bool failIfLeftoverChars){
63 std::istringstream i(s);
66 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
72 template<
typename T>
inline std::string type2string(T
const& value){
73 std::ostringstream oss;
106 template<
class T>
class Optionpk :
public std::vector <T>
112 Optionpk(
const std::string& shortName,
const std::string& longName,
const std::string& helpInfo);
114 Optionpk(
const std::string& shortName,
const std::string& longName,
const std::string& helpInfo,
const T& defaultValue,
short hide=0);
118 void setHelp(
const std::string& helpInfo){m_help=helpInfo;};
122 bool retrieveOption(
int argc,
const std::vector<std::string>& argv);
124 bool retrieveOption(
int argc,
char ** argv);
126 template<
class T1>
friend std::ostream& operator<<(std::ostream & os, const Optionpk<T1>& theOption);
128 void setAll(
const std::string& shortName,
const std::string& longName,
const std::string& helpInfo);
130 void setAll(
const std::string& shortName,
const std::string& longName,
const std::string& helpInfo,
const T& defaultValue,
short hide);
132 void setDefault(
const T& defaultValue);
133 std::string getDefaultValue()
const {
return m_defaultValue;};
135 void setShortName(
const std::string& shortName);
137 void setLongName(
const std::string& longName);
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\ 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\ 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");};
162 typename std::vector<T>::const_iterator
findSubstring(
const T& argument)
const {std::string errorString=
"Error: findSubstring only defined for options of type std::string";
throw(errorString);};
166 bool hasArgument()
const {
return m_hasArgument;};
168 bool hasShortOption()
const {
return m_shortName.compare(
"\0");};
170 bool hasLongOption()
const {
return m_longName.compare(
"\0");};
172 std::string usage()
const;
174 std::string usageDoxygen()
const;
176 std::string m_shortName;
178 std::string m_longName;
191 template<
class T1> std::ostream& operator<<(std::ostream& os, const Optionpk<T1>& theOption)
193 os << theOption.getLongName() <<
": ";
194 for(
int index=0;index<theOption.size();++index)
195 os << type2string<T1>(theOption[index]) <<
" ";
201 : m_hasDefault(false)
211 template<
class T>
inline Optionpk<T>::Optionpk(
const std::string& shortName,
const std::string& longName,
const std::string& helpInfo)
212 : m_hasDefault(false)
214 setAll(shortName,longName,helpInfo);
227 template<
class T>
inline Optionpk<T>::Optionpk(
const std::string& shortName,
const std::string& longName,
const std::string& helpInfo,
const T& defaultValue,
short hide)
229 setAll(shortName,longName,helpInfo,defaultValue, hide);
234 std::ostringstream helpss;
235 std::string shortOption=m_shortName;
236 std::string longOption=m_longName;
237 shortOption.insert(0,
"-");
238 longOption.insert(0,
"--");
239 if(hasShortOption()){
240 helpss <<
" " << std::setiosflags(std::ios::left) << std::setw(6) << shortOption;
243 helpss <<
" " << std::setiosflags(std::ios::left) << std::setw(6) <<
" ";
246 helpss <<
" " << std::setiosflags(std::ios::left) << std::setw(20) << longOption;
249 helpss <<
" " << std::setiosflags(std::ios::left) << std::setw(20) <<
" ";
251 helpss <<
" " << m_help;
253 helpss <<
" (default: " << type2string<T>(m_defaultValue) <<
")";
259 std::ostringstream helpss;
260 std::string shortOption=m_shortName;
261 std::string longOption=m_longName;
264 helpss <<
" | " << std::setiosflags(std::ios::left) << std::setw(6) << shortOption <<
" | ";
266 helpss <<
" | " << std::setiosflags(std::ios::left) <<
" | ";
268 helpss << std::setiosflags(std::ios::left) << std::setw(20) << longOption <<
" | ";
270 helpss << std::setiosflags(std::ios::left) <<
" | ";
272 helpss << std::setiosflags(std::ios::left) << std::setw(4) << mytypeid(T) <<
" | ";
275 helpss <<std::setiosflags(std::ios::left) << std::setw(5) << type2string<T>(m_defaultValue) <<
" |";
278 helpss << m_help <<
" | ";
283 template<
class T>
inline void Optionpk<T>::setAll(
const std::string& shortName,
const std::string& longName,
const std::string& helpInfo)
285 m_shortName=shortName;
292 template<
class T>
inline void Optionpk<T>::setAll(
const std::string& shortName,
const std::string& longName,
const std::string& helpInfo,
const T& defaultValue,
short hide)
294 m_shortName=shortName;
298 m_defaultValue=defaultValue;
313 std::string helpStringShort=
"-h";
314 std::string helpStringLong=
"--help";
315 std::string helpStringDoxygen=
"--doxygen";
316 std::string versionString=
"--version";
317 std::string licenseString=
"--license";
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);
330 if(!helpStringShort.compare(currentOption)){
332 std::cout << usage() << std::endl;
335 else if(!helpStringLong.compare(currentOption)){
337 std::cout << usage() << std::endl;
340 else if(!helpStringDoxygen.compare(currentOption)){
342 std::cout << usageDoxygen() << std::endl;
345 else if(!versionString.compare(currentOption)){
346 std::string 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.";
354 else if(!licenseString.compare(currentOption)){
357 if(hasShortOption()&&!(shortOption.compare(currentOption))){
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]));
363 this->push_back(string2type<T>(
"1"));
365 else if(hasLongOption()&&!(longOption.compare(currentOption))){
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]));
371 this->push_back(string2type<T>(
"1"));
374 if(!(this->size())&&m_hasDefault)
375 this->push_back(m_defaultValue);
384 std::string helpStringShort=
"-h";
385 std::string helpStringLong=
"--help";
386 std::string helpStringDoxygen=
"--doxygen";
387 std::string versionString=
"--version";
388 std::string licenseString=
"--license";
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);
401 if(!helpStringShort.compare(currentOption)){
403 std::cout << usage() << std::endl;
406 else if(!helpStringLong.compare(currentOption)){
408 std::cout << usage() << std::endl;
411 else if(!helpStringDoxygen.compare(currentOption)){
413 std::cout << usageDoxygen() << std::endl;
416 else if(!versionString.compare(currentOption)){
417 std::string 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.";
425 else if(!licenseString.compare(currentOption)){
428 if(hasShortOption()&&!(shortOption.compare(currentOption))){
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]));
434 this->push_back(string2type<T>(
"1"));
436 else if(hasLongOption()&&!(longOption.compare(currentOption))){
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]));
442 this->push_back(string2type<T>(
"1"));
445 if(!(this->size())&&m_hasDefault)
446 this->push_back(m_defaultValue);
456 template<>
inline std::string string2type(std::string
const& s){
460 template<>
inline double string2type(std::string
const& s){
461 std::istringstream i;
465 if (!(i >> std::setprecision(12) >> x) )
470 template<>
inline float string2type(std::string
const& s){
471 std::istringstream i;
475 if (!(i >> std::setprecision(12) >> x) )
481 template<>
inline OGRFieldType string2type(std::string
const& s){
483 int ogr_typecount=11;
484 for(
int iType = 0; iType < ogr_typecount; ++iType){
485 if( OGRFieldDefn::GetFieldTypeName((OGRFieldType)iType) != NULL
486 && EQUAL(OGRFieldDefn::GetFieldTypeName((OGRFieldType)iType),s.c_str()))
487 ftype=(OGRFieldType) iType;
493 template<>
inline std::string type2string(
bool const& value){
501 template<>
inline std::string type2string(std::string
const& value){
509 template<>
inline std::string type2string(
float const& value){
510 std::ostringstream oss;
518 template<>
inline std::string type2string(
double const& value){
519 std::ostringstream oss;
527 template<>
inline void Optionpk<bool>::setAll(
const std::string& shortName,
const std::string& longName,
const std::string& helpInfo)
529 m_shortName=shortName;
537 template<>
inline void Optionpk<bool>::setAll(
const std::string& shortName,
const std::string& longName,
const std::string& helpInfo,
const bool& defaultValue,
short hide)
539 m_shortName=shortName;
543 m_defaultValue=defaultValue;
551 setAll(shortName,longName,helpInfo);
555 template<>
inline Optionpk<bool>::Optionpk(
const std::string& shortName,
const std::string& longName,
const std::string& helpInfo,
const bool& defaultValue,
short hide)
557 setAll(shortName,longName,helpInfo,defaultValue, hide);
563 std::vector<std::string>::const_iterator opit=this->begin();
564 while(opit!=this->end()){
565 if(opit->find(argument)!=std::string::npos)
throw this class when syntax error in command line option
std::string getLongName() const
get the long name to be used as –longName
std::string getHelp() const
get help info stored in m_help
Optionpk()
default constructor
~Optionpk()
default destructor
std::string getShortName() const
get the short name to be used as -shortName
void setHelp(const std::string &helpInfo)
set help information
static std::string getGPLv3License()
get license info
std::vector< T >::const_iterator findSubstring(const T &argument) const
void setAll(const std::string &shortName, const std::string &longName, const std::string &helpInfo)
set all attributes of the option, except default and hide
void setHide(short hide)
hide option from short help -h (1) or make invisible to short and long help –help (2) ...
bool retrieveOption(int argc, const std::vector< std::string > &argv)
read option from command line (use for all options!), std::string implementation