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