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;};
119 void setHide(
short hide){m_hide=hide;};
121 template<
class T1>
friend std::ostream& operator<<(std::ostream & os, const Optionpk<T1>& theOption);
123 void setAll(
const std::string& shortName,
const std::string& longName,
const std::string& helpInfo);
124 void setAll(
const std::string& shortName,
const std::string& longName,
const std::string& helpInfo,
const T& defaultValue,
short hide);
125 void setDefault(
const T& defaultValue);
126 std::string getDefaultValue()
const {
return m_defaultValue;};
127 void setShortName(
const std::string& shortName);
128 void setLongName(
const std::string& longName);
129 std::string getShortName()
const {
return m_shortName;};
130 std::string getLongName()
const {
return m_longName;};
132 std::string getHelp()
const {
return m_help;};
133 static std::string getGPLv3License(){
134 return static_cast<std::string
>(
"\n\
135 This program is free software: you can redistribute it and/or modify\n\
136 it under the terms of the GNU General Public License as published by\n\
137 the Free Software Foundation, either version 3 of the License, or\n\
138 (at your option) any later version.\n\
140 This program is distributed in the hope that it will be useful,\n\
141 but WITHOUT ANY WARRANTY; without even the implied warranty of\n\
142 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n\
143 GNU General Public License for more details.\n\
145 You should have received a copy of the GNU General Public License\n\
146 along with this program. If not, see <http://www.gnu.org/licenses/>.\n");};
149 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);};
152 bool hasArgument()
const {
return m_hasArgument;};
153 bool hasShortOption()
const {
return m_shortName.compare(
"\0");};
154 bool hasLongOption()
const {
return m_longName.compare(
"\0");};
155 std::string usage()
const;
156 std::string usageDoxygen()
const;
158 std::string m_shortName;
159 std::string m_longName;
167 template<
class T1> std::ostream& operator<<(std::ostream& os, const Optionpk<T1>& theOption)
169 os << theOption.getLongName() <<
": ";
170 for(
int index=0;index<theOption.size();++index)
171 os << type2string<T1>(theOption[index]) <<
" ";
177 : m_hasDefault(false)
187 template<
class T>
inline Optionpk<T>::Optionpk(
const std::string& shortName,
const std::string& longName,
const std::string& helpInfo)
188 : m_hasDefault(false)
190 setAll(shortName,longName,helpInfo);
203 template<
class T>
inline Optionpk<T>::Optionpk(
const std::string& shortName,
const std::string& longName,
const std::string& helpInfo,
const T& defaultValue,
short hide)
205 setAll(shortName,longName,helpInfo,defaultValue, hide);
210 std::ostringstream helpss;
211 std::string shortOption=m_shortName;
212 std::string longOption=m_longName;
213 shortOption.insert(0,
"-");
214 longOption.insert(0,
"--");
216 helpss <<
" " << std::setiosflags(std::ios::left) << std::setw(4) << shortOption;
218 helpss <<
" " << std::setiosflags(std::ios::left) << std::setw(4) <<
" ";
220 helpss <<
" " << std::setiosflags(std::ios::left) << std::setw(20) << longOption;
222 helpss <<
" " << std::setiosflags(std::ios::left) << std::setw(20) <<
" ";
223 helpss <<
" " << m_help;
225 helpss <<
" (default: " << type2string<T>(m_defaultValue) <<
")";
231 std::ostringstream helpss;
232 std::string shortOption=m_shortName;
233 std::string longOption=m_longName;
236 helpss <<
" | " << std::setiosflags(std::ios::left) << std::setw(6) << shortOption <<
" | ";
238 helpss <<
" | " << std::setiosflags(std::ios::left) <<
" | ";
240 helpss << std::setiosflags(std::ios::left) << std::setw(20) << longOption <<
" | ";
242 helpss << std::setiosflags(std::ios::left) <<
" | ";
244 helpss << std::setiosflags(std::ios::left) << std::setw(4) << mytypeid(T) <<
" | ";
247 helpss <<std::setiosflags(std::ios::left) << std::setw(5) << type2string<T>(m_defaultValue) <<
" |";
250 helpss << m_help <<
" | ";
255 template<
class T>
inline void Optionpk<T>::setAll(
const std::string& shortName,
const std::string& longName,
const std::string& helpInfo)
257 m_shortName=shortName;
264 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)
266 m_shortName=shortName;
270 m_defaultValue=defaultValue;
285 std::string helpStringShort=
"-h";
286 std::string helpStringLong=
"--help";
287 std::string helpStringDoxygen=
"--doxygen";
288 std::string versionString=
"--version";
289 std::string licenseString=
"--license";
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);
302 if(!helpStringShort.compare(currentOption)){
304 std::cout << usage() << std::endl;
307 else if(!helpStringLong.compare(currentOption)){
309 std::cout << usage() << std::endl;
312 else if(!helpStringDoxygen.compare(currentOption)){
314 std::cout << usageDoxygen() << std::endl;
317 else if(!versionString.compare(currentOption)){
318 std::string 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.";
326 else if(!licenseString.compare(currentOption)){
327 throw(getGPLv3License());
329 if(hasShortOption()&&!(shortOption.compare(currentOption))){
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]));
335 this->push_back(string2type<T>(
"1"));
337 else if(hasLongOption()&&!(longOption.compare(currentOption))){
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]));
343 this->push_back(string2type<T>(
"1"));
346 if(!(this->size())&&m_hasDefault)
347 this->push_back(m_defaultValue);
357 template<>
inline std::string string2type(std::string
const& s){
361 template<>
inline double string2type(std::string
const& s){
362 std::istringstream i;
366 if (!(i >> std::setprecision(12) >> x) )
371 template<>
inline float string2type(std::string
const& s){
372 std::istringstream i;
376 if (!(i >> std::setprecision(12) >> x) )
382 template<>
inline OGRFieldType string2type(std::string
const& s){
384 int ogr_typecount=11;
385 for(
int iType = 0; iType < ogr_typecount; ++iType){
386 if( OGRFieldDefn::GetFieldTypeName((OGRFieldType)iType) != NULL
387 && EQUAL(OGRFieldDefn::GetFieldTypeName((OGRFieldType)iType),s.c_str()))
388 ftype=(OGRFieldType) iType;
394 template<>
inline std::string type2string(
bool const& value){
402 template<>
inline std::string type2string(std::string
const& value){
410 template<>
inline std::string type2string(
float const& value){
411 std::ostringstream oss;
419 template<>
inline std::string type2string(
double const& value){
420 std::ostringstream oss;
428 template<>
inline void Optionpk<bool>::setAll(
const std::string& shortName,
const std::string& longName,
const std::string& helpInfo)
430 m_shortName=shortName;
438 template<>
inline void Optionpk<bool>::setAll(
const std::string& shortName,
const std::string& longName,
const std::string& helpInfo,
const bool& defaultValue,
short hide)
440 m_shortName=shortName;
444 m_defaultValue=defaultValue;
450 template<>
inline Optionpk<bool>::Optionpk(
const std::string& shortName,
const std::string& longName,
const std::string& helpInfo)
452 setAll(shortName,longName,helpInfo);
456 template<>
inline Optionpk<bool>::Optionpk(
const std::string& shortName,
const std::string& longName,
const std::string& helpInfo,
const bool& defaultValue,
short hide)
458 setAll(shortName,longName,helpInfo,defaultValue, hide);
464 std::vector<std::string>::const_iterator opit=this->begin();
465 while(opit!=this->end()){
466 if(opit->find(argument)!=std::string::npos)
throw this class when syntax error in command line option
Optionpk()
default constructor
~Optionpk()
default destructor
void setHelp(const std::string &helpInfo)
set help information
bool retrieveOption(int argc, char **argv)