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