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