Jpp 21.0.0-rc.1
the software that should make you happy
Loading...
Searching...
No Matches
JParser.hh
Go to the documentation of this file.
1#ifndef __JEEP__JPARSER__
2#define __JEEP__JPARSER__
3
4#include <sys/types.h>
5#include <unistd.h>
6#include <stdio.h>
7#include <string>
8#include <iostream>
9#include <sstream>
10#include <fstream>
11#include <vector>
12#include <map>
13#include <ctype.h>
14#include <type_traits>
15#include <functional>
16
17#include "JLang/Jpp.hh"
18#include "JLang/JAbstractIO.hh"
19#include "JLang/JException.hh"
22#include "JLang/JComparable.hh"
24#include "JLang/JResolve.hh"
25#include "JLang/JLangToolkit.hh"
26#include "JLang/JManip.hh"
27
29#include "Jeep/JeepToolkit.hh"
30#include "Jeep/JArgs.hh"
31#include "Jeep/JMessage.hh"
32#include "Jeep/JPrint.hh"
33
34
35/**
36 * \file
37 * Utility class to parse command line options.
38 * \author mdejong
39 */
40
41class TString; //<! Forward declaration of ROOT class
42
43/**
44 * Local namespace for command line parser.
45 */
46namespace JPARSER {
47
55
56 using JEEP::readObject;
58 using JEEP::writeArray;
60 using JEEP::JArgs;
61 using JEEP::JMessage;
62
63
64 /**
65 * Empty structure for specification of parser element that is initialised (i.e.\ does not require input).
66 */
67 struct initialised {};
68
69
70 /**
71 * Empty structure for specification of parser element that is not initialised (i.e.\ does require input).
72 */
73 struct not_initialised {};
74
75
76 /**
77 * Parser options.
78 */
79 static char START_OF_OPTION = '-'; //!< start of an option
80 static char END_OF_OPTIONS = '-'; //!< end of all options
81 static char HELP_OPTION = 'h'; //!< help option
82 static char REVISION_OPTION = 'v'; //!< revision option
83 static char PRINT_OPTION = '!'; //!< print option
84 static char PID_OPTION = 'P'; //!< print PID to file
85 static int NORMAL_EXIT_CODE = 0; //!< exit code of normal end
86 static int WIDTH = 12; //!< format specifier
87
88
89 /**
90 * Check for stream state.
91 *
92 * Note that end-of-file is not defined as an error so to normally process e.g.\ std::string and std::vector.
93 *
94 * \param in input stream
95 * \return true if failure; else false
96 */
97 inline bool fail(std::istream& in)
98 {
99 return in.bad() || (in.fail() && !in.eof());
100 }
101
102
103 /**
104 * Auxiliary class to assign a custom value following the reading of a specific textual value.
105 */
106 template<class T, bool is_fundamental = std::is_fundamental<T>::value>
107 class JProxy;
108
109
110 /**
111 * Template specialisation of JProxy for fundamental data type.
112 */
113 template<class T>
114 class JProxy<T, true>
115 {
116 public:
117 /**
118 * Constructor.
119 *
120 * \param option textual value
121 * \param value custom value
122 */
123 JProxy(const std::string& option, const T& value) :
124 __value (),
125 __option(option),
126 __custom(value)
127 {}
128
129
130 /**
131 * Constructor.
132 *
133 * \param value actual value
134 */
135 JProxy(const T& value) :
136 __value (value),
137 __option(),
138 __custom()
139 {}
140
141
142 /**
143 * Get actual value.
144 *
145 * \return value
146 */
147 const T& getValue() const
148 {
149 return __value;
150 }
151
152
153 /**
154 * Get option.
155 *
156 * \return option
157 */
158 const std::string& getOption() const
159 {
160 return __option;
161 }
162
163
164 /**
165 * Get custom value.
166 *
167 * \return value
168 */
169 const T& getCustom() const
170 {
171 return __custom;
172 }
173
174
175 /**
176 * Type conversion operator.
177 *
178 * \return object
179 */
180 operator const T&() const
181 {
182 return __value;
183 }
184
185
186 /**
187 * Assignment operator.
188 *
189 * Note that only the actual value is assigned.
190 *
191 * \param object object
192 * \return this object
193 */
194 JProxy& operator=(const JProxy& object)
195 {
196 __value = object.__value;
197
198 return *this;
199 }
200
201
202 /**
203 * Assignment to a value.
204 *
205 * \param value value
206 * \return this object
207 */
208 JProxy& operator=(const T& value)
209 {
210 __value = value;
211
212 return *this;
213 }
214
215
216 /**
217 * Read option from input.
218 *
219 * \param in input stream
220 * \param object option
221 * \return input stream
222 */
223 friend inline std::istream& operator>>(std::istream& in, JProxy& object)
224 {
225 using namespace std;
226
227 string buffer;
228
229 if (getline(in, buffer)) {
230
231 if (buffer == object.__option) {
232
233 object.__value = object.__custom;
234
235 } else {
236
237 istringstream is(buffer);
238
239 is >> object.__value;
240
241 in.setstate(is.rdstate());
242 }
243 }
244
245 return in;
246 }
247
248
249 /**
250 * Write options to output.
251 *
252 * \param out output stream
253 * \param object option
254 * \return output stream
255 */
256 friend inline std::ostream& operator<<(std::ostream& out, const JProxy& object)
257 {
258 return out << object.__value;
259 }
260
261 protected:
263 std::string __option;
265 };
266
267
268 /**
269 * Template specialisation of JProxy for non-fundamental data type.
270 */
271 template<class T>
272 class JProxy<T, false> :
273 public T
274 {
275 public:
276 /**
277 * Constructor.
278 *
279 * \param option textual value
280 * \param value custom value
281 */
282 JProxy(const std::string& option, const T& value) :
283 T(),
284 __option(option),
285 __custom(value)
286 {}
287
288
289 /**
290 * Constructor.
291 *
292 * \param value actual value
293 */
294 JProxy(const T& value) :
295 T(value),
296 __option(),
297 __custom()
298 {}
299
300
301 /**
302 * Get actual value.
303 *
304 * \return value
305 */
306 const T& getValue() const
307 {
308 return static_cast<const T&>(*this);
309 }
310
311
312 /**
313 * Get option.
314 *
315 * \return option
316 */
317 const std::string& getOption() const
318 {
319 return __option;
320 }
321
322
323 /**
324 * Get custom value.
325 *
326 * \return value
327 */
328 const T& getCustom() const
329 {
330 return __custom;
331 }
332
333
334 /**
335 * Assignment operator.
336 *
337 * Note that only the actual value is assigned.
338 *
339 * \param object object
340 * \return this object
341 */
342 JProxy& operator=(const JProxy& object)
343 {
344 static_cast<T&>(*this) = static_cast<const T&>(object);
345
346 return *this;
347 }
348
349
350 /**
351 * Assignment to a value.
352 *
353 * \param value value
354 * \return this object
355 */
356 JProxy& operator=(const T& value)
357 {
358 static_cast<T&>(*this) = value;
359
360 return *this;
361 }
362
363
364 /**
365 * Read option from input.
366 *
367 * \param in input stream
368 * \param object option
369 * \return input stream
370 */
371 friend inline std::istream& operator>>(std::istream& in, JProxy& object)
372 {
373 using namespace std;
374
375 string buffer;
376
377 if (getline(in, buffer)) {
378
379 if (buffer == object.__option) {
380
381 static_cast<T&>(object) = object.__custom;
382
383 } else {
384
385 istringstream is(buffer);
386
387 is >> static_cast<T&>(object);
388
389 in.setstate(is.rdstate());
390 }
391 }
392
393 return in;
394 }
395
396
397 /**
398 * Write options to output.
399 *
400 * \param out output stream
401 * \param object option
402 * \return output stream
403 */
404 friend inline std::ostream& operator<<(std::ostream& out, const JProxy& object)
405 {
406 return out << static_cast<const T&>(object);
407 }
408
409 protected:
410 std::string __option;
412 };
413
414
415 /**
416 * Auxiliary class to handle multiple boolean-like I/O.
417 */
418 class JCounter :
420 public JComparable<JCounter>,
421 public JComparable<JCounter, int>
422 {
423 public:
424 /**
425 * Default constructor.
426 */
428 counter(0)
429 {}
430
431
432 /**
433 * Get status of this counter.
434 *
435 * \return true if counter not zero; else false
436 */
437 virtual bool getStatus() const override
438 {
439 return (this->counter != 0);
440 }
441
442
443 /**
444 * Compare value.
445 *
446 * \param value value
447 * \return true if this counter is less than given value; else false
448 */
449 inline bool less(const JCounter& value) const
450 {
451 return this->counter < value.counter;
452 }
453
454
455 /**
456 * Compare value.
457 *
458 * \param value value
459 * \return true if this counter is less than given value; else false
460 */
461 inline bool less(const int value) const
462 {
463 return this->counter < value;
464 }
465
466
467 /**
468 * Compare value.
469 *
470 * \param value value
471 * \return true if this counter is more than given value; else false
472 */
473 inline bool more(const int value) const
474 {
475 return this->counter > value;
476 }
477
478
479 /**
480 * Get counter.
481 *
482 * \return counter
483 */
484 int getCounter() const
485 {
486 return counter;
487 }
488
489
490 /**
491 * Set value.
492 *
493 * Note that no assignment actually takes place but the internal counter is set to one.
494 *
495 * \param value value
496 * \return this object
497 */
498 const JCounter& operator=(const bool value)
499 {
500 counter = 1;
501
502 return *this;
503 }
504
505
506 /**
507 * Read option from input.
508 *
509 * Note that no value actually is read, only the internal counter is incremented.
510 *
511 * \param in input stream
512 * \param object option
513 * \return input stream
514 */
515 friend inline std::istream& operator>>(std::istream& in, JCounter& object)
516 {
517 ++object.counter;
518
519 return in;
520 }
521
522
523 /**
524 * Write options to output.
525 *
526 * \param out output stream
527 * \param object option
528 * \return output stream
529 */
530 friend inline std::ostream& operator<<(std::ostream& out, const JCounter& object)
531 {
532 out << object.counter;
533
534 return out;
535 }
536
537 protected:
539 };
540
541
542 /**
543 * Interface for I/O of parser element.
544 */
546 public JStreamInput,
547 public JStreamOutput
548 {
549 protected:
550 /**
551 * Constructor.
552 *
553 * \param name name of object
554 * \param help help of object
555 */
556 JParserElementInterface(const std::string& name = "arg",
557 const std::string& help = "") :
558 __name(name),
559 __help(help)
560 {}
561
562
563 public:
564 /**
565 * Get name of parameter.
566 *
567 * \return name
568 */
569 const std::string& getName() const
570 {
571 return __name;
572 }
573
574
575 /**
576 * Get help of parameter.
577 *
578 * \return help
579 */
580 const std::string& getHelp() const
581 {
582 return __help;
583 }
584
585
586 /**
587 * Get status of parameter.
588 *
589 * \return status
590 */
591 virtual bool getStatus() const = 0;
592
593
594 /**
595 * Get initialisation status of parameter.
596 *
597 * \return status
598 */
599 virtual bool getInitialisationStatus() const = 0;
600
601
602 /**
603 * Set initialisation status of parameter.
604 *
605 * \param value initialisation status
606 */
607 virtual void setInitialiationStatus(const bool value) = 0;
608
609
610 /**
611 * Print.
612 *
613 * \param out output stream
614 */
615 virtual void print(std::ostream& out) const
616 {
617 using namespace std;
618
619 out << "<" << getName() << ">";
620
621 if (getStatus() && !getShortprint(out)) {
622
623 int width = WIDTH - getName().length();
624
625 if (width > 0) {
626 out << setw(width) << " ";
627 }
628
629 out << " = ";
630
631 write(out);
632 }
633
634 if (getLongprint(out) && getHelp() != "") {
635 out << " \"" << getHelp() << "\"";
636 }
637 }
638
639
640 /**
641 * Read counter.
642 *
643 * \return true if at least one character to be read; else false
644 */
645 virtual bool gcount() const
646 {
647 return true;
648 }
649
650
651 protected:
652 std::string __name;
653 std::string __help;
654 };
655
656
657 /**
658 * Template class holder for I/O of parser element.
659 * This class implements the JPARSER::JParserElementInterface interface.
660 */
661 template<class JType_t, bool has_eq = JComparisonAvailable<JType_t>::has_eq>
663
664
665 /**
666 * Auxiliary class to assign the remainder of a sequence of Comma Separated Values.
667 */
668 template<class JType_t>
669 class JCSV {
670 public:
671 /**
672 * Constructor.
673 *
674 * \param element parser element
675 */
677 __element(element)
678 {}
679
680
681 /**
682 * Type conversion operator.
683 *
684 * \return parser element
685 */
686 operator const JParserTemplateElement<JType_t>&() const
687 {
688 return __element;
689 }
690
691
692 /**
693 * Parsing of additional possible values.
694 *
695 * \param value possible value
696 * \return this JCSV object
697 */
698 JCSV& operator,(JType_t value)
699 {
700 __element.possibleValues.push_back(value);
701
702 return *this;
703 }
704
705
706 /**
707 * Parsing of additional possible values.
708 *
709 * \param values possible values
710 * \return this object
711 */
712 template<template<class, class> class JContainer_t, class JAllocator_t>
713 JCSV& operator,(const JContainer_t<JType_t, JAllocator_t>& values)
714 {
715 for (typename JContainer_t<JType_t, JAllocator_t>::const_iterator i = values.begin(); i != values.end(); ++i) {
716 __element.possibleValues.push_back(*i);
717 }
718
719 return *this;
720 }
721
722
723 /**
724 * Parsing of additional possible values.
725 *
726 * \param values possible values
727 * \return this object
728 */
729 template<template<class, class, class> class JContainer_t, class JCompare_t, class JAllocator_t>
730 JCSV& operator,(const JContainer_t<JType_t, JCompare_t, JAllocator_t>& values)
731 {
732 for (typename JContainer_t<JType_t, JCompare_t, JAllocator_t>::const_iterator i = values.begin(); i != values.end(); ++i) {
733 __element.possibleValues.push_back(*i);
734 }
735
736 return *this;
737 }
738
739 private:
741 };
742
743
744 /**
745 * Template specialisation of JPARSER::JParserTemplateElement for data type without equal operator <tt>==</tt>.
746 */
747 template<class JType_t>
748 class JParserTemplateElement<JType_t, false> :
750 {
751 public:
752 /**
753 * Constructor.
754 *
755 * \param object reference to object
756 * \param name name of object
757 * \param help help of object
758 */
759 JParserTemplateElement(JType_t& object,
760 const std::string& name = "arg",
761 const std::string& help = "") :
762 JParserElementInterface(name, help),
763 object(object),
764 is_initialised(false)
765 {}
766
767
768 /**
769 * Set initialised status to true.
770 *
771 * \param value initialised object
772 * \return this object
773 */
775 {
776 setInitialiationStatus(true);
777
778 return *this;
779 }
780
781
782 /**
783 * Set initialised status to false.
784 *
785 * \param value initialised object
786 * \return this object
787 */
789 {
790 setInitialiationStatus(false);
791
792 return *this;
793 }
794
795
796 /**
797 * Assignment to a default value.
798 *
799 * \param value default value
800 * \return this object
801 */
803 {
804 object = value;
805
806 setInitialiationStatus(true);
807
808 return *this;
809 }
810
811
812 /**
813 * Get status of object.
814 *
815 * \return true if current value is ok, else false
816 */
817 virtual bool getStatus() const override
818 {
819 return getInitialisationStatus();
820 }
821
822
823 /**
824 * Get initialisation status of parameter.
825 *
826 * \return status
827 */
828 virtual bool getInitialisationStatus() const override
829 {
830 return is_initialised;
831 }
832
833
834 /**
835 * Set initialisation status of parameter.
836 *
837 * \param value initialisation status
838 */
839 virtual void setInitialiationStatus(const bool value) override
840 {
841 is_initialised = value;
842 }
843
844
845 /**
846 * Stream input.
847 *
848 * \param in input stream
849 * \return input stream
850 */
851 virtual std::istream& read(std::istream& in) override
852 {
853 if (in.peek() == EOF) {
854 THROW(JParserException, "JParser: error no data for parameter " << getName());
855 }
856
857 readObject(in, object);
858
859 if (fail(in)) {
860 THROW(JParserException, "JParser: error reading parameter " << getName());
861 }
862
863 while (isspace(in.peek())) {
864 in.get();
865 }
866
867 if (in.peek() != EOF) {
868 THROW(JParserException, "JParser: pending data after reading parameter " << getName());
869 }
870
871 setInitialiationStatus(true);
872
873 return in;
874 }
875
876
877 /**
878 * Stream output.
879 *
880 * \param out output stream
881 * \return output stream
882 */
883 virtual std::ostream& write(std::ostream& out) const override
884 {
885 return writeObject(out, object);
886 }
887
888 protected:
889 JType_t& object;
891 };
892
893
894 /**
895 * Template specialisation of JParserTemplateElement<std::string>::read to read complete line from stream input.
896 *
897 * \param in input stream
898 * \return input stream
899 */
900 template<>
901 inline std::istream& JParserTemplateElement<std::string, false>::read(std::istream& in)
902 {
903 std::getline(in, object);
904
905 if (fail(in)) {
906 THROW(JParserException, "JParser: error reading parameter " << getName());
907 }
908
909 if (in.peek() != EOF) {
910 THROW(JParserException, "JParser: pending data after reading parameter " << getName());
911 }
912
913 setInitialiationStatus(true);
914
915 return in;
916 }
917
918
919 /**
920 * Template specialisation of JParserTemplateElement<std::string>::write to surround text with quotes.
921 *
922 * \param out output stream
923 * \return output stream
924 */
925 template<>
926 inline std::ostream& JParserTemplateElement<std::string, false>::write(std::ostream& out) const
927 {
928 return writeObject(out, JLANG::double_quote(object));
929 }
930
931
932 /**
933 * Auxiliary class for handling I/O of TString depending on its existence.\n
934 * The result is identical to that of std::string.
935 */
936 template<bool option = JLANG::JResolve<TString>::value>
938
939 /**
940 * Specialisation of TStringHelper if TString does not exist.
941 */
942 template<>
943 struct TStringHelper<false>
944 {
945 /**
946 * Read object from input stream.\n
947 * This method thrown an error.
948 *
949 * \param in input stream
950 * \param object object
951 * \return input stream
952 */
953 template<class T>
954 static inline std::istream& read(std::istream& in, T& object)
955 {
956 THROW(JParserException, "JParser: invalid data type TString (include TString.h before JParser.hh)");
957 }
958
959 /**
960 * Read std::vector of objects from input stream.\n
961 * This method thrown an error.
962 *
963 * \param in input stream
964 * \param object object
965 * \return input stream
966 */
967 template<class T>
968 static inline std::istream& read(std::istream& in, std::vector<T>& object)
969 {
970 THROW(JParserException, "JParser: invalid data type TString (include TString.h before JParser.hh)");
971 }
972 };
973
974 /**
975 * Specialisation of TStringHelper if TString exists.
976 */
977 template<>
978 struct TStringHelper<true>
979 {
980 /**
981 * Read object from input stream.
982 *
983 * \param in input stream
984 * \param object object
985 * \return input stream
986 */
987 template<class T>
988 static inline std::istream& read(std::istream& in, T& object)
989 {
990 return object.ReadLine(in);
991 }
992
993 /**
994 * Read std::vector of objects from input stream.
995 *
996 * \param in input stream
997 * \param object object
998 * \return input stream
999 */
1000 template<class T>
1001 static inline std::istream& read(std::istream& in, std::vector<T>& object)
1002 {
1003 for (std::string buffer; in >> buffer; ) {
1004 object.push_back(buffer.c_str());
1005 }
1006
1007 return in;
1008 }
1009 };
1010
1011
1012 /**
1013 * Template specialisation of JParserTemplateElement<TString>::read to read complete line from stream input.
1014 *
1015 * \param in input stream
1016 * \return input stream
1017 */
1018 template<>
1019 inline std::istream& JParserTemplateElement<TString, false>::read(std::istream& in)
1020 {
1021 TStringHelper<>::read(in, object);
1022
1023 setInitialiationStatus(true);
1024
1025 return in;
1026 }
1027
1028
1029 /**
1030 * Template specialisation of JParserTemplateElement< std::vector<TString> >::read to read tokens from stream input.
1031 *
1032 * \param in input stream
1033 * \return input stream
1034 */
1035 template<>
1036 inline std::istream& JParserTemplateElement<std::vector<TString>, false>::read(std::istream& in)
1037 {
1038 TStringHelper<>::read(in, object);
1039
1040 setInitialiationStatus(true);
1041
1042 return in;
1043 }
1044
1045
1046 /**
1047 * Template specialisation of JPARSER::JParserTemplateElement for data type with equal operator <tt>==</tt>.
1048 */
1049 template<class JType_t>
1050 class JParserTemplateElement<JType_t, true> :
1051 public JParserTemplateElement<JType_t, false>
1052 {
1053 public:
1054
1055 friend class JCSV<JType_t>;
1056
1057 /**
1058 * Constructor.
1059 *
1060 * \param object reference to object
1061 * \param name name of object
1062 * \param help help of object
1063 */
1064 JParserTemplateElement(JType_t& object,
1065 const std::string& name = "arg",
1066 const std::string& help = "") :
1067 JParserTemplateElement<JType_t, false>(object, name, help)
1068 {}
1069
1070
1071 /**
1072 * Constructor.
1073 *
1074 * \param object reference to object
1075 * \param name name of object
1076 * \param __begin begin of possible values
1077 * \param __end end of possible values
1078 */
1079 template<class T>
1080 JParserTemplateElement(JType_t& object,
1081 const std::string& name,
1082 T __begin,
1083 T __end) :
1084 JParserTemplateElement<JType_t, false>(object, name)
1085 {
1086 setPossibleValues(__begin, __end);
1087 }
1088
1089
1090 /**
1091 * Constructor.
1092 *
1093 * \param object reference to object
1094 * \param name name of object
1095 * \param help help of object
1096 * \param __begin begin of possible values
1097 * \param __end end of possible values
1098 */
1099 template<class T>
1100 JParserTemplateElement(JType_t& object,
1101 const std::string& name,
1102 const std::string& help,
1103 T __begin,
1104 T __end) :
1105 JParserTemplateElement<JType_t, false>(object, name, help)
1106 {
1107 setPossibleValues(__begin, __end);
1108 }
1109
1110
1111 /**
1112 * Set initialised status to true.
1113 *
1114 * \param value initialised object
1115 * \return this object
1116 */
1118 {
1119 this->setInitialiationStatus(true);
1120
1121 return *this;
1122 }
1123
1124
1125 /**
1126 * Set initialised status to false.
1127 *
1128 * \param value initialised object
1129 * \return this object
1130 */
1132 {
1133 this->setInitialiationStatus(false);
1134
1135 return *this;
1136 }
1137
1138
1139 /**
1140 * Assignment to a default value and possible other values.
1141 *
1142 * \param value default value
1143 * \return comma separated values parser
1144 */
1145 JCSV<JType_t> operator=(const JType_t& value)
1146 {
1147 this->object = value;
1148
1149 this->setInitialiationStatus(true);
1150
1151 possibleValues.push_back(value);
1152
1153 return JCSV<JType_t>(*this);
1154 }
1155
1156
1157 /**
1158 * Assignment to a default value and possible other values.
1159 *
1160 * \param values default values
1161 * \return this object
1162 */
1163 template<template<class, class> class JContainer_t, class JAllocator_t>
1164 JCSV<JType_t> operator=(const JContainer_t<JType_t, JAllocator_t>& values)
1165 {
1166 setPossibleValues(values.begin(), values.end());
1167
1168 return JCSV<JType_t>(*this);
1169 }
1170
1171
1172 /**
1173 * Assignment to a default value and possible other values.
1174 *
1175 * \param values default values
1176 * \return this object
1177 */
1178 template<template<class, class, class> class JContainer_t, class JCompare_t, class JAllocator_t>
1179 JCSV<JType_t> operator=(const JContainer_t<JType_t, JCompare_t, JAllocator_t>& values)
1180 {
1181 setPossibleValues(values.begin(), values.end());
1182
1183 return JCSV<JType_t>(*this);
1184 }
1185
1186
1187 /**
1188 * Get status of object.
1189 *
1190 * If more than one possible values are provided,
1191 * the current value should be equal to one of the possible values,
1192 * else a value should have been set by the user.
1193 *
1194 * \return true if current value is ok, else false
1195 */
1196 virtual bool getStatus() const override
1197 {
1198 if (possibleValues.size() > 1) {
1199
1200 for (typename std::vector<JType_t>::const_iterator i = possibleValues.begin(); i != possibleValues.end(); ++i) {
1201 if (this->object == *i) {
1202 return true;
1203 }
1204 }
1205
1206 return false;
1207
1208 } else {
1209
1210 return this->getInitialisationStatus();
1211 }
1212 }
1213
1214
1215 /**
1216 * Print.
1217 *
1218 * \param out output stream
1219 */
1220 virtual void print(std::ostream& out) const override
1221 {
1223
1224 if (possibleValues.size() > 1 && getLongprint(out)) {
1225 writeArray(out, " [", "]", ", ", possibleValues.begin(), possibleValues.end());
1226 }
1227 }
1228
1229 protected:
1230 /**
1231 * Set possible values.
1232 *
1233 * \param __begin begin of possible values
1234 * \param __end end of possible values
1235 */
1236 template<class T>
1237 void setPossibleValues(T __begin, T __end)
1238 {
1239 if (__begin != __end) {
1240
1241 this->object = *__begin;
1242
1243 this->setInitialiationStatus(true);
1244
1245 for (T i = __begin; i != __end; ++i) {
1246 possibleValues.push_back(*i);
1247 }
1248 }
1249 }
1250
1252 };
1253
1254
1255 /**
1256 * Template specialisation of JPARSER::JParserTemplateElement for type <tt>bool</tt>.
1257 * The value is by default set to false and set to true in method read() without reading any data.
1258 * This makes it possible to parse mutiple options in one go (e.g.\ <tt>-abc</tt>).
1259 * This class implements the JPARSER::JParserElementInterface interface.
1260 */
1261 template<>
1264 {
1265 public:
1266 /**
1267 * Constructor.
1268 *
1269 * The constructor assigns the default value false to the referenced parameter.
1270 *
1271 * \param object reference to object
1272 * \param name name of object
1273 * \param help help of object
1274 */
1275 JParserTemplateElement(bool& object, const std::string& name = "arg", const std::string& help = "") :
1276 JParserElementInterface(name, help),
1277 object(object)
1278 {
1279 this->object = false;
1280 }
1281
1282
1283 /**
1284 * Stream input.
1285 * This method sets the value to true, without reading any data.
1286 *
1287 * \param in input stream
1288 * \return input stream
1289 */
1290 virtual std::istream& read(std::istream& in) override
1291 {
1292 this->object = true;
1293
1294 return in;
1295 }
1296
1297
1298 /**
1299 * Stream output.
1300 *
1301 * \param out output stream
1302 * \return output stream
1303 */
1304 virtual std::ostream& write(std::ostream& out) const override
1305 {
1306 return out << object;
1307 }
1308
1309
1310 /**
1311 * Status of object.
1312 *
1313 * \return true
1314 */
1315 virtual bool getStatus() const override
1316 {
1317 return true;
1318 }
1319
1320
1321 /**
1322 * Get initialisation status of parameter.
1323 *
1324 * \return true
1325 */
1326 virtual bool getInitialisationStatus() const override
1327 {
1328 return true;
1329 }
1330
1331
1332 /**
1333 * Set initialisation status of parameter.
1334 * This implementation doesn't do anything.
1335 *
1336 * \param value initialisation status
1337 */
1338 virtual void setInitialiationStatus(const bool value) override
1339 {}
1340
1341
1342 /**
1343 * Read counter.
1344 *
1345 * \return true if at least one character to be read; else false
1346 */
1347 virtual bool gcount() const override
1348 {
1349 return false;
1350 }
1351
1352
1353 private:
1354 bool& object;
1355 };
1356
1357
1358 /**
1359 * Template specialisation of JPARSER::JParserTemplateElement for type <tt>JCounter</tt>.
1360 * The value is by default set to zero and set incremented in method read() without reading any data.
1361 * This makes it possible to parse mutiple options in one go (e.g.\ <tt>-aaa</tt>).
1362 * This class implements the JPARSER::JParserElementInterface interface.
1363 */
1364 template<>
1367 {
1368 public:
1369 /**
1370 * Constructor.
1371 *
1372 * The constructor assigns the default value false to the referenced parameter.
1373 *
1374 * \param object reference to object
1375 * \param name name of object
1376 * \param help help of object
1377 */
1378 JParserTemplateElement(JCounter& object, const std::string& name, const std::string& help = "") :
1379 JParserElementInterface(name, help),
1380 object(object)
1381 {
1382 this->object = JCounter();
1383 }
1384
1385
1386 /**
1387 * Stream input.
1388 * This method sets the value to true, without reading any data.
1389 *
1390 * \param in input stream
1391 * \return input stream
1392 */
1393 virtual std::istream& read(std::istream& in) override
1394 {
1395 return in >> object;
1396 }
1397
1398
1399 /**
1400 * Stream output.
1401 *
1402 * \param out output stream
1403 * \return output stream
1404 */
1405 virtual std::ostream& write(std::ostream& out) const override
1406 {
1407 return out << object;
1408 }
1409
1410
1411 /**
1412 * Status of object.
1413 *
1414 * \return true
1415 */
1416 virtual bool getStatus() const override
1417 {
1418 return true;
1419 }
1420
1421
1422 /**
1423 * Get initialisation status of parameter.
1424 *
1425 * \return true
1426 */
1427 virtual bool getInitialisationStatus() const override
1428 {
1429 return true;
1430 }
1431
1432
1433 /**
1434 * Set initialisation status of parameter.
1435 * This implementation doesn't do anything.
1436 *
1437 * \param value initialisation status
1438 */
1439 virtual void setInitialiationStatus(const bool value) override
1440 {}
1441
1442
1443 /**
1444 * Read counter.
1445 *
1446 * \return true if at least one character to be read; else false
1447 */
1448 virtual bool gcount() const override
1449 {
1450 return false;
1451 }
1452
1453
1454 private:
1456 };
1457
1458
1459 /**
1460 * Auxiliary class to handle pointer to JPARSER::JParserElementInterface.
1461 */
1463 public JSharedPointer<JParserElementInterface>
1464 {
1465 public:
1466
1468
1469
1470 /**
1471 * Default constructor.
1472 */
1476
1477
1478 /**
1479 * Copy constructor.
1480 *
1481 * \param value reference to JParserElement
1482 */
1484 JSharedPointer_t(static_cast<const JSharedPointer_t&>(value))
1485 {}
1486
1487
1488 /**
1489 * Assignment operator.
1490 *
1491 * \param value reference to JParserElement
1492 * \return this JParserElement
1493 */
1495 {
1496 JSharedPointer_t::operator=(static_cast<JSharedPointer_t&>(value));
1497
1498 return *this;
1499 }
1500
1501
1502 /**
1503 * Assignment operator.
1504 *
1505 * \param value reference to unnamed data object
1506 * \return corresponding new JParserTemplateElement object
1507 */
1508 template<class JType_t>
1510 {
1512
1513 reset(__p);
1514
1515 return *__p;
1516 }
1517
1518
1519 /**
1520 * Assignment operator.
1521 *
1522 * \param value reference to JParserTemplateElement object
1523 * \return corresponding new JParserTemplateElement object
1524 */
1525 template<class JType_t>
1534
1535
1536 /**
1537 * Assignment operator.
1538 *
1539 * \param value reference to a corresponding JCSV object
1540 * \return corresponding new comma separated values parser
1541 */
1542 template<class JType_t>
1544 {
1546
1547 reset(__p);
1548
1549 return JCSV<JType_t>(*__p);
1550 }
1551
1552
1553 /**
1554 * Set initialised status to true.
1555 *
1556 * \param value initialised object
1557 * \return this object
1558 */
1560 {
1561 if (!is_valid())
1562 THROW(JParserException, "No parser object defined.");
1563 else
1564 (*this)->setInitialiationStatus(true);
1565
1566 return *this;
1567 }
1568
1569
1570 /**
1571 * Set initialised status to false.
1572 *
1573 * \param value initialised object
1574 * \return this object
1575 */
1577 {
1578 (*this)->setInitialiationStatus(false);
1579
1580 return *this;
1581 }
1582
1583
1584 /**
1585 * Stream input.
1586 *
1587 * \param in input stream
1588 * \param value parser element
1589 * \return input stream
1590 */
1591 friend inline std::istream& operator>>(std::istream& in, JParserElement& value)
1592 {
1593 if (value.is_valid())
1594 return value->read(in);
1595 else
1596 return in;
1597 }
1598
1599
1600 /**
1601 * Stream output.
1602 *
1603 * \param out output stream
1604 * \param value parser element
1605 * \return output stream
1606 */
1607 friend inline std::ostream& operator<<(std::ostream& out, const JParserElement& value)
1608 {
1609 if (value.is_valid())
1610 return value->write(out);
1611 else
1612 return out;
1613 }
1614
1615
1616 /**
1617 * Print.
1618 *
1619 * \param out output stream
1620 */
1621 void print(std::ostream& out) const
1622 {
1623 if (is_valid()) {
1624 return get()->print(out);
1625 }
1626 }
1627 };
1628
1629
1630 /**
1631 * Utility class to parse command line options.
1632 *
1633 * The mapping between a parameter (of any type) and a unique option
1634 * has to be defined in the user's application, e.g.
1635 * \code{.cpp}
1636
1637 #include "Jeep/JParser.hh"
1638
1639 int main(int argc, char**argv)
1640 {
1641 int aap;
1642 bool noot;
1643 bool mies;
1644
1645 try {
1646
1647 JParser<> zap;
1648
1649 zap['i'] = make_field(aap) = 123; // set default value
1650 zap['b'] = make_field(noot); // default is false
1651 zap['B'] = make_field(mies);
1652
1653 zap(argc, argv);
1654 }
1655 catch(const std::exception& error) {
1656 cerr << error.what() << endl;
1657 return 1;
1658 }
1659 }
1660 \endcode
1661 *
1662 * The behaviour of the parser is different for parameters of type <tt>bool</tt>.
1663 * By default, its value is set to <tt>false</tt>; it is set to <tt>true</tt>
1664 * when the corresponding option is parsed.
1665 * This implies that no data are read and that several options can be
1666 * parsed in sequence without repeating the '-' symbol.
1667 *
1668 *
1669 * The syntax for the command line is:
1670 *
1671 \verbatim
1672 program [-<option> <value> [-<option> <value>]]
1673 \endverbatim
1674
1675 \verbatim
1676 program -h
1677 \endverbatim
1678 * will print the usage specification including all existing options.
1679
1680 \verbatim
1681 program --!
1682 \endverbatim
1683 * will terminate the parsing of options and print the actual setting of all options.
1684 *
1685 * After the command line has been parsed, it is checked whether
1686 * each parameter has been assigned a value by default or at run time.
1687 * If not, an exception is thrown.
1688 *
1689 * For a comparison between the parsing of command line options with JParser and boost,
1690 * see <a href="https://drive.google.com/file/d/1R62NPhYjc_R7FYTRu-tkgUEo4PGoJc_y/view?usp=sharing">internal note</a>
1691 * which is accessible for anyone with a %KM3NeT account.
1692 */
1693 template<class JKey_t = char>
1694 class JParser :
1695 public std::map<JKey_t, JParserElement>,
1696 public JMessage< JParser<JKey_t> >
1697 {
1698 public:
1699
1701 typedef JKey_t key_type;
1703
1704 typedef typename map_type::iterator iterator;
1705 typedef typename map_type::const_iterator const_iterator;
1706
1708
1709
1710 /**
1711 * Default constructor.
1712 *
1713 * \param debug debug level
1714 */
1715 JParser(const int debug = 0) :
1716 help ()
1717 {
1718 this->debug = debug;
1719 }
1720
1721
1722 /**
1723 * Constructor.
1724 *
1725 * \param message message printed with option <tt>-h</tt>
1726 * \param debug debug level
1727 */
1728 JParser(const std::string& message,
1729 const int debug = 0) :
1730 help (message)
1731 {
1732 this->debug = debug;
1733 }
1734
1735
1736 /**
1737 * Join parser.
1738 *
1739 * \param parser parser
1740 * \return this parser
1741 */
1742 JParser& join(const JParser& parser)
1743 {
1744 this->insert(parser.begin(), parser.end());
1745
1746 return *this;
1747 }
1748
1749
1750 /**
1751 * Print the possible command line options.
1752 *
1753 * \param out output stream
1754 */
1755 void print(std::ostream& out) const
1756 {
1757 using namespace std;
1758 using namespace JPP;
1759
1760 if (help != "") {
1761 out << help << endl;
1762 }
1763
1764 out << "usage: " << getFilename(pid) << endl;
1765
1766 out << ' ' << START_OF_OPTION << HELP_OPTION << ' ' << " \"help\"" << endl;
1767 out << ' ' << START_OF_OPTION << HELP_OPTION << PRINT_OPTION << " \"help with print of default and possible values\"" << endl;
1768 out << ' ' << START_OF_OPTION << REVISION_OPTION << ' ' << " \"print revision\"" << endl;
1769 out << ' ' << START_OF_OPTION << END_OF_OPTIONS << ' ' << " \"end of options; remainder will be discarded\"" << endl;
1770 out << ' ' << START_OF_OPTION << END_OF_OPTIONS << PRINT_OPTION << " \"end of options with print of actual values\"" << endl;
1771
1772 for (const_iterator i = this->begin(); i != this->end(); ++i) {
1773
1774 out << ' ' << START_OF_OPTION << i->first << " ";
1775
1776 i->second.print(out);
1777
1778 out << endl;
1779 }
1780
1781 if (getURL() != "") {
1782 out << endl << "See also: " << getURL() << '#' << getFilename(pid) << endl;
1783 }
1784 }
1785
1786
1787 /**
1788 * Terminate.
1789 *
1790 * \param status exit status
1791 */
1792 virtual void terminate(const int status)
1793 {
1794 exit(status);
1795 }
1796
1797
1798 /**
1799 * Parse the program's command line options.
1800 *
1801 * \param argc number of arguments
1802 * \param argv argument list
1803 * \return argument list
1804 */
1805 JArgs operator()(const int argc, const char* const argv[])
1806 {
1807 return (*this)(JArgs(argc, argv));
1808 }
1809
1810
1811 /**
1812 * Parse the program's command line options.
1813 *
1814 * \param args argument list
1815 * \return argument list
1816 */
1818 {
1819 using namespace std;
1820 using namespace JLANG;
1821 using namespace JEEP;
1822
1823 pid = args.PID;
1824
1825 istringstream is;
1826
1827 // argument passing
1828
1829 for (JArgs::const_iterator i = args.begin(); i != args.end(); ++i) {
1830
1831 DEBUG("Processing option <" << *i << ">" << endl);
1832
1833 is.clear();
1834 is.str(*i);
1835
1836 for (int c; (c = is.get()) != EOF; ) {
1837
1838 if (c == START_OF_OPTION) {
1839
1840 if (is.peek() == EOF) { // end-of-file
1841
1842 THROW(JParserException, "stray " << START_OF_OPTION << " in <" << is.str() << "> at " << JArgs("", i, args.end()));
1843
1844 } else if (isspace(is.peek())) { // white space
1845
1846 THROW(JParserException, "stray " << START_OF_OPTION << " in <" << is.str() << "> at " << JArgs("", i, args.end()));;
1847 }
1848
1849 while (is.peek() != EOF) { // read option(s)
1850
1851 if (is.peek() == HELP_OPTION) { // help
1852
1853 is.get();
1854
1855 setPrintOption(cout, SHORT_PRINT);
1856
1857 if (is.peek() != EOF) {
1858
1859 if (is.get() == PRINT_OPTION) {
1860
1861 setPrintOption(cout, LONG_PRINT);
1862
1863 } else {
1864
1865 THROW(JParserException, "invalid option at <" << is.str() << ">");
1866 }
1867 }
1868
1869 print(cout);
1870
1872
1873 return JArgs();
1874
1875 } else if (is.peek() == REVISION_OPTION) { // revision
1876
1877 cout << "source: " << getSource() << endl;
1878 cout << "version: " << getGITVersion() << endl;
1879 cout << "commit: " << getGITCommit() << endl;
1880 cout << "date: " << getGITDate() << endl;
1881 cout << "namespace: " << getNamespace() << endl;
1882
1884
1885 return JArgs();
1886
1887 } else if (is.peek() == END_OF_OPTIONS) { // end of options
1888
1889 is.get();
1890
1891 if (is.peek() != EOF) {
1892
1893 c = is.get();
1894
1895 if (c == PRINT_OPTION) {
1896
1897 setPrintOption(cout, MEDIUM_PRINT);
1898
1899 print(cout);
1900
1901 } else if (c == PID_OPTION) {
1902
1903 if (is.peek() == EOF && i + 1 != args.end()) {
1904 is.clear();
1905 is.str(*++i);
1906 }
1907
1908 string file_name;
1909
1910 getline(is, file_name);
1911
1912 ofstream out(file_name.c_str());
1913
1914 out << getpid() << endl;
1915
1916 if (!out) {
1917 THROW(JParserException, "invalid option at <" << is.str() << ">");
1918 }
1919
1920 out.close();
1921
1922 } else {
1923
1924 THROW(JParserException, "invalid option at <" << is.str() << ">");
1925 }
1926 }
1927
1928 check_status();
1929
1930 return JArgs(pid, ++i, args.end());
1931
1932 } else {
1933
1934 key_type option;
1935
1936 is >> option;
1937
1938 iterator p = this->find(option);
1939
1940 DEBUG("Processing option <" << option << "> " << (p != this->end()) << endl);
1941
1942 if (p != this->end()) {
1943
1944 if (p->second->gcount()) {
1945
1946 if (is.peek() == EOF && i + 1 != args.end()) {
1947 is.clear();
1948 is.str(*++i);
1949 }
1950 }
1951
1952 try {
1953 is >> p->second;
1954 }
1955 catch(const exception& error) {
1956 THROW(JParserException, "read error " << error.what() << " at <" << is.str() << ">");
1957 }
1958
1959 if (fail(is)) {
1960 THROW(JParserException, "read error at <" << is.str() << ">");
1961 }
1962
1963 } else {
1964
1965 THROW(JParserException, "unknown option <" << is.str() << "> at " << JArgs("", i, args.end()));
1966 }
1967 }
1968 }
1969
1970 } else {
1971
1972 THROW(JParserException, "illegal character <" << (char) c << "> at " << JArgs("", i, args.end()));
1973 }
1974 }
1975 }
1976
1977 check_status();
1978
1979 return JArgs();
1980 }
1981
1982
1983 /**
1984 * Parse the program's command line options.\n
1985 * This method is maintained for backward compatibility and will be deprecated.
1986 *
1987 * \param argc number of arguments
1988 * \param argv argument list
1989 * \return 0
1990 */
1991 int read(const int argc, const char* const argv[])
1992 {
1993 (*this)(argc, argv);
1994
1995 return 0;
1996 }
1997
1998
1999 /**
2000 * Parse the program's command line options.\n
2001 * This method is maintained for backward compatibility and will be deprecated.
2002 *
2003 * \param args argument list
2004 * \return 0
2005 */
2006 int read(const JArgs& args)
2007 {
2008 (*this)(args);
2009
2010 return 0;
2011 }
2012
2013
2014 /**
2015 * Print the current parameter values.
2016 *
2017 * \param out output stream
2018 * \return output stream
2019 */
2020 std::ostream& write(std::ostream& out) const
2021 {
2022 for (const_iterator i = this->begin(); i != this->end(); ++i) {
2023 out << i->second->getName() << '=' << i->second << std::endl;
2024 }
2025
2026 return out;
2027 }
2028
2029
2030 /**
2031 * Stream output.
2032 *
2033 * \param out output stream
2034 * \param parser parser
2035 * \return output stream
2036 */
2037 friend inline std::ostream& operator<<(std::ostream& out, const JParser<key_type>& parser)
2038 {
2039 return parser.write(out);
2040 }
2041
2042
2043 protected:
2044 /**
2045 * Check if all required options have been set.\n
2046 * This method throws an exception in case of a non-compliance.
2047 */
2048 void check_status() const
2049 {
2050 for (const_iterator p = this->begin(); p != this->end(); ++p) {
2051
2052 if (!p->second->getInitialisationStatus()) {
2053 THROW(JParserException, pid << " option: " << START_OF_OPTION << p->first << " <" << p->second->getName() << ">" << " has no value");
2054 }
2055
2056 if (!p->second->getStatus()) {
2057 THROW(JParserException, pid << " option: " << START_OF_OPTION << p->first << " <" << p->second->getName() << ">" << " has illegal value");
2058 }
2059 }
2060 }
2061
2062 std::string help; //!< help message
2063 std::string pid; //!< process name
2064 };
2065
2066
2067 /**
2068 * Auxiliary method for creation of template parser element object
2069 *
2070 * \param object object
2071 * \param name name of object
2072 * \param help help of object
2073 * \return parser element
2074 */
2075 template<class JType_t>
2076 inline JParserTemplateElement<JType_t> getOption(JType_t& object, const std::string& name, const std::string& help = "")
2077 {
2078 return JParserTemplateElement<JType_t>(object, name, help);
2079 }
2080
2081
2082 /**
2083 * Auxiliary method for creation of template parser element object
2084 *
2085 * \param object object
2086 * \param name name of object
2087 * \param help help of object
2088 * \param __begin begin of possible values
2089 * \param __end end of possible values
2090 * \return parser element
2091 */
2092 template<class JType_t, class T>
2094 const std::string& name,
2095 const std::string& help,
2096 T __begin,
2097 T __end)
2098 {
2099 return JParserTemplateElement<JType_t>(object, name, help, __begin, __end);
2100 }
2101
2102
2103 /**
2104 * Auxiliary method for creation of template parser element object
2105 *
2106 * \param object object
2107 * \param name name of object
2108 * \param __begin begin of possible values
2109 * \param __end end of possible values
2110 * \return parser element
2111 */
2112 template<class JType_t, class T>
2114 const std::string& name,
2115 T __begin,
2116 T __end)
2117 {
2118 return getOption(object, name, "", __begin, __end);
2119 }
2120}
2121
2122
2123/**
2124 * Make string for variadic macro.
2125 *
2126 * When called,
2127 * - first argument should correspond to a dummy value;
2128 * - second to the ##__VA_ARGS__ macro; and
2129 * - third to the fall back value (e.g.\ "");
2130 *
2131 * \param A dummy value
2132 * \param B ##__VA_ARGS__ macro
2133 * \return std::string
2134 */
2135#define VARGS_STRING(A, B, ...) std::invoke( [&]() { std::ostringstream out; out << B; return out.str(); } )
2136
2137/**
2138 * macro to convert parameter to JParserTemplateElement object
2139 */
2140#define make_field(A, ...) JPARSER::getOption(A, #A, VARGS_STRING("", ##__VA_ARGS__, ""))
2141
2142
2143/**
2144 * macro to convert parameter to JParserTemplateElement object
2145 */
2146#define make_option(A, ...) JPARSER::getOption(A, #A, VARGS_STRING("", ##__VA_ARGS__, ""))
2147
2148
2149using JPARSER::JParser;
2150using JPARSER::JProxy;
2151using JPARSER::JCounter;
2152using JPARSER::getOption;
2153using JPARSER::JArgs;
2154
2155#endif
Exceptions.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
I/O manipulators.
bool getShortprint(std::ostream &out)
Get short print option.
Definition JManip.hh:75
bool getLongprint(std::ostream &out)
Get long print option.
Definition JManip.hh:121
void setPrintOption(std::ostream &out, const int option)
Set print option.
Definition JManip.hh:63
General purpose messaging.
#define DEBUG(A)
Message macros.
Definition JMessage.hh:62
I/O formatting auxiliaries.
Auxiliary methods for handling file names, type names and environment.
Jpp environment information.
Data structure to store command line arguments.
Definition JArgs.hh:26
bool is_valid() const
Check validity of pointer.
Template definition of test availability of comparison operators.
Exception when parsing a value.
virtual JClass_t * get() const override
Get pointer.
Definition JPointer.hh:64
JClass_t * __p
pointer to object
Definition JPointer.hh:136
The template JSharedPointer class can be used to share a pointer to an object.
virtual void reset() override
Reset pointer.
JSharedPointer & operator=(const JSharedPointer &object)
Assignment operator.
Interface for ASCII input using standard streams.
Interface for ASCII output using standard streams.
virtual std::ostream & write(std::ostream &out) const =0
Stream output.
Auxiliary class to assign the remainder of a sequence of Comma Separated Values.
Definition JParser.hh:669
JCSV & operator,(const JContainer_t< JType_t, JCompare_t, JAllocator_t > &values)
Parsing of additional possible values.
Definition JParser.hh:730
JCSV(JParserTemplateElement< JType_t > &element)
Constructor.
Definition JParser.hh:676
JCSV & operator,(const JContainer_t< JType_t, JAllocator_t > &values)
Parsing of additional possible values.
Definition JParser.hh:713
JParserTemplateElement< JType_t > & __element
Definition JParser.hh:740
JCSV & operator,(JType_t value)
Parsing of additional possible values.
Definition JParser.hh:698
Auxiliary class to handle multiple boolean-like I/O.
Definition JParser.hh:422
bool more(const int value) const
Compare value.
Definition JParser.hh:473
virtual bool getStatus() const override
Get status of this counter.
Definition JParser.hh:437
const JCounter & operator=(const bool value)
Set value.
Definition JParser.hh:498
JCounter()
Default constructor.
Definition JParser.hh:427
friend std::istream & operator>>(std::istream &in, JCounter &object)
Read option from input.
Definition JParser.hh:515
bool less(const int value) const
Compare value.
Definition JParser.hh:461
bool less(const JCounter &value) const
Compare value.
Definition JParser.hh:449
friend std::ostream & operator<<(std::ostream &out, const JCounter &object)
Write options to output.
Definition JParser.hh:530
int getCounter() const
Get counter.
Definition JParser.hh:484
Interface for I/O of parser element.
Definition JParser.hh:548
virtual bool gcount() const
Read counter.
Definition JParser.hh:645
virtual void setInitialiationStatus(const bool value)=0
Set initialisation status of parameter.
virtual bool getStatus() const =0
Get status of parameter.
virtual void print(std::ostream &out) const
Print.
Definition JParser.hh:615
JParserElementInterface(const std::string &name="arg", const std::string &help="")
Constructor.
Definition JParser.hh:556
const std::string & getName() const
Get name of parameter.
Definition JParser.hh:569
const std::string & getHelp() const
Get help of parameter.
Definition JParser.hh:580
virtual bool getInitialisationStatus() const =0
Get initialisation status of parameter.
Auxiliary class to handle pointer to JPARSER::JParserElementInterface.
Definition JParser.hh:1464
const JParserElement & operator=(const not_initialised &value)
Set initialised status to false.
Definition JParser.hh:1576
JParserElement(const JParserElement &value)
Copy constructor.
Definition JParser.hh:1483
JParserTemplateElement< JType_t > & operator=(JType_t &value)
Assignment operator.
Definition JParser.hh:1509
void print(std::ostream &out) const
Print.
Definition JParser.hh:1621
JParserTemplateElement< JType_t > & operator=(const JParserTemplateElement< JType_t > &value)
Assignment operator.
Definition JParser.hh:1526
friend std::istream & operator>>(std::istream &in, JParserElement &value)
Stream input.
Definition JParser.hh:1591
JSharedPointer< JParserElementInterface > JSharedPointer_t
Definition JParser.hh:1467
const JParserElement & operator=(const initialised &value)
Set initialised status to true.
Definition JParser.hh:1559
friend std::ostream & operator<<(std::ostream &out, const JParserElement &value)
Stream output.
Definition JParser.hh:1607
JParserElement & operator=(JParserElement &value)
Assignment operator.
Definition JParser.hh:1494
JParserElement()
Default constructor.
Definition JParser.hh:1473
JCSV< JType_t > operator=(const JCSV< JType_t > &value)
Assignment operator.
Definition JParser.hh:1543
JParserTemplateElement(JCounter &object, const std::string &name, const std::string &help="")
Constructor.
Definition JParser.hh:1378
virtual void setInitialiationStatus(const bool value) override
Set initialisation status of parameter.
Definition JParser.hh:1439
virtual bool gcount() const override
Read counter.
Definition JParser.hh:1448
virtual std::istream & read(std::istream &in) override
Stream input.
Definition JParser.hh:1393
virtual bool getInitialisationStatus() const override
Get initialisation status of parameter.
Definition JParser.hh:1427
virtual std::ostream & write(std::ostream &out) const override
Stream output.
Definition JParser.hh:1405
virtual bool getStatus() const override
Status of object.
Definition JParser.hh:1416
virtual bool getInitialisationStatus() const override
Get initialisation status of parameter.
Definition JParser.hh:828
const JParserTemplateElement< JType_t, false > & operator=(const JType_t &value)
Assignment to a default value.
Definition JParser.hh:802
JParserTemplateElement(JType_t &object, const std::string &name="arg", const std::string &help="")
Constructor.
Definition JParser.hh:759
virtual bool getStatus() const override
Get status of object.
Definition JParser.hh:817
virtual void setInitialiationStatus(const bool value) override
Set initialisation status of parameter.
Definition JParser.hh:839
const JParserTemplateElement< JType_t, false > & operator=(const initialised &value)
Set initialised status to true.
Definition JParser.hh:774
const JParserTemplateElement< JType_t, false > & operator=(const not_initialised &value)
Set initialised status to false.
Definition JParser.hh:788
virtual std::istream & read(std::istream &in) override
Stream input.
Definition JParser.hh:851
virtual std::ostream & write(std::ostream &out) const override
Stream output.
Definition JParser.hh:883
const JParserTemplateElement< JType_t, true > & operator=(const initialised &value)
Set initialised status to true.
Definition JParser.hh:1117
JCSV< JType_t > operator=(const JType_t &value)
Assignment to a default value and possible other values.
Definition JParser.hh:1145
JParserTemplateElement(JType_t &object, const std::string &name, const std::string &help, T __begin, T __end)
Constructor.
Definition JParser.hh:1100
const JParserTemplateElement< JType_t, true > & operator=(const not_initialised &value)
Set initialised status to false.
Definition JParser.hh:1131
JCSV< JType_t > operator=(const JContainer_t< JType_t, JCompare_t, JAllocator_t > &values)
Assignment to a default value and possible other values.
Definition JParser.hh:1179
virtual bool getStatus() const override
Get status of object.
Definition JParser.hh:1196
void setPossibleValues(T __begin, T __end)
Set possible values.
Definition JParser.hh:1237
virtual void print(std::ostream &out) const override
Print.
Definition JParser.hh:1220
JParserTemplateElement(JType_t &object, const std::string &name="arg", const std::string &help="")
Constructor.
Definition JParser.hh:1064
JParserTemplateElement(JType_t &object, const std::string &name, T __begin, T __end)
Constructor.
Definition JParser.hh:1080
JCSV< JType_t > operator=(const JContainer_t< JType_t, JAllocator_t > &values)
Assignment to a default value and possible other values.
Definition JParser.hh:1164
JParserTemplateElement(bool &object, const std::string &name="arg", const std::string &help="")
Constructor.
Definition JParser.hh:1275
virtual std::istream & read(std::istream &in) override
Stream input.
Definition JParser.hh:1290
virtual bool getStatus() const override
Status of object.
Definition JParser.hh:1315
virtual void setInitialiationStatus(const bool value) override
Set initialisation status of parameter.
Definition JParser.hh:1338
virtual std::ostream & write(std::ostream &out) const override
Stream output.
Definition JParser.hh:1304
virtual bool gcount() const override
Read counter.
Definition JParser.hh:1347
virtual bool getInitialisationStatus() const override
Get initialisation status of parameter.
Definition JParser.hh:1326
Template class holder for I/O of parser element.
Definition JParser.hh:662
std::ostream & write(std::ostream &out) const
Template specialisation of JParserTemplateElement<std::string>::write to surround text with quotes.
Definition JParser.hh:926
std::istream & read(std::istream &in)
Template specialisation of JParserTemplateElement<std::string>::read to read complete line from strea...
Definition JParser.hh:901
Utility class to parse command line options.
Definition JParser.hh:1697
JArgs operator()(const int argc, const char *const argv[])
Parse the program's command line options.
Definition JParser.hh:1805
int read(const int argc, const char *const argv[])
Parse the program's command line options.
Definition JParser.hh:1991
std::string pid
process name
Definition JParser.hh:2063
map_type::const_iterator const_iterator
Definition JParser.hh:1705
JParser & join(const JParser &parser)
Join parser.
Definition JParser.hh:1742
map_type::iterator iterator
Definition JParser.hh:1704
virtual void terminate(const int status)
Terminate.
Definition JParser.hh:1792
friend std::ostream & operator<<(std::ostream &out, const JParser< key_type > &parser)
Stream output.
Definition JParser.hh:2037
JLANG::JParserException JParserException
Definition JParser.hh:1700
JParser(const std::string &message, const int debug=0)
Constructor.
Definition JParser.hh:1728
void print(std::ostream &out) const
Print the possible command line options.
Definition JParser.hh:1755
void check_status() const
Check if all required options have been set.
Definition JParser.hh:2048
std::ostream & write(std::ostream &out) const
Print the current parameter values.
Definition JParser.hh:2020
JParser(const int debug=0)
Default constructor.
Definition JParser.hh:1715
std::string help
help message
Definition JParser.hh:2062
int read(const JArgs &args)
Parse the program's command line options.
Definition JParser.hh:2006
std::map< key_type, JParserElement > map_type
Definition JParser.hh:1702
JArgs operator()(const JArgs &args)
Parse the program's command line options.
Definition JParser.hh:1817
friend std::istream & operator>>(std::istream &in, JProxy &object)
Read option from input.
Definition JParser.hh:371
JProxy(const std::string &option, const T &value)
Constructor.
Definition JParser.hh:282
const T & getCustom() const
Get custom value.
Definition JParser.hh:328
JProxy & operator=(const T &value)
Assignment to a value.
Definition JParser.hh:356
const std::string & getOption() const
Get option.
Definition JParser.hh:317
friend std::ostream & operator<<(std::ostream &out, const JProxy &object)
Write options to output.
Definition JParser.hh:404
const T & getValue() const
Get actual value.
Definition JParser.hh:306
JProxy & operator=(const JProxy &object)
Assignment operator.
Definition JParser.hh:342
JProxy(const T &value)
Constructor.
Definition JParser.hh:294
JProxy(const T &value)
Constructor.
Definition JParser.hh:135
friend std::istream & operator>>(std::istream &in, JProxy &object)
Read option from input.
Definition JParser.hh:223
const T & getValue() const
Get actual value.
Definition JParser.hh:147
const T & getCustom() const
Get custom value.
Definition JParser.hh:169
JProxy & operator=(const JProxy &object)
Assignment operator.
Definition JParser.hh:194
JProxy(const std::string &option, const T &value)
Constructor.
Definition JParser.hh:123
const std::string & getOption() const
Get option.
Definition JParser.hh:158
friend std::ostream & operator<<(std::ostream &out, const JProxy &object)
Write options to output.
Definition JParser.hh:256
JProxy & operator=(const T &value)
Assignment to a value.
Definition JParser.hh:208
Auxiliary class to assign a custom value following the reading of a specific textual value.
Definition JParser.hh:107
bool read(Vec &v, std::istream &is)
Read a Vec(tor) from a stream.
Definition io_ascii.hh:142
General puprpose classes and methods.
std::ostream & writeObject(std::ostream &out, const T &object)
Stream output of object.
std::string getFilename(const std::string &file_name)
Get file name part, i.e. part after last JEEP::PATHNAME_SEPARATOR if any.
std::istream & readObject(std::istream &in, T &object)
Stream input of object.
std::ostream & writeArray(std::ostream &out, const char *left, const char *right, const char *sep, T __begin, T __end)
Write array of objects.
Auxiliary classes and methods for language specific functionality.
std::string double_quote(const std::string &value)
Quote string.
Local namespace for command line parser.
Definition JParser.hh:46
static char END_OF_OPTIONS
end of all options
Definition JParser.hh:80
bool fail(std::istream &in)
Check for stream state.
Definition JParser.hh:97
JParserTemplateElement< JType_t > getOption(JType_t &object, const std::string &name, const std::string &help="")
Auxiliary method for creation of template parser element object.
Definition JParser.hh:2076
static char REVISION_OPTION
revision option
Definition JParser.hh:82
static int NORMAL_EXIT_CODE
exit code of normal end
Definition JParser.hh:85
static char PID_OPTION
print PID to file
Definition JParser.hh:84
static char START_OF_OPTION
Parser options.
Definition JParser.hh:79
static char HELP_OPTION
help option
Definition JParser.hh:81
static char PRINT_OPTION
print option
Definition JParser.hh:83
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary class for handling debug parameter within a class.
Definition JMessage.hh:44
static int debug
debug level (default is off).
Definition JMessage.hh:45
Interface for status of object.
Template definition of auxiliary base class for comparison of data structures.
static std::istream & read(std::istream &in, T &object)
Read object from input stream.
Definition JParser.hh:954
static std::istream & read(std::istream &in, std::vector< T > &object)
Read std::vector of objects from input stream.
Definition JParser.hh:968
static std::istream & read(std::istream &in, T &object)
Read object from input stream.
Definition JParser.hh:988
Auxiliary class for handling I/O of TString depending on its existence.
Definition JParser.hh:937
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition JParser.hh:67
Empty structure for specification of parser element that is not initialised (i.e. does require input)...
Definition JParser.hh:73
Auxiliary data structure for alignment of data.
Definition JManip.hh:231