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;};
124 template<
class T1>
friend std::ostream& operator<<(std::ostream & os, const Optionpk<T1>& theOption);
126 void setAll(
const std::string& shortName,
const std::string& longName,
const std::string& helpInfo);
128 void setAll(
const std::string& shortName,
const std::string& longName,
const std::string& helpInfo,
const T& defaultValue,
short hide);
131 std::string getDefaultValue()
const {
return m_defaultValue;};
144 return static_cast<std::string
>(
"\n\
145 This program is free software: you can redistribute it and/or modify\n\
146 it under the terms of the GNU General Public License as published by\n\
147 the Free Software Foundation, either version 3 of the License, or\n\
148 (at your option) any later version.\n\
150 This program is distributed in the hope that it will be useful,\n\
151 but WITHOUT ANY WARRANTY; without even the implied warranty of\n\
152 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n\
153 GNU General Public License for more details.\n\
155 You should have received a copy of the GNU General Public License\n\
156 along with this program. If not, see <http://www.gnu.org/licenses/>.\n");};
160 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);};
164 bool hasArgument()
const {
return m_hasArgument;};
166 bool hasShortOption()
const {
return m_shortName.compare(
"\0");};
168 bool hasLongOption()
const {
return m_longName.compare(
"\0");};
170 std::string usage()
const;
172 std::string usageDoxygen()
const;
174 std::string m_shortName;
176 std::string m_longName;
189 template<
class T1> std::ostream& operator<<(std::ostream& os, const Optionpk<T1>& theOption)
191 os << theOption.getLongName() <<
": ";
192 for(
int index=0;index<theOption.size();++index)
193 os << type2string<T1>(theOption[index]) <<
" ";
199 : m_hasDefault(false)
209 template<
class T>
inline Optionpk<T>::Optionpk(
const std::string& shortName,
const std::string& longName,
const std::string& helpInfo)
210 : m_hasDefault(false)
212 setAll(shortName,longName,helpInfo);
225 template<
class T>
inline Optionpk<T>::Optionpk(
const std::string& shortName,
const std::string& longName,
const std::string& helpInfo,
const T& defaultValue,
short hide)
227 setAll(shortName,longName,helpInfo,defaultValue, hide);
232 std::ostringstream helpss;
233 std::string shortOption=m_shortName;
234 std::string longOption=m_longName;
235 shortOption.insert(0,
"-");
236 longOption.insert(0,
"--");
238 helpss <<
" " << std::setiosflags(std::ios::left) << std::setw(4) << shortOption;
240 helpss <<
" " << std::setiosflags(std::ios::left) << std::setw(4) <<
" ";
242 helpss <<
" " << std::setiosflags(std::ios::left) << std::setw(20) << longOption;
244 helpss <<
" " << std::setiosflags(std::ios::left) << std::setw(20) <<
" ";
245 helpss <<
" " << m_help;
247 helpss <<
" (default: " << type2string<T>(m_defaultValue) <<
")";
253 std::ostringstream helpss;
254 std::string shortOption=m_shortName;
255 std::string longOption=m_longName;
258 helpss <<
" | " << std::setiosflags(std::ios::left) << std::setw(6) << shortOption <<
" | ";
260 helpss <<
" | " << std::setiosflags(std::ios::left) <<
" | ";
262 helpss << std::setiosflags(std::ios::left) << std::setw(20) << longOption <<
" | ";
264 helpss << std::setiosflags(std::ios::left) <<
" | ";
266 helpss << std::setiosflags(std::ios::left) << std::setw(4) << mytypeid(T) <<
" | ";
269 helpss <<std::setiosflags(std::ios::left) << std::setw(5) << type2string<T>(m_defaultValue) <<
" |";
272 helpss << m_help <<
" | ";
277 template<
class T>
inline void Optionpk<T>::setAll(
const std::string& shortName,
const std::string& longName,
const std::string& helpInfo)
279 m_shortName=shortName;
286 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)
288 m_shortName=shortName;
292 m_defaultValue=defaultValue;
307 std::string helpStringShort=
"-h";
308 std::string helpStringLong=
"--help";
309 std::string helpStringDoxygen=
"--doxygen";
310 std::string versionString=
"--version";
311 std::string licenseString=
"--license";
312 for(
int i = 1; i < argc; ++i ){
313 std::string currentArgument;
314 std::string currentOption=argv[i];
315 std::string shortOption=m_shortName;
316 std::string longOption=m_longName;
317 shortOption.insert(0,
"-");
318 longOption.insert(0,
"--");
319 size_t foundEqual=currentOption.rfind(
"=");
320 if(foundEqual!=std::string::npos){
321 currentArgument=currentOption.substr(foundEqual+1);
322 currentOption=currentOption.substr(0,foundEqual);
324 if(!helpStringShort.compare(currentOption)){
326 std::cout << usage() << std::endl;
329 else if(!helpStringLong.compare(currentOption)){
331 std::cout << usage() << std::endl;
334 else if(!helpStringDoxygen.compare(currentOption)){
336 std::cout << usageDoxygen() << std::endl;
339 else if(!versionString.compare(currentOption)){
340 std::string theVersion=
"version ";
342 theVersion+=
", Copyright (C) Pieter Kempeneers.\n\
343 This program comes with ABSOLUTELY NO WARRANTY; for details type use option -h.\n \
344 This is free software, and you are welcome to redistribute it\n \
345 under certain conditions; use option --license for details.";
348 else if(!licenseString.compare(currentOption)){
349 throw(getGPLv3License());
351 if(hasShortOption()&&!(shortOption.compare(currentOption))){
352 if(foundEqual!=std::string::npos)
353 this->push_back(string2type<T>(currentArgument));
354 else if(m_hasArgument && i < argc-1)
355 this->push_back(string2type<T>(argv[++i]));
357 this->push_back(string2type<T>(
"1"));
359 else if(hasLongOption()&&!(longOption.compare(currentOption))){
360 if(foundEqual!=std::string::npos)
361 this->push_back(string2type<T>(currentArgument));
362 else if(m_hasArgument && i < argc-1)
363 this->push_back(string2type<T>(argv[++i]));
365 this->push_back(string2type<T>(
"1"));
368 if(!(this->size())&&m_hasDefault)
369 this->push_back(m_defaultValue);
379 template<>
inline std::string string2type(std::string
const& s){
383 template<>
inline double string2type(std::string
const& s){
384 std::istringstream i;
388 if (!(i >> std::setprecision(12) >> x) )
393 template<>
inline float string2type(std::string
const& s){
394 std::istringstream i;
398 if (!(i >> std::setprecision(12) >> x) )
404 template<>
inline OGRFieldType string2type(std::string
const& s){
406 int ogr_typecount=11;
407 for(
int iType = 0; iType < ogr_typecount; ++iType){
408 if( OGRFieldDefn::GetFieldTypeName((OGRFieldType)iType) != NULL
409 && EQUAL(OGRFieldDefn::GetFieldTypeName((OGRFieldType)iType),s.c_str()))
410 ftype=(OGRFieldType) iType;
416 template<>
inline std::string type2string(
bool const& value){
424 template<>
inline std::string type2string(std::string
const& value){
432 template<>
inline std::string type2string(
float const& value){
433 std::ostringstream oss;
441 template<>
inline std::string type2string(
double const& value){
442 std::ostringstream oss;
450 template<>
inline void Optionpk<bool>::setAll(
const std::string& shortName,
const std::string& longName,
const std::string& helpInfo)
452 m_shortName=shortName;
460 template<>
inline void Optionpk<bool>::setAll(
const std::string& shortName,
const std::string& longName,
const std::string& helpInfo,
const bool& defaultValue,
short hide)
462 m_shortName=shortName;
466 m_defaultValue=defaultValue;
472 template<>
inline Optionpk<bool>::Optionpk(
const std::string& shortName,
const std::string& longName,
const std::string& helpInfo)
474 setAll(shortName,longName,helpInfo);
478 template<>
inline Optionpk<bool>::Optionpk(
const std::string& shortName,
const std::string& longName,
const std::string& helpInfo,
const bool& defaultValue,
short hide)
480 setAll(shortName,longName,helpInfo,defaultValue, hide);
486 std::vector<std::string>::const_iterator opit=this->begin();
487 while(opit!=this->end()){
488 if(opit->find(argument)!=std::string::npos)
throw this class when syntax error in command line option
std::string getShortName() const
get the short name to be used as -shortName
Optionpk()
default constructor
~Optionpk()
default destructor
std::string getHelp() const
get help info stored in m_help
void setDefault(const T &defaultValue)
set a default value for the option
void setHelp(const std::string &helpInfo)
set help information
std::vector< T >::const_iterator findSubstring(const T &argument) const
static std::string getGPLv3License()
get license info
void setLongName(const std::string &longName)
set the long name to be used as –longName
std::string getLongName() const
get the long name to be used as –longName
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 setShortName(const std::string &shortName)
set the short name to be used as -shortName
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, char **argv)
read option from command line (use for all options!)