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