Debian Codegraph @

src/base/Optionjl.h

raw symbols SCIP
1 /**********************************************************************
2 Optionjl.h: class to handle command line options (inherits from stl vector class)
3 Author(s): [email protected]
4 Copyright (C) 2016-2020 European Union (Joint Research Centre)
5
6 This file is part of jiplib.
7
8 jiplib is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 jiplib is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with jiplib. If not, see <https://www.gnu.org/licenses/>.
20 ***********************************************************************/
21 #ifndef _OPTIONPK_H_
22 #define _OPTIONPK_H_
23
24 #include <vector>
25 #include <string>
26 #include <cstdlib>
27 #include <assert.h>
28 #include <stdexcept>
29 #include <iostream>
30 #include <iomanip>
31 #include <sstream>
32 #include <typeinfo>
33 /* #ifndef WIN32 */
34 /* #include <cxxabi.h> */
35 /* #include <type.hpp> */
36 /* #define mytypeid(T) abi::__cxa_demangle(typeid(T).name(),0,0,&status) */
37 /* #else */
38 /* #define mytypeid(T) typeid(T).name() */
39 /* #endif */
40 #include "ogr_feature.h"
41
42 #ifdef HAVE_CONFIG_H
43 #include <config_jiplib.h>
44 #endif
45
46 #include "typeconversion.h"
47 #include "apps/AppFactory.h"
48
49 /**
50 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.\n
51 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 ...].
52 Several command line option formats are supported:
53 - `-shortOption value`
54 - `-shortOption=value`
55 - `--longOption value`
56 - `--longOption=value`
57 - `-shortOption` (no value for boolean options, which are automatically set by invoking the option)
58 - `--longOption` (no value for boolean options, which are automatically set by invoking the option)
59
60 Option names should have regular characters and no white space in them. Some names are reserved and can not be used either:
61 - short option `h` or long option `help`: shows usage
62 - long option `help` shows long help info
63 - long option `license`: shows license info
64 - long option `version`: shows current version of jiplib
65 - long option `doxygen`: shows help info in table format, ready to be included in doxygen
66 - long option `doxypar`: shows list of parameters for doxygen inline documentation
67
68 A call to member function \ref 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).
69
70 All calls to retrieveOption should reside in a try{} block. If one of the reserved options
71 - `license`
72 - `version`
73 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.
74
75 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 \ref retrieveOption (for any option). An example how to use Optionjl is shown in \ref pktestOption.cc
76 **/
77
78 template<class T> class Optionjl : public std::vector <T>
79 {
80 public:
81 ///default constructor
82 Optionjl();
83 ///constructor for option without default value
84 Optionjl(const std::string& shortName, const std::string& longName, const std::string& helpInfo);
85 ///constructor for option with default value. Option can be hidden for help info
86 Optionjl(const std::string& shortName, const std::string& longName, const std::string& helpInfo,const T& defaultValue, short hide=0);
87 ///default destructor
88 ~Optionjl();
89 ///set help information
90 void setHelp(const std::string& helpInfo){m_help=helpInfo;};
91 ///hide option from short help -h (1) or make invisible to short and long help --help (2)
92 void setHide(short hide){m_hide=hide;};
93 ///read option from command line (use for all options!), std::string implementation
94 bool retrieveOption(int argc, const std::vector<std::string>& argv);
95 ///read option from command line (use for all options!)
96 bool retrieveOption(int argc, char ** argv);
97 ///read option from command line (use for all options!) using AppFactory and register option in the app
98 bool retrieveOption(const app::AppFactory& app);
99 bool retrieveOption(app::AppFactory& app);
100 ///print values for this option
101 template<class T1> friend std::ostream& operator<<(std::ostream & os, const Optionjl<T1>& theOption);
102 ///set all attributes of the option, except default and hide
103 void setAll(const std::string& shortName, const std::string& longName, const std::string& helpInfo);
104 ///set all attributes of the option
105 void setAll(const std::string& shortName, const std::string& longName, const std::string& helpInfo,const T& defaultValue, short hide);
106 ///set a default value for the option
107 void setDefault(const T& defaultValue);
108 std::string getDefaultValue() const {return m_defaultValue;};
109 ///set the short name to be used as -shortName
110 void setShortName(const std::string& shortName);
111 ///set the long name to be used as --longName
112 void setLongName(const std::string& longName);
113 ///get the short name to be used as -shortName
114 std::string getShortName() const {return m_shortName;};
115 ///get the long name to be used as --longName
116 std::string getLongName() const {return m_longName;};
117 ///get help info stored in m_help
118 std::string getHelp() const {return m_help;};
119 ///get license info
120 static std::string getGPLv3License(){
121 return static_cast<std::string>("\n\
122 This program is free software: you can redistribute it and/or modify\n\
123 it under the terms of the GNU General Public License as published by\n\
124 the Free Software Foundation, either version 3 of the License, or\n\
125 (at your option) any later version.\n\
126 \n\
127 This program is distributed in the hope that it will be useful,\n\
128 but WITHOUT ANY WARRANTY; without even the implied warranty of\n\
129 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n\
130 GNU General Public License for more details.\n\
131 \n\
132 You should have received a copy of the GNU General Public License\n\
133 along with this program. If not, see <http://www.gnu.org/licenses/>.\n");};
134
135 ///find substring in options of type string (e.g., -co INTERLEAVE=BAND)
136 ///this template function only makes sense for T=std::string (implemented via a specialization)
137 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);};
138 ///this template function only makes sense for T=std::string (implemented via a specialization)
139 int countSubstring(const T& argument) const{std::string errorString="Error: countSubstring only defined for options of type std::string"; throw(errorString);};
140
141 private:
142 ///all options except bools should have arguments
143 bool hasArgument() const {return m_hasArgument;};
144 ///true unless option was defined with \0 as short option
145 bool hasShortOption() const {return m_shortName.compare("\0");};
146 ///true unless option was defined with \0 as long option
147 bool hasLongOption() const {return m_longName.compare("\0");};
148 ///report the use of the option (called when utility is run with -h or --help)
149 std::string usage() const;
150 ///used to generate help in Doxygen style (used for web documentation)
151 std::string usageDoxygen() const;
152 ///used to generate help in Dictionary style (used for Python binding)
153 std::string usageDict() const;
154 ///used to generate help with in Doxygen style (used for parameters in inline documentation)
155 std::string usageDoxypar() const;
156 ///short name to be used as -shortName
157 std::string m_shortName;
158 ///long name to be used as --longName
159 std::string m_longName;
160 ///string with help information
161 std::string m_help;
162 ///all options except bools should have arguments
163 bool m_hasArgument;
164 ///default value of the option
165 T m_defaultValue;
166 ///has the default been defined
167 bool m_hasDefault;
168 ///0: always show; 1: only show with --help; 2: invisible option
169 short m_hide;
170 };
171
172 template<class T1> std::ostream& operator<<(std::ostream& os, const Optionjl<T1>& theOption)
173 {
174 os << theOption.getLongName() << ": ";
175 for(int index=0;index<theOption.size();++index)
176 os << type2string<T1>(theOption[index]) << " ";
177 os << std::endl;
178 return os;
179 }
180
181 template<class T> inline Optionjl<T>::Optionjl()
182 : m_hasDefault(false)
183 {
184 }
185
186 /**
187 constructor without default value\n
188 shortName is option invoked with `-`\n
189 longName is option invoked with `--`\n
190 helpInfo is the help message that is shown when option -h or --help is invoked\n
191 **/
192 template<class T> inline Optionjl<T>::Optionjl(const std::string& shortName, const std::string& longName, const std::string& helpInfo)
193 : m_hasDefault(false)
194 {
195 setAll(shortName,longName,helpInfo);
196 }
197
198 /**
199 constructor with default value.\n
200 shortName is option invoked with `-`\n
201 longName is option invoked with `--`\n
202 helpInfo is the help message that is shown when option -h or --help is invoked\n
203 defaultValue is default value of the option (first value of vector: option[0])\n
204 hide=0 : option is visible for in both short (`-h`). Typical use: mandatory options\n
205 hide=1 : option is only visible in long help (`--help`). Typical use: expert options\n
206 hide=2 : option is hidden for user. Typical use: Easter eggs or options only known to author
207 **/
208 template<class T> inline Optionjl<T>::Optionjl(const std::string& shortName, const std::string& longName, const std::string& helpInfo,const T& defaultValue, short hide)
209 {
210 setAll(shortName,longName,helpInfo,defaultValue, hide);
211 }
212
213 template<class T> inline std::string Optionjl<T>::usage() const
214 {
215 std::ostringstream helpss;
216 std::string shortOption=m_shortName;
217 std::string longOption=m_longName;
218 shortOption.insert(0,"-");
219 longOption.insert(0,"--");
220 if(hasShortOption()){
221 helpss << " " << std::setiosflags(std::ios::left) << std::setw(6) << shortOption;
222 }
223 else{
224 helpss << " " << std::setiosflags(std::ios::left) << std::setw(6) << " ";
225 }
226 if(hasLongOption()){
227 helpss << " " << std::setiosflags(std::ios::left) << std::setw(20) << longOption;
228 }
229 else{
230 helpss << " " << std::setiosflags(std::ios::left) << std::setw(20) << " ";
231 }
232 helpss << " " << m_help;
233 if(m_hasDefault)
234 helpss << " (default: " << type2string<T>(m_defaultValue) << ")";
235 return helpss.str();
236 }
237
238 template<class T> inline std::string Optionjl<T>::usageDoxypar() const
239 {
240 std::ostringstream helpss;
241 std::string shortOption=m_shortName;
242 std::string longOption=m_longName;
243
244 if(hasLongOption())
245 helpss << std::setiosflags(std::ios::left) << " * @param " << longOption << " ";
246 else if(hasShortOption())
247 helpss << " | " << std::setiosflags(std::ios::left) << " * @param "<< shortOption << " ";
248 int status;
249 helpss << std::setiosflags(std::ios::left) << std::setw(4) << "(type: " << mytypeid(T) << ") ";
250 if(m_hasDefault)
251 helpss << std::setiosflags(std::ios::left) << std::setw(4) << "(default: " << type2string<T>(m_defaultValue) << ") ";
252 helpss << m_help;
253 return helpss.str();
254 }
255
256 template<class T> inline std::string Optionjl<T>::usageDict() const
257 {
258 std::ostringstream helpss;
259 std::string shortOption=m_shortName;
260 std::string longOption=m_longName;
261
262 if(hasLongOption())
263 helpss << std::setiosflags(std::ios::left) << longOption << ":<value> ";
264 else if(hasShortOption())
265 helpss << std::setiosflags(std::ios::left) << shortOption << ":<value> ";
266 int status;
267 helpss << std::setiosflags(std::ios::left) << std::setw(4) << " (type: " << mytypeid(T) << ") ";
268 if(m_hasDefault)
269 helpss << std::setiosflags(std::ios::left) << std::setw(4) << " (default: " << type2string<T>(m_defaultValue) << ") : ";
270 helpss << m_help;
271 return helpss.str();
272 }
273
274 template<class T> inline std::string Optionjl<T>::usageDoxygen() const
275 {
276 std::ostringstream helpss;
277 std::string shortOption=m_shortName;
278 std::string longOption=m_longName;
279
280 if(hasShortOption())
281 helpss << " | " << std::setiosflags(std::ios::left) << std::setw(6) << shortOption << " | ";
282 else
283 helpss << " | " << std::setiosflags(std::ios::left) << " | ";
284 if(hasLongOption())
285 helpss << std::setiosflags(std::ios::left) << std::setw(20) << longOption << " | ";
286 else
287 helpss << std::setiosflags(std::ios::left) << " | ";
288 int status;
289 helpss << std::setiosflags(std::ios::left) << std::setw(4) << mytypeid(T) << " | ";
290 //helpss << std::setiosflags(std::ios::left) << std::setw(4) << abi::__cxa_demangle(typeid(T).name(),0,0,&status) << " | ";
291 if(m_hasDefault)
292 helpss <<std::setiosflags(std::ios::left) << std::setw(5) << type2string<T>(m_defaultValue) << " |";
293 else
294 helpss << " |";
295 helpss << m_help << " | ";
296
297 return helpss.str();
298 }
299
300 template<class T> inline void Optionjl<T>::setAll(const std::string& shortName, const std::string& longName, const std::string& helpInfo)
301 {
302 m_shortName=shortName;
303 m_longName=longName;
304 m_hasArgument=true;
305 m_help=helpInfo;
306 m_hide=0;
307 }
308
309 template<class T> inline void Optionjl<T>::setAll(const std::string& shortName, const std::string& longName, const std::string& helpInfo,const T& defaultValue, short hide)
310 {
311 m_shortName=shortName;
312 m_longName=longName;
313 m_hasArgument=true;
314 m_help=helpInfo;
315 m_defaultValue=defaultValue;
316 m_hasDefault=true;
317 m_hide=hide;
318 }
319
320
321 template<class T> inline Optionjl<T>::~Optionjl()
322 {
323 }
324
325 template<class T> inline bool Optionjl<T>::retrieveOption(const app::AppFactory& app){
326 return(retrieveOption(app.getArgc(),app.getArgv()));
327 }
328
329 template<class T> inline bool Optionjl<T>::retrieveOption(app::AppFactory& app){
330 if(getShortName()!="\0")
331 app.registerShortOption(getShortName());
332 if(getLongName()!="\0")
333 app.registerLongOption(getLongName());
334 return(retrieveOption(app.getArgc(),app.getArgv()));
335 }
336
337 ///make sure to call this function first before using the option in main program (or segmentation fault will occur...)
338 template<class T> inline bool Optionjl<T>::retrieveOption(int argc, const std::vector<std::string>& argv){
339 bool noHelp=true;//return value, alert main program that hard coded option (help, version, license, doxygen) was invoked
340 std::string helpStringShort="-h";//short option for help (hard coded)
341 std::string helpStringLong="--help";//long option for help (hard coded)
342 std::string helpStringDict="--dict";//option for help in Dictionary format
343 std::string helpStringDoxygen="--doxygen";//option to create table of options ready to use for doxygen
344 std::string helpStringDoxypar="--doxypar";//option to create list of doxygen parameters for inline documentation
345 std::string versionString="--version";//option to show current version
346 std::string licenseString="--license";//option to show current version
347 for(int i = 1; i < argc; ++i ){
348 std::string currentArgument;
349 std::string currentOption=argv[i];
350 std::string shortOption=m_shortName;
351 std::string longOption=m_longName;
352 shortOption.insert(0,"-");
353 longOption.insert(0,"--");
354 size_t foundEqual=currentOption.rfind("=");
355 if(foundEqual!=std::string::npos){
356 currentArgument=currentOption.substr(foundEqual+1);
357 currentOption=currentOption.substr(0,foundEqual);
358 }
359 if(!helpStringShort.compare(currentOption)){
360 if(m_hide<1)
361 std::cout << usage() << std::endl;
362 noHelp=false;
363 }
364 else if(!helpStringLong.compare(currentOption)){
365 if(m_hide<2)
366 std::cout << usage() << std::endl;
367 noHelp=false;
368 }
369 else if(!helpStringDoxygen.compare(currentOption)){
370 if(m_hide<2)
371 std::cout << usageDoxygen() << std::endl;
372 noHelp=false;
373 }
374 else if(!helpStringDoxypar.compare(currentOption)){
375 if(m_hide<2)
376 std::cout << usageDoxypar() << std::endl;
377 noHelp=false;
378 }
379 else if(!helpStringDict.compare(currentOption)){
380 if(m_hide<2)
381 std::cout << usageDict() << std::endl;
382 noHelp=false;
383 }
384 else if(!versionString.compare(currentOption)){
385 std::ostringstream oss;
386 oss << "version " << JIPLIB_VERSION;
387 std::string theVersion="version ";
388 /* theVersion+=VERSION; */
389 oss << ", Copyright (C) Pieter Kempeneers.\n\
390 This program comes with ABSOLUTELY NO WARRANTY; for details type use option -h.\n \
391 This is free software, and you are welcome to redistribute it\n \
392 under certain conditions; use option --license for details.";
393 throw(oss.str());//no need to continue registering (break prevents from multiplication of version info)
394 }
395 else if(!licenseString.compare(currentOption)){
396 throw(getGPLv3License());
397 }
398 if(hasShortOption()&&!(shortOption.compare(currentOption))){//for -option
399 if(foundEqual!=std::string::npos)
400 this->push_back(string2type<T>(currentArgument));
401 else if(m_hasArgument && i < argc-1)
402 this->push_back(string2type<T>(argv[++i]));
403 else
404 this->push_back(string2type<T>("1"));
405 }
406 else if(hasLongOption()&&!(longOption.compare(currentOption))){//for --option
407 if(foundEqual!=std::string::npos)
408 this->push_back(string2type<T>(currentArgument));
409 else if(m_hasArgument && i < argc-1){
410 std::string argValue=argv[++i];
411 this->push_back(string2type<T>(argValue));
412 //this->push_back(string2type<T>(argv[++i]));
413 }
414 else
415 this->push_back(string2type<T>("1"));
416 }
417 }
418 if(!(this->size())&&m_hasDefault)//only set default value if no options were given
419 this->push_back(m_defaultValue);
420 return(noHelp);
421 }
422
423 /**
424 make sure to call this function first before using the option in main program (or segmentation fault will occur...)
425 **/
426 template<class T> inline bool Optionjl<T>::retrieveOption(int argc, char **argv){
427 bool noHelp=true;//return value, alert main program that hard coded option (help, version, license, doxygen) was invoked
428 std::string helpStringShort="-h";//short option for help (hard coded)
429 std::string helpStringLong="--help";//long option for help (hard coded)
430 std::string helpStringDict="--dict";//option for help in Dictionary format
431 std::string helpStringDoxygen="--doxygen";//option to create table of options ready to use for doxygen
432 std::string helpStringDoxypar="--doxypar";//option to list of parameters for doxygen inline documentation
433 std::string versionString="--version";//option to show current version
434 std::string licenseString="--license";//option to show current version
435 for(int i = 1; i < argc; ++i ){
436 std::string currentArgument;
437 std::string currentOption=argv[i];
438 std::string shortOption=m_shortName;
439 std::string longOption=m_longName;
440 shortOption.insert(0,"-");
441 longOption.insert(0,"--");
442 size_t foundEqual=currentOption.rfind("=");
443 if(foundEqual!=std::string::npos){
444 currentArgument=currentOption.substr(foundEqual+1);
445 currentOption=currentOption.substr(0,foundEqual);
446 }
447 if(!helpStringShort.compare(currentOption)){
448 if(m_hide<1)
449 std::cout << usage() << std::endl;
450 noHelp=false;
451 }
452 else if(!helpStringLong.compare(currentOption)){
453 if(m_hide<2)
454 std::cout << usage() << std::endl;
455 noHelp=false;
456 }
457 else if(!helpStringDoxygen.compare(currentOption)){
458 if(m_hide<2)
459 std::cout << usageDoxygen() << std::endl;
460 noHelp=false;
461 }
462 else if(!helpStringDict.compare(currentOption)){
463 if(m_hide<2)
464 std::cout << usageDict() << std::endl;
465 noHelp=false;
466 }
467 else if(!helpStringDoxypar.compare(currentOption)){
468 if(m_hide<2)
469 std::cout << usageDoxypar() << std::endl;
470 noHelp=false;
471 }
472 else if(!versionString.compare(currentOption)){
473 std::ostringstream oss;
474 oss << "version " << JIPLIB_VERSION;
475 std::string theVersion="version ";
476 /* theVersion+=VERSION; */
477 oss << ", Copyright (C) Pieter Kempeneers.\n\
478 This program comes with ABSOLUTELY NO WARRANTY; for details type use option -h.\n \
479 This is free software, and you are welcome to redistribute it\n \
480 under certain conditions; use option --license for details.";
481 throw(oss.str());//no need to continue registering (break prevents from multiplication of version info)
482 }
483 else if(!licenseString.compare(currentOption)){
484 throw(getGPLv3License());
485 }
486 if(hasShortOption()&&!(shortOption.compare(currentOption))){//for -option
487 if(foundEqual!=std::string::npos)
488 this->push_back(string2type<T>(currentArgument));
489 else if(m_hasArgument && i < argc-1)
490 this->push_back(string2type<T>(argv[++i]));
491 else
492 this->push_back(string2type<T>("1"));
493 }
494 else if(hasLongOption()&&!(longOption.compare(currentOption))){//for --option
495 if(foundEqual!=std::string::npos)
496 this->push_back(string2type<T>(currentArgument));
497 else if(m_hasArgument && i < argc-1)
498 this->push_back(string2type<T>(argv[++i]));
499 else
500 this->push_back(string2type<T>("1"));
501 }
502 }
503 if(!(this->size())&&m_hasDefault)//only set default value if no options were given
504 this->push_back(m_defaultValue);
505 return(noHelp);
506 }
507
508 //template<class T> typename std::vector<T>::const_iterator Optionjl<T>::findSubstring(const T& argument) const {std::string errorString="Error: findSubstring only defined for options of type std::string"; throw(errorString);}
509
510 ///specialization for bool
511 template<> inline void Optionjl<bool>::setAll(const std::string& shortName, const std::string& longName, const std::string& helpInfo)
512 {
513 m_shortName=shortName;
514 m_longName=longName;
515 m_hasArgument=false;
516 m_help=helpInfo;
517 m_hide=0;
518 }
519
520 ///specialization for bool
521 template<> inline void Optionjl<bool>::setAll(const std::string& shortName, const std::string& longName, const std::string& helpInfo,const bool& defaultValue, short hide)
522 {
523 m_shortName=shortName;
524 m_longName=longName;
525 m_hasArgument=false;
526 m_help=helpInfo;
527 m_defaultValue=defaultValue;
528 m_hasDefault=true;
529 m_hide=hide;
530 }
531
532 ///specialization for bool
533 template<> inline Optionjl<bool>::Optionjl(const std::string& shortName, const std::string& longName, const std::string& helpInfo)
534 {
535 setAll(shortName,longName,helpInfo);
536 }
537
538 ///specialization for bool
539 template<> inline Optionjl<bool>::Optionjl(const std::string& shortName, const std::string& longName, const std::string& helpInfo,const bool& defaultValue, short hide)
540 {
541 setAll(shortName,longName,helpInfo,defaultValue, hide);
542 }
543
544 //specialization (only makes sense for T=std::string), generic function throws exception
545 //find a substring in string option (e.g., option is of type -co INTERLEAVE=BAND)
546 template<> inline std::vector<std::string>::const_iterator Optionjl<std::string>::findSubstring(const std::string& argument) const{
547 std::vector<std::string>::const_iterator opit=this->begin();
548 while(opit!=this->end()){
549 if(opit->find(argument)!=std::string::npos)
550 break;
551 ++opit;
552 }
553 return opit;
554 }
555
556 //specialization (only makes sense for T=std::string), generic function throws exception
557 //find a substring in string option (e.g., option is of type -co INTERLEAVE=BAND)
558 template<> inline int Optionjl<std::string>::countSubstring(const std::string& argument) const{
559 int count=0;
560 std::vector<std::string>::const_iterator opit=this->begin();
561 while(opit!=this->end()){
562 if(opit->find(argument)!=std::string::npos)
563 ++count;
564 ++opit;
565 }
566 return count;
567 }
568
569 #endif