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;
105 template<
class T>
class Optionpk :
public std::vector <T>
111 Optionpk(
const std::string& shortName,
const std::string& longName,
const std::string& helpInfo);
113 Optionpk(
const std::string& shortName,
const std::string& longName,
const std::string& helpInfo,
const T& defaultValue,
short hide=0);
117 void setHelp(
const std::string& helpInfo){m_help=helpInfo;};
119 template<
class T1>
friend std::ostream& operator<<(std::ostream & os, const Optionpk<T1>& theOption);
121 void setAll(
const std::string& shortName,
const std::string& longName,
const std::string& helpInfo);
122 void setAll(
const std::string& shortName,
const std::string& longName,
const std::string& helpInfo,
const T& defaultValue,
short hide);
123 void setDefault(
const T& defaultValue);
124 std::string getDefaultValue()
const {
return m_defaultValue;};
125 void setShortName(
const std::string& shortName);
126 void setLongName(
const std::string& longName);
127 std::string getShortName()
const {
return m_shortName;};
128 std::string getLongName()
const {
return m_longName;};
130 std::string getHelp()
const {
return m_help;};
131 static std::string getGPLv3License(){
132 return static_cast<std::string
>(
"\n\
133 This program is free software: you can redistribute it and/or modify\n\
134 it under the terms of the GNU General Public License as published by\n\
135 the Free Software Foundation, either version 3 of the License, or\n\
136 (at your option) any later version.\n\
138 This program is distributed in the hope that it will be useful,\n\
139 but WITHOUT ANY WARRANTY; without even the implied warranty of\n\
140 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n\
141 GNU General Public License for more details.\n\
143 You should have received a copy of the GNU General Public License\n\
144 along with this program. If not, see <http://www.gnu.org/licenses/>.\n");};
147 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);};
150 bool hasArgument()
const {
return m_hasArgument;};
151 bool hasShortOption()
const {
return m_shortName.compare(
"\0");};
152 bool hasLongOption()
const {
return m_longName.compare(
"\0");};
153 std::string usage()
const;
154 std::string usageDoxygen()
const;
156 std::string m_shortName;
157 std::string m_longName;
165 template<
class T1> std::ostream& operator<<(std::ostream& os, const Optionpk<T1>& theOption)
167 os << theOption.getLongName() <<
": ";
168 for(
int index=0;index<theOption.size();++index)
169 os << type2string<T1>(theOption[index]) <<
" ";
175 : m_hasDefault(false)
185 template<
class T>
inline Optionpk<T>::Optionpk(
const std::string& shortName,
const std::string& longName,
const std::string& helpInfo)
186 : m_hasDefault(false)
188 setAll(shortName,longName,helpInfo);
201 template<
class T>
inline Optionpk<T>::Optionpk(
const std::string& shortName,
const std::string& longName,
const std::string& helpInfo,
const T& defaultValue,
short hide)
203 setAll(shortName,longName,helpInfo,defaultValue, hide);
208 std::ostringstream helpss;
209 std::string shortOption=m_shortName;
210 std::string longOption=m_longName;
211 shortOption.insert(0,
"-");
212 longOption.insert(0,
"--");
214 helpss <<
" " << std::setiosflags(std::ios::left) << std::setw(4) << shortOption;
216 helpss <<
" " << std::setiosflags(std::ios::left) << std::setw(4) <<
" ";
218 helpss <<
" " << std::setiosflags(std::ios::left) << std::setw(20) << longOption;
220 helpss <<
" " << std::setiosflags(std::ios::left) << std::setw(20) <<
" ";
221 helpss <<
" " << m_help;
223 helpss <<
" (default: " << type2string<T>(m_defaultValue) <<
")";
229 std::ostringstream helpss;
230 std::string shortOption=m_shortName;
231 std::string longOption=m_longName;
234 helpss <<
" | " << std::setiosflags(std::ios::left) << std::setw(6) << shortOption <<
" | ";
236 helpss <<
" | " << std::setiosflags(std::ios::left) <<
" | ";
238 helpss << std::setiosflags(std::ios::left) << std::setw(20) << longOption <<
" | ";
240 helpss << std::setiosflags(std::ios::left) <<
" | ";
242 helpss << std::setiosflags(std::ios::left) << std::setw(4) << mytypeid(T) <<
" | ";
245 helpss <<std::setiosflags(std::ios::left) << std::setw(5) << type2string<T>(m_defaultValue) <<
" |";
248 helpss << m_help <<
" | ";
253 template<
class T>
inline void Optionpk<T>::setAll(
const std::string& shortName,
const std::string& longName,
const std::string& helpInfo)
255 m_shortName=shortName;
262 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)
264 m_shortName=shortName;
268 m_defaultValue=defaultValue;
283 std::string helpStringShort=
"-h";
284 std::string helpStringLong=
"--help";
285 std::string helpStringDoxygen=
"--doxygen";
286 std::string versionString=
"--version";
287 std::string licenseString=
"--license";
288 for(
int i = 1; i < argc; ++i ){
289 std::string currentArgument;
290 std::string currentOption=argv[i];
291 std::string shortOption=m_shortName;
292 std::string longOption=m_longName;
293 shortOption.insert(0,
"-");
294 longOption.insert(0,
"--");
295 size_t foundEqual=currentOption.rfind(
"=");
296 if(foundEqual!=std::string::npos){
297 currentArgument=currentOption.substr(foundEqual+1);
298 currentOption=currentOption.substr(0,foundEqual);
300 if(!helpStringShort.compare(currentOption)){
302 std::cout << usage() << std::endl;
305 else if(!helpStringLong.compare(currentOption)){
307 std::cout << usage() << std::endl;
310 else if(!helpStringDoxygen.compare(currentOption)){
312 std::cout << usageDoxygen() << std::endl;
315 else if(!versionString.compare(currentOption)){
316 std::string theVersion=
"version ";
318 theVersion+=
", Copyright (C) Pieter Kempeneers.\n\
319 This program comes with ABSOLUTELY NO WARRANTY; for details type use option -h.\n \
320 This is free software, and you are welcome to redistribute it\n \
321 under certain conditions; use option --license for details.";
324 else if(!licenseString.compare(currentOption)){
325 throw(getGPLv3License());
327 if(hasShortOption()&&!(shortOption.compare(currentOption))){
328 if(foundEqual!=std::string::npos)
329 this->push_back(string2type<T>(currentArgument));
330 else if(m_hasArgument && i < argc-1)
331 this->push_back(string2type<T>(argv[++i]));
333 this->push_back(string2type<T>(
"1"));
335 else if(hasLongOption()&&!(longOption.compare(currentOption))){
336 if(foundEqual!=std::string::npos)
337 this->push_back(string2type<T>(currentArgument));
338 else if(m_hasArgument && i < argc-1)
339 this->push_back(string2type<T>(argv[++i]));
341 this->push_back(string2type<T>(
"1"));
344 if(!(this->size())&&m_hasDefault)
345 this->push_back(m_defaultValue);
355 template<>
inline std::string string2type(std::string
const& s){
360 template<>
inline OGRFieldType string2type(std::string
const& s){
362 int ogr_typecount=11;
363 for(
int iType = 0; iType < ogr_typecount; ++iType){
364 if( OGRFieldDefn::GetFieldTypeName((OGRFieldType)iType) != NULL
365 && EQUAL(OGRFieldDefn::GetFieldTypeName((OGRFieldType)iType),s.c_str()))
366 ftype=(OGRFieldType) iType;
372 template<>
inline std::string type2string(
bool const& value){
380 template<>
inline std::string type2string(std::string
const& value){
388 template<>
inline std::string type2string(
float const& value){
389 std::ostringstream oss;
397 template<>
inline std::string type2string(
double const& value){
398 std::ostringstream oss;
406 template<>
inline void Optionpk<bool>::setAll(
const std::string& shortName,
const std::string& longName,
const std::string& helpInfo)
408 m_shortName=shortName;
416 template<>
inline void Optionpk<bool>::setAll(
const std::string& shortName,
const std::string& longName,
const std::string& helpInfo,
const bool& defaultValue,
short hide)
418 m_shortName=shortName;
422 m_defaultValue=defaultValue;
428 template<>
inline Optionpk<bool>::Optionpk(
const std::string& shortName,
const std::string& longName,
const std::string& helpInfo)
430 setAll(shortName,longName,helpInfo);
434 template<>
inline Optionpk<bool>::Optionpk(
const std::string& shortName,
const std::string& longName,
const std::string& helpInfo,
const bool& defaultValue,
short hide)
436 setAll(shortName,longName,helpInfo,defaultValue, hide);
442 std::vector<std::string>::const_iterator opit=this->begin();
443 while(opit!=this->end()){
444 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)