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