Jpp  17.1.1
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  */
376  JParserElementInterface(const std::string& name = "arg",
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  * \return output stream
435  */
436  virtual void print(std::ostream& out) const
437  {
438  using namespace std;
439 
440  out << "<" << getName() << ">";
441 
442  if (getStatus() && !getShortprint(out)) {
443 
444  int width = WIDTH - getName().length();
445 
446  if (width > 0) {
447  out << setw(width) << " ";
448  }
449 
450  out << " = ";
451 
452  write(out);
453  }
454 
455  if (getLongprint(out) && getHelp() != "") {
456  out << " \"" << getHelp() << "\"";
457  }
458  }
459 
460 
461  /**
462  * Read counter.
463  *
464  * \return true if at least one character to be read; else false
465  */
466  virtual bool gcount() const
467  {
468  return true;
469  }
470 
471 
472  protected:
473  std::string __name;
474  std::string __help;
475  };
476 
477 
478  /**
479  * Template class holder for I/O of parser element.
480  * This class implements the JPARSER::JParserElementInterface interface.
481  */
482  template<class JType_t, bool has_eq = JComparisonAvailable<JType_t>::has_eq>
484 
485 
486  /**
487  * Auxiliary class to assign the remainder of a sequence of Comma Separated Values.
488  */
489  template<class JType_t>
490  class JCSV {
491  public:
492  /**
493  * Constructor.
494  *
495  * \param element parser element
496  */
498  __element(element)
499  {}
500 
501 
502  /**
503  * Type conversion operator.
504  *
505  * \return parser element
506  */
507  operator const JParserTemplateElement<JType_t>&() const
508  {
509  return __element;
510  }
511 
512 
513  /**
514  * Parsing of additional possible values.
515  *
516  * \param value possible value
517  * \return this JCSV object
518  */
519  JCSV& operator,(JType_t value)
520  {
521  __element.possibleValues.push_back(value);
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 JContainer_t, class JAllocator_t>
534  JCSV& operator,(const JContainer_t<JType_t, JAllocator_t>& values)
535  {
536  for (typename JContainer_t<JType_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 
544  /**
545  * Parsing of additional possible values.
546  *
547  * \param values possible values
548  * \return this object
549  */
550  template<template<class, class, class> class JContainer_t, class JCompare_t, class JAllocator_t>
551  JCSV& operator,(const JContainer_t<JType_t, JCompare_t, JAllocator_t>& values)
552  {
553  for (typename JContainer_t<JType_t, JCompare_t, JAllocator_t>::const_iterator i = values.begin(); i != values.end(); ++i) {
554  __element.possibleValues.push_back(*i);
555  }
556 
557  return *this;
558  }
559 
560  private:
562  };
563 
564 
565  /**
566  * Template specialisation of JPARSER::JParserTemplateElement for data type without equal operator <tt>==</tt>.
567  */
568  template<class JType_t>
569  class JParserTemplateElement<JType_t, false> :
571  {
572  public:
573  /**
574  * Constructor.
575  *
576  * \param object reference to object
577  * \param name name of object
578  * \param help help of object
579  */
580  JParserTemplateElement(JType_t& object,
581  const std::string& name = "arg",
582  const std::string& help = "") :
584  object(object),
585  is_initialised(false)
586  {}
587 
588 
589  /**
590  * Set initialised status to true.
591  *
592  * \param value initialised object
593  * \return this object
594  */
596  {
598 
599  return *this;
600  }
601 
602 
603  /**
604  * Set initialised status to false.
605  *
606  * \param value initialised object
607  * \return this object
608  */
610  {
611  setInitialiationStatus(false);
612 
613  return *this;
614  }
615 
616 
617  /**
618  * Assignment to a default value.
619  *
620  * \param value default value
621  * \return this object
622  */
624  {
625  object = value;
626 
628 
629  return *this;
630  }
631 
632 
633  /**
634  * Get status of object.
635  *
636  * \return true if current value is ok, else false
637  */
638  virtual bool getStatus() const override
639  {
640  return getInitialisationStatus();
641  }
642 
643 
644  /**
645  * Get initialisation status of parameter.
646  *
647  * \return status
648  */
649  virtual bool getInitialisationStatus() const override
650  {
651  return is_initialised;
652  }
653 
654 
655  /**
656  * Set initialisation status of parameter.
657  *
658  * \param value initialisation status
659  */
660  virtual void setInitialiationStatus(const bool value) override
661  {
662  is_initialised = value;
663  }
664 
665 
666  /**
667  * Stream input.
668  *
669  * \param in input stream
670  * \return input stream
671  */
672  virtual std::istream& read(std::istream& in) override
673  {
674  if (in.peek() == EOF) {
675  THROW(JParserException, "JParser: error no data for parameter " << getName());
676  }
677 
678  readObject(in, object);
679 
680  if (fail(in)) {
681  THROW(JParserException, "JParser: error reading parameter " << getName());
682  }
683 
684  while (isspace(in.peek())) {
685  in.get();
686  }
687 
688  if (in.peek() != EOF) {
689  THROW(JParserException, "JParser: pending data after reading parameter " << getName());
690  }
691 
693 
694  return in;
695  }
696 
697 
698  /**
699  * Stream output.
700  *
701  * \param out output stream
702  * \return output stream
703  */
704  virtual std::ostream& write(std::ostream& out) const override
705  {
706  return writeObject(out, object);
707  }
708 
709  protected:
710  JType_t& object;
712  };
713 
714 
715  /**
716  * Template specialisation of JParserTemplateElement<std::string>::read to read complete line from stream input.
717  *
718  * \param in input stream
719  * \return input stream
720  */
721  template<>
722  inline std::istream& JParserTemplateElement<std::string, false>::read(std::istream& in)
723  {
724  std::getline(in, object);
725 
726  if (fail(in)) {
727  THROW(JParserException, "JParser: error reading parameter " << getName());
728  }
729 
730  if (in.peek() != EOF) {
731  THROW(JParserException, "JParser: pending data after reading parameter " << getName());
732  }
733 
735 
736  return in;
737  }
738 
739 
740  /**
741  * Template specialisation of JParserTemplateElement<std::string>::write to surround text with quotes.
742  *
743  * \param out output stream
744  * \return output stream
745  */
746  template<>
747  inline std::ostream& JParserTemplateElement<std::string, false>::write(std::ostream& out) const
748  {
749  return writeObject(out, JLANG::double_quote(object));
750  }
751 
752 
753  /**
754  * Auxiliary class for handling I/O of TString depending on its existence.\n
755  * The result is identical to that of std::string.
756  */
757  template<bool option = JLANG::JResolve<TString>::value>
759 
760  /**
761  * Specialisation of TStringHelper if TString does not exist.
762  */
763  template<>
764  struct TStringHelper<false>
765  {
766  /**
767  * Read object from input stream.\n
768  * This method thrown an error.
769  *
770  * \param in input stream
771  * \param object object
772  * \return input stream
773  */
774  template<class T>
775  static inline std::istream& read(std::istream& in, T& object)
776  {
777  THROW(JParserException, "JParser: invalid data type TString (include TString.h before JParser.hh)");
778  }
779 
780  /**
781  * Read std::vector of objects from input stream.\n
782  * This method thrown an error.
783  *
784  * \param in input stream
785  * \param object object
786  * \return input stream
787  */
788  template<class T>
789  static inline std::istream& read(std::istream& in, std::vector<T>& object)
790  {
791  THROW(JParserException, "JParser: invalid data type TString (include TString.h before JParser.hh)");
792  }
793  };
794 
795  /**
796  * Specialisation of TStringHelper if TString exists.
797  */
798  template<>
799  struct TStringHelper<true>
800  {
801  /**
802  * Read object from input stream.
803  *
804  * \param in input stream
805  * \param object object
806  * \return input stream
807  */
808  template<class T>
809  static inline std::istream& read(std::istream& in, T& object)
810  {
811  return object.ReadLine(in);
812  }
813 
814  /**
815  * Read std::vector of objects from input stream.
816  *
817  * \param in input stream
818  * \param object object
819  * \return input stream
820  */
821  template<class T>
822  static inline std::istream& read(std::istream& in, std::vector<T>& object)
823  {
824  for (std::string buffer; in >> buffer; ) {
825  object.push_back(TString(buffer.c_str()));
826  }
827 
828  return in;
829  }
830  };
831 
832 
833  /**
834  * Template specialisation of JParserTemplateElement<TString>::read to read complete line from stream input.
835  *
836  * \param in input stream
837  * \return input stream
838  */
839  template<>
840  inline std::istream& JParserTemplateElement<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 JParserTemplateElement< std::vector<TString> >::read to read tokens from stream input.
852  *
853  * \param in input stream
854  * \return input stream
855  */
856  template<>
857  inline std::istream& JParserTemplateElement<std::vector<TString>, false>::read(std::istream& in)
858  {
859  TStringHelper<>::read(in, object);
860 
862 
863  return in;
864  }
865 
866 
867  /**
868  * Template specialisation of JPARSER::JParserTemplateElement for data type with equal operator <tt>==</tt>.
869  */
870  template<class JType_t>
871  class JParserTemplateElement<JType_t, true> :
872  public JParserTemplateElement<JType_t, false>
873  {
874  public:
875 
876  friend class JCSV<JType_t>;
877 
878  /**
879  * Constructor.
880  *
881  * \param object reference to object
882  * \param name name of object
883  * \param help help of object
884  */
885  JParserTemplateElement(JType_t& object,
886  const std::string& name = "arg",
887  const std::string& help = "") :
888  JParserTemplateElement<JType_t, false>(object, name, help)
889  {}
890 
891 
892  /**
893  * Constructor.
894  *
895  * \param object reference to object
896  * \param name name of object
897  * \param __begin begin of possible values
898  * \param __end end of possible values
899  */
900  template<class T>
901  JParserTemplateElement(JType_t& object,
902  const std::string& name,
903  T __begin,
904  T __end) :
905  JParserTemplateElement<JType_t, false>(object, name)
906  {
907  setPossibleValues(__begin, __end);
908  }
909 
910 
911  /**
912  * Constructor.
913  *
914  * \param object reference to object
915  * \param name name of object
916  * \param help help of object
917  * \param __begin begin of possible values
918  * \param __end end of possible values
919  */
920  template<class T>
921  JParserTemplateElement(JType_t& object,
922  const std::string& name,
923  const std::string& help,
924  T __begin,
925  T __end) :
926  JParserTemplateElement<JType_t, false>(object, name, help)
927  {
928  setPossibleValues(__begin, __end);
929  }
930 
931 
932  /**
933  * Set initialised status to true.
934  *
935  * \param value initialised object
936  * \return this object
937  */
939  {
940  this->setInitialiationStatus(true);
941 
942  return *this;
943  }
944 
945 
946  /**
947  * Set initialised status to false.
948  *
949  * \param value initialised object
950  * \return this object
951  */
953  {
954  this->setInitialiationStatus(false);
955 
956  return *this;
957  }
958 
959 
960  /**
961  * Assignment to a default value and possible other values.
962  *
963  * \param value default value
964  * \return comma separated values parser
965  */
966  JCSV<JType_t> operator=(const JType_t& value)
967  {
968  this->object = value;
969 
970  this->setInitialiationStatus(true);
971 
972  possibleValues.push_back(value);
973 
974  return JCSV<JType_t>(*this);
975  }
976 
977 
978  /**
979  * Assignment to a default value and possible other values.
980  *
981  * \param values default values
982  * \return this object
983  */
984  template<template<class, class> class JContainer_t, class JAllocator_t>
985  JCSV<JType_t> operator=(const JContainer_t<JType_t, JAllocator_t>& values)
986  {
987  setPossibleValues(values.begin(), values.end());
988 
989  return JCSV<JType_t>(*this);
990  }
991 
992 
993  /**
994  * Assignment to a default value and possible other values.
995  *
996  * \param values default values
997  * \return this object
998  */
999  template<template<class, class, class> class JContainer_t, class JCompare_t, class JAllocator_t>
1000  JCSV<JType_t> operator=(const JContainer_t<JType_t, JCompare_t, JAllocator_t>& values)
1001  {
1002  setPossibleValues(values.begin(), values.end());
1003 
1004  return JCSV<JType_t>(*this);
1005  }
1006 
1007 
1008  /**
1009  * Get status of object.
1010  *
1011  * If more than one possible values are provided,
1012  * the current value should be equal to one of the possible values,
1013  * else a value should have been set by the user.
1014  *
1015  * \return true if current value is ok, else false
1016  */
1017  virtual bool getStatus() const override
1018  {
1019  if (possibleValues.size() > 1) {
1020 
1021  for (typename std::vector<JType_t>::const_iterator i = possibleValues.begin(); i != possibleValues.end(); ++i) {
1022  if (this->object == *i) {
1023  return true;
1024  }
1025  }
1026 
1027  return false;
1028 
1029  } else {
1030 
1031  return this->getInitialisationStatus();
1032  }
1033  }
1034 
1035 
1036  /**
1037  * Print.
1038  *
1039  * \param out output stream
1040  * \return output stream
1041  */
1042  virtual void print(std::ostream& out) const override
1043  {
1045 
1046  if (possibleValues.size() > 1 && getLongprint(out)) {
1047  writeArray(out, " [", "]", ", ", possibleValues.begin(), possibleValues.end());
1048  }
1049  }
1050 
1051  protected:
1052  /**
1053  * Set possible values.
1054  *
1055  * \param __begin begin of possible values
1056  * \param __end end of possible values
1057  */
1058  template<class T>
1059  void setPossibleValues(T __begin, T __end)
1060  {
1061  if (__begin != __end) {
1062 
1063  this->object = *__begin;
1064 
1065  this->setInitialiationStatus(true);
1066 
1067  for (T i = __begin; i != __end; ++i) {
1068  possibleValues.push_back(*i);
1069  }
1070  }
1071  }
1072 
1074  };
1075 
1076 
1077  /**
1078  * Template specialisation of JPARSER::JParserTemplateElement for type <tt>bool</tt>.
1079  * The value is by default set to false and set to true in method read() without reading any data.
1080  * This makes it possible to parse mutiple options in one go (e.g.\ <tt>-abc</tt>).
1081  * This class implements the JPARSER::JParserElementInterface interface.
1082  */
1083  template<>
1084  class JParserTemplateElement<bool> :
1085  public JParserElementInterface
1086  {
1087  public:
1088  /**
1089  * Constructor.
1090  *
1091  * The constructor assigns the default value false to the referenced parameter.
1092  *
1093  * \param object reference to object
1094  * \param name name of object
1095  * \param help help of object
1096  */
1097  JParserTemplateElement(bool& object, const std::string& name = "arg", const std::string& help = "") :
1099  object(object)
1100  {
1101  this->object = false;
1102  }
1103 
1104 
1105  /**
1106  * Stream input.
1107  * This method sets the value to true, without reading any data.
1108  *
1109  * \param in input stream
1110  * \return input stream
1111  */
1112  virtual std::istream& read(std::istream& in) override
1113  {
1114  this->object = true;
1115 
1116  return in;
1117  }
1118 
1119 
1120  /**
1121  * Stream output.
1122  *
1123  * \param out output stream
1124  * \return output stream
1125  */
1126  virtual std::ostream& write(std::ostream& out) const override
1127  {
1128  return out << object;
1129  }
1130 
1131 
1132  /**
1133  * Status of object.
1134  *
1135  * \return true
1136  */
1137  virtual bool getStatus() const override
1138  {
1139  return true;
1140  }
1141 
1142 
1143  /**
1144  * Get initialisation status of parameter.
1145  *
1146  * \return true
1147  */
1148  virtual bool getInitialisationStatus() const override
1149  {
1150  return true;
1151  }
1152 
1153 
1154  /**
1155  * Set initialisation status of parameter.
1156  * This implementation doesn't do anything.
1157  *
1158  * \param value initialisation status
1159  */
1160  virtual void setInitialiationStatus(const bool value) override
1161  {}
1162 
1163 
1164  /**
1165  * Read counter.
1166  *
1167  * \return true if at least one character to be read; else false
1168  */
1169  virtual bool gcount() const override
1170  {
1171  return false;
1172  }
1173 
1174 
1175  private:
1176  bool& object;
1177  };
1178 
1179 
1180  /**
1181  * Template specialisation of JPARSER::JParserTemplateElement for type <tt>JCounter</tt>.
1182  * The value is by default set to zero and set incremented in method read() without reading any data.
1183  * This makes it possible to parse mutiple options in one go (e.g.\ <tt>-aaa</tt>).
1184  * This class implements the JPARSER::JParserElementInterface interface.
1185  */
1186  template<>
1188  public JParserElementInterface
1189  {
1190  public:
1191  /**
1192  * Constructor.
1193  *
1194  * The constructor assigns the default value false to the referenced parameter.
1195  *
1196  * \param object reference to object
1197  * \param name name of object
1198  * \param help help of object
1199  */
1200  JParserTemplateElement(JCounter& object, const std::string& name, const std::string& help = "") :
1201  JParserElementInterface(name, help),
1202  object(object)
1203  {
1204  this->object = JCounter();
1205  }
1206 
1207 
1208  /**
1209  * Stream input.
1210  * This method sets the value to true, without reading any data.
1211  *
1212  * \param in input stream
1213  * \return input stream
1214  */
1215  virtual std::istream& read(std::istream& in) override
1216  {
1217  return in >> object;
1218  }
1219 
1220 
1221  /**
1222  * Stream output.
1223  *
1224  * \param out output stream
1225  * \return output stream
1226  */
1227  virtual std::ostream& write(std::ostream& out) const override
1228  {
1229  return out << object;
1230  }
1231 
1232 
1233  /**
1234  * Status of object.
1235  *
1236  * \return true
1237  */
1238  virtual bool getStatus() const override
1239  {
1240  return true;
1241  }
1242 
1243 
1244  /**
1245  * Get initialisation status of parameter.
1246  *
1247  * \return true
1248  */
1249  virtual bool getInitialisationStatus() const override
1250  {
1251  return true;
1252  }
1253 
1254 
1255  /**
1256  * Set initialisation status of parameter.
1257  * This implementation doesn't do anything.
1258  *
1259  * \param value initialisation status
1260  */
1261  virtual void setInitialiationStatus(const bool value) override
1262  {}
1263 
1264 
1265  /**
1266  * Read counter.
1267  *
1268  * \return true if at least one character to be read; else false
1269  */
1270  virtual bool gcount() const override
1271  {
1272  return false;
1273  }
1274 
1275 
1276  private:
1278  };
1279 
1280 
1281  /**
1282  * Auxiliary class to handle pointer to JPARSER::JParserElementInterface.
1283  */
1285  public JSharedPointer<JParserElementInterface>
1286  {
1287  public:
1288 
1290 
1291 
1292  /**
1293  * Default constructor.
1294  */
1297  {}
1298 
1299 
1300  /**
1301  * Copy constructor.
1302  *
1303  * \param value reference to JParserElement
1304  */
1306  JSharedPointer_t(static_cast<const JSharedPointer_t&>(value))
1307  {}
1308 
1309 
1310  /**
1311  * Assignment operator.
1312  *
1313  * \param value reference to JParserElement
1314  * \return this JParserElement
1315  */
1317  {
1318  JSharedPointer_t::operator=(static_cast<JSharedPointer_t&>(value));
1319 
1320  return *this;
1321  }
1322 
1323 
1324  /**
1325  * Assignment operator.
1326  *
1327  * \param value reference to unnamed data 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 JParserTemplateElement object
1345  * \return corresponding new JParserTemplateElement object
1346  */
1347  template<class JType_t>
1349  {
1351 
1352  reset(__p);
1353 
1354  return *__p;
1355  }
1356 
1357 
1358  /**
1359  * Assignment operator.
1360  *
1361  * \param value reference to a corresponding JCSV object
1362  * \return corresponding new comma separated values parser
1363  */
1364  template<class JType_t>
1366  {
1368 
1369  reset(__p);
1370 
1371  return JCSV<JType_t>(*__p);
1372  }
1373 
1374 
1375  /**
1376  * Set initialised status to true.
1377  *
1378  * \param value initialised object
1379  * \return this object
1380  */
1381  const JParserElement& operator=(const initialised& value)
1382  {
1383  if (!is_valid())
1384  THROW(JParserException, "No parser object defined.");
1385  else
1386  (*this)->setInitialiationStatus(true);
1387 
1388  return *this;
1389  }
1390 
1391 
1392  /**
1393  * Set initialised status to false.
1394  *
1395  * \param value initialised object
1396  * \return this object
1397  */
1399  {
1400  (*this)->setInitialiationStatus(false);
1401 
1402  return *this;
1403  }
1404 
1405 
1406  /**
1407  * Stream input.
1408  *
1409  * \param in input stream
1410  * \param value parser element
1411  * \return input stream
1412  */
1413  friend inline std::istream& operator>>(std::istream& in, JParserElement& value)
1414  {
1415  if (value.is_valid())
1416  return value->read(in);
1417  else
1418  return in;
1419  }
1420 
1421 
1422  /**
1423  * Stream output.
1424  *
1425  * \param out output stream
1426  * \param value parser element
1427  * \return output stream
1428  */
1429  friend inline std::ostream& operator<<(std::ostream& out, const JParserElement& value)
1430  {
1431  if (value.is_valid())
1432  return value->write(out);
1433  else
1434  return out;
1435  }
1436 
1437 
1438  /**
1439  * Print.
1440  *
1441  * \param out output stream
1442  * \return output stream
1443  */
1444  void print(std::ostream& out) const
1445  {
1446  if (is_valid()) {
1447  return get()->print(out);
1448  }
1449  }
1450  };
1451 
1452 
1453  /**
1454  * Utility class to parse command line options.
1455  *
1456  * The mapping between a parameter (of any type) and a unique option
1457  * has to be defined in the user's application, e.g.
1458  * \code{.cpp}
1459 
1460  #include "Jeep/JParser.hh"
1461 
1462  int main(int argc, char**argv)
1463  {
1464  int aap;
1465  bool noot;
1466  bool mies;
1467 
1468  try {
1469 
1470  JParser<> zap;
1471 
1472  zap['i'] = make_field(aap) = 123; // set default value
1473  zap['b'] = make_field(noot); // default is false
1474  zap['B'] = make_field(mies);
1475 
1476  zap(argc, argv);
1477  }
1478  catch(const std::exception& error) {
1479  cerr << error.what() << endl;
1480  return 1;
1481  }
1482  }
1483  \endcode
1484  *
1485  * The behaviour of the parser is different for parameters of type <tt>bool</tt>.
1486  * By default, its value is set to <tt>false</tt>; it is set to <tt>true</tt>
1487  * when the corresponding option is parsed.
1488  * This implies that no data are read and that several options can be
1489  * parsed in sequence without repeating the '-' symbol.
1490  *
1491  *
1492  * The syntax for the command line is:
1493  *
1494  \verbatim
1495  program [-<option> <value> [-<option> <value>]]
1496  \endverbatim
1497 
1498  \verbatim
1499  program -h
1500  \endverbatim
1501  * will print the usage specification including all existing options.
1502 
1503  \verbatim
1504  program --!
1505  \endverbatim
1506  * will terminate the parsing of options and print the actual setting of all options.
1507  *
1508  * After the command line has been parsed, it is checked whether
1509  * each parameter has been assigned a value by default or at run time.
1510  * If not, an exception is thrown.
1511  *
1512  * For a comparison between the parsing of command line options with JParser and boost,
1513  * see <a href="https://drive.google.com/file/d/1R62NPhYjc_R7FYTRu-tkgUEo4PGoJc_y/view?usp=sharing">internal note</a>
1514  * which is accessible for anyone with a %KM3NeT account.
1515  */
1516  template<class JKey_t = char>
1517  class JParser :
1518  public std::map<JKey_t, JParserElement>,
1519  public JMessage< JParser<JKey_t> >
1520  {
1521  public:
1522 
1524  typedef JKey_t key_type;
1526 
1527  typedef typename map_type::iterator iterator;
1528  typedef typename map_type::const_iterator const_iterator;
1529 
1531 
1532 
1533  /**
1534  * Default constructor.
1535  *
1536  * \param debug debug level
1537  */
1538  JParser(const int debug = 0) :
1539  help ()
1540  {
1541  this->debug = debug;
1542  }
1543 
1544 
1545  /**
1546  * Constructor.
1547  *
1548  * \param message message printed with option <tt>-h</tt>
1549  * \param debug debug level
1550  */
1551  JParser(const std::string& message,
1552  const int debug = 0) :
1553  help (message)
1554  {
1555  this->debug = debug;
1556  }
1557 
1558 
1559  /**
1560  * Join parser.
1561  *
1562  * \param parser parser
1563  * \return this parser
1564  */
1565  JParser& join(const JParser& parser)
1566  {
1567  this->insert(parser.begin(), parser.end());
1568 
1569  return *this;
1570  }
1571 
1572 
1573  /**
1574  * Print the possible command line options.
1575  *
1576  * \param out output stream
1577  * \return output stream
1578  */
1579  void print(std::ostream& out) const
1580  {
1581  using namespace std;
1582  using namespace JPP;
1583 
1584  if (help != "") {
1585  out << help << endl;
1586  }
1587 
1588  out << "usage: " << getFilename(pid) << endl;
1589 
1590  out << ' ' << START_OF_OPTION << HELP_OPTION << ' ' << " \"help\"" << endl;
1591  out << ' ' << START_OF_OPTION << HELP_OPTION << PRINT_OPTION << " \"help with print of default and possible values\"" << endl;
1592  out << ' ' << START_OF_OPTION << REVISION_OPTION << ' ' << " \"print revision\"" << endl;
1593  out << ' ' << START_OF_OPTION << END_OF_OPTIONS << ' ' << " \"end of options; remainder will be discarded\"" << endl;
1594  out << ' ' << START_OF_OPTION << END_OF_OPTIONS << PRINT_OPTION << " \"end of options with print of actual values\"" << endl;
1595 
1596  for (const_iterator i = this->begin(); i != this->end(); ++i) {
1597 
1598  out << ' ' << START_OF_OPTION << i->first << " ";
1599 
1600  i->second.print(out);
1601 
1602  out << endl;
1603  }
1604 
1605  if (getURL() != "") {
1606  out << endl << "See also: " << getURL() << '#' << getFilename(pid) << endl;
1607  }
1608  }
1609 
1610 
1611  /**
1612  * Terminate.
1613  *
1614  * \param status exit status
1615  */
1616  virtual void terminate(const int status)
1617  {
1618  exit(status);
1619  }
1620 
1621 
1622  /**
1623  * Parse the program's command line options.
1624  *
1625  * \param argc number of arguments
1626  * \param argv argument list
1627  * \return argument list
1628  */
1629  JArgs operator()(const int argc, const char* const argv[])
1630  {
1631  return (*this)(JArgs(argc, argv));
1632  }
1633 
1634 
1635  /**
1636  * Parse the program's command line options.
1637  *
1638  * \param args argument list
1639  * \return argument list
1640  */
1641  JArgs operator()(const JArgs& args)
1642  {
1643  using namespace std;
1644  using namespace JLANG;
1645  using namespace JEEP;
1646 
1647  pid = args.PID;
1648 
1649  istringstream is;
1650 
1651  // argument passing
1652 
1653  for (JArgs::const_iterator i = args.begin(); i != args.end(); ++i) {
1654 
1655  DEBUG("Processing option <" << *i << ">" << endl);
1656 
1657  is.clear();
1658  is.str(*i);
1659 
1660  for (int c; (c = is.get()) != EOF; ) {
1661 
1662  if (c == START_OF_OPTION) {
1663 
1664  if (is.peek() == EOF) { // end-of-file
1665 
1666  THROW(JParserException, "stray " << START_OF_OPTION << " in <" << is.str() << "> at " << JArgs("", i, args.end()));
1667 
1668  } else if (isspace(is.peek())) { // white space
1669 
1670  THROW(JParserException, "stray " << START_OF_OPTION << " in <" << is.str() << "> at " << JArgs("", i, args.end()));;
1671  }
1672 
1673  while (is.peek() != EOF) { // read option(s)
1674 
1675  if (is.peek() == HELP_OPTION) { // help
1676 
1677  is.get();
1678 
1679  setPrintOption(cout, SHORT_PRINT);
1680 
1681  if (is.peek() != EOF) {
1682 
1683  if (is.get() == PRINT_OPTION) {
1684 
1685  setPrintOption(cout, LONG_PRINT);
1686 
1687  } else {
1688 
1689  THROW(JParserException, "invalid option at <" << is.str() << ">");
1690  }
1691  }
1692 
1693  print(cout);
1694 
1695  terminate(NORMAL_EXIT_CODE);
1696 
1697  return JArgs();
1698 
1699  } else if (is.peek() == REVISION_OPTION) { // revision
1700 
1701  cout << "source: " << getSource() << endl;
1702  cout << "version: " << getGITVersion() << endl;
1703  cout << "commit: " << getGITCommit() << endl;
1704  cout << "date: " << getGITDate() << endl;
1705  cout << "namespace: " << getNamespace() << endl;
1706 
1707  terminate(NORMAL_EXIT_CODE);
1708 
1709  return JArgs();
1710 
1711  } else if (is.peek() == END_OF_OPTIONS) { // end of options
1712 
1713  is.get();
1714 
1715  if (is.peek() != EOF) {
1716 
1717  c = is.get();
1718 
1719  if (c == PRINT_OPTION) {
1720 
1722 
1723  print(cout);
1724 
1725  } else if (c == PID_OPTION) {
1726 
1727  if (is.peek() == EOF && i + 1 != args.end()) {
1728  is.clear();
1729  is.str(*++i);
1730  }
1731 
1732  string file_name;
1733 
1734  getline(is, file_name);
1735 
1736  ofstream out(file_name.c_str());
1737 
1738  out << getpid() << endl;
1739 
1740  if (!out) {
1741  THROW(JParserException, "invalid option at <" << is.str() << ">");
1742  }
1743 
1744  out.close();
1745 
1746  } else {
1747 
1748  THROW(JParserException, "invalid option at <" << is.str() << ">");
1749  }
1750  }
1751 
1752  check_status();
1753 
1754  return JArgs(pid, ++i, args.end());
1755 
1756  } else {
1757 
1758  key_type option;
1759 
1760  is >> option;
1761 
1762  iterator p = this->find(option);
1763 
1764  DEBUG("Processing option <" << option << "> " << (p != this->end()) << endl);
1765 
1766  if (p != this->end()) {
1767 
1768  if (p->second->gcount()) {
1769 
1770  if (is.peek() == EOF && i + 1 != args.end()) {
1771  is.clear();
1772  is.str(*++i);
1773  }
1774  }
1775 
1776  try {
1777  is >> p->second;
1778  }
1779  catch(const exception& error) {
1780  THROW(JParserException, "read error " << error.what() << " at <" << is.str() << ">");
1781  }
1782 
1783  if (fail(is)) {
1784  THROW(JParserException, "read error at <" << is.str() << ">");
1785  }
1786 
1787  } else {
1788 
1789  THROW(JParserException, "unknown option <" << is.str() << "> at " << JArgs("", i, args.end()));
1790  }
1791  }
1792  }
1793 
1794  } else {
1795 
1796  THROW(JParserException, "illegal character <" << (char) c << "> at " << JArgs("", i, args.end()));
1797  }
1798  }
1799  }
1800 
1801  check_status();
1802 
1803  return JArgs();
1804  }
1805 
1806 
1807  /**
1808  * Parse the program's command line options.\n
1809  * This method is maintained for backward compatibility and will be deprecated.
1810  *
1811  * \param argc number of arguments
1812  * \param argv argument list
1813  * \return 0
1814  */
1815  int read(const int argc, const char* const argv[])
1816  {
1817  (*this)(argc, argv);
1818 
1819  return 0;
1820  }
1821 
1822 
1823  /**
1824  * Parse the program's command line options.\n
1825  * This method is maintained for backward compatibility and will be deprecated.
1826  *
1827  * \param args argument list
1828  * \return 0
1829  */
1830  int read(const JArgs& args)
1831  {
1832  (*this)(args);
1833 
1834  return 0;
1835  }
1836 
1837 
1838  /**
1839  * Print the current parameter values.
1840  *
1841  * \param out output stream
1842  * \return output stream
1843  */
1844  std::ostream& write(std::ostream& out) const
1845  {
1846  for (const_iterator i = this->begin(); i != this->end(); ++i) {
1847  out << i->second->getName() << '=' << i->second << std::endl;
1848  }
1849 
1850  return out;
1851  }
1852 
1853 
1854  /**
1855  * Stream output.
1856  *
1857  * \param out output stream
1858  * \param parser parser
1859  * \return output stream
1860  */
1861  friend inline std::ostream& operator<<(std::ostream& out, const JParser<key_type>& parser)
1862  {
1863  return parser.write(out);
1864  }
1865 
1866 
1867  protected:
1868  /**
1869  * Check if all required options have been set.\n
1870  * This method throws an exception in case of a non-compliance.
1871  */
1872  void check_status() const
1873  {
1874  for (const_iterator p = this->begin(); p != this->end(); ++p) {
1875 
1876  if (!p->second->getInitialisationStatus()) {
1877  THROW(JParserException, "option: " << START_OF_OPTION << p->first << " <" << p->second->getName() << ">" << " has no value");
1878  }
1879 
1880  if (!p->second->getStatus()) {
1881  THROW(JParserException, "option: " << START_OF_OPTION << p->first << " <" << p->second->getName() << ">" << " has illegal value");
1882  }
1883  }
1884  }
1885 
1886  std::string help; //!< help message
1887  std::string pid; //!< process name
1888  };
1889 
1890 
1891  /**
1892  * Auxiliary method for creation of template parser element object
1893  *
1894  * \param object object
1895  * \param name name of object
1896  * \param help help of object
1897  * \return parser element
1898  */
1899  inline JParserTemplateElement<bool> getOption(bool& object, const std::string& name, const std::string& help = "")
1900  {
1901  return JParserTemplateElement<bool>(object, name, help);
1902  }
1903 
1904 
1905  /**
1906  * Auxiliary method for creation of template parser element object
1907  *
1908  * \param object object
1909  * \param name name of object
1910  * \param help help of object
1911  * \return parser element
1912  */
1913  inline JParserTemplateElement<JCounter> getOption(JCounter& object, const std::string& name, const std::string& help = "")
1914  {
1915  return JParserTemplateElement<JCounter>(object, name, help);
1916  }
1917 
1918 
1919  /**
1920  * Auxiliary method for creation of template parser element object
1921  *
1922  * \param object object
1923  * \param name name of object
1924  * \param help help of object
1925  * \return parser element
1926  */
1927  template<class JType_t>
1928  inline JParserTemplateElement<JType_t> getOption(JType_t& object, const std::string& name, const std::string& help = "")
1929  {
1930  return JParserTemplateElement<JType_t>(object, name, help);
1931  }
1932 
1933 
1934  /**
1935  * Auxiliary method for creation of template parser element object
1936  *
1937  * \param object object
1938  * \param name name of object
1939  * \param help help of object
1940  * \param __begin begin of possible values
1941  * \param __end end of possible values
1942  * \return parser element
1943  */
1944  template<class JType_t, class T>
1946  const std::string& name,
1947  const std::string& help,
1948  T __begin,
1949  T __end)
1950  {
1951  return JParserTemplateElement<JType_t>(object, name, help, __begin, __end);
1952  }
1953 
1954 
1955  /**
1956  * Auxiliary method for creation of template parser element object
1957  *
1958  * \param object object
1959  * \param name name of object
1960  * \param __begin begin of possible values
1961  * \param __end end of possible values
1962  * \return parser element
1963  */
1964  template<class JType_t, class T>
1966  const std::string& name,
1967  T __begin,
1968  T __end)
1969  {
1970  return getOption(object, name, "", __begin, __end);
1971  }
1972 }
1973 
1974 
1975 /**
1976  * Make string for variadic macro.
1977  *
1978  * When called,
1979  * - first argument should correspond to a dummy value;
1980  * - second to the ##__VA_ARGS__ macro; and
1981  * - third to the fall back value (e.g.\ "");
1982  *
1983  * \param A dummy value
1984  * \param B ##__VA_ARGS__ macro
1985  * \return std::string
1986  */
1987 #define VARGS_STRING(A, B, ...) (static_cast<std::ostringstream&>(JPARSER::getOstream() << B << std::flush)).str()
1988 
1989 
1990 /**
1991  * macro to convert parameter to JParserTemplateElement object
1992  */
1993 #define make_field(A, ...) JPARSER::getOption(A, #A, VARGS_STRING("", ##__VA_ARGS__, ""))
1994 
1995 
1996 /**
1997  * macro to convert parameter to JParserTemplateElement object
1998  */
1999 #define make_option(A, ...) JPARSER::getOption(A, #A, VARGS_STRING("", ##__VA_ARGS__, ""))
2000 
2001 
2002 using JPARSER::JParser;
2003 using JPARSER::JOption;
2004 using JPARSER::JCounter;
2005 using JPARSER::getOption;
2006 using JPARSER::JArgs;
2007 
2008 #endif
JArgs operator()(const JArgs &args)
Parse the program&#39;s command line options.
Definition: JParser.hh:1641
Utility class to parse command line options.
Definition: JParser.hh:1517
Auxiliary data structure for alignment of data.
Definition: JManip.hh:231
virtual std::istream & read(std::istream &in) override
Stream input.
Definition: JParser.hh:672
JParser(const int debug=0)
Default constructor.
Definition: JParser.hh:1538
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:1348
virtual bool getStatus() const =0
Get status of parameter.
const JParserElement & operator=(const initialised &value)
Set initialised status to true.
Definition: JParser.hh:1381
medium print
Definition: JManip.hh:39
virtual bool getInitialisationStatus() const override
Get initialisation status of parameter.
Definition: JParser.hh:1249
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:871
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:1261
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:1844
std::string help
help message
Definition: JParser.hh:1886
virtual bool getStatus() const override
Status of object.
Definition: JParser.hh:1238
friend std::istream & operator>>(std::istream &in, JParserElement &value)
Stream input.
Definition: JParser.hh:1413
virtual std::ostream & write(std::ostream &out) const override
Stream output.
Definition: JParser.hh:1227
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:569
JParserTemplateElement< JType_t > & operator=(JType_t &value)
Assignment operator.
Definition: JParser.hh:1331
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:1899
JParserTemplateElement(JType_t &object, const std::string &name="arg", const std::string &help="")
Constructor.
Definition: JParser.hh:885
#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:1284
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:822
Auxiliary class for handling I/O of TString depending on its existence.
Definition: JParser.hh:758
Jpp environment information.
const JParserTemplateElement< JType_t, true > & operator=(const not_initialised &value)
Set initialised status to false.
Definition: JParser.hh:952
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:1169
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:660
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:1551
JParserElement()
Default constructor.
Definition: JParser.hh:1295
JSharedPointer< JParserElementInterface > JSharedPointer_t
Definition: JParser.hh:1289
const std::string & getName() const
Get name of parameter.
Definition: JParser.hh:389
virtual void print(std::ostream &out) const
Print.
Definition: JParser.hh:436
JParserTemplateElement< JType_t > & __element
Definition: JParser.hh:561
std::istream & readObject(std::istream &in, T &object)
Stream input of object.
virtual bool getStatus() const override
Status of object.
Definition: JParser.hh:1137
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:1305
bool more(const int value) const
Compare value.
Definition: JParser.hh:293
void setPossibleValues(T __begin, T __end)
Set possible values.
Definition: JParser.hh:1059
std::ostream & write(std::ostream &out) const
Definition: JParser.hh:747
void print(std::ostream &out) const
Print the possible command line options.
Definition: JParser.hh:1579
JParserTemplateElement(JType_t &object, const std::string &name, T __begin, T __end)
Constructor.
Definition: JParser.hh:901
JCSV & operator,(JType_t value)
Parsing of additional possible values.
Definition: JParser.hh:519
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:497
map_type::iterator iterator
Definition: JParser.hh:1527
JParserElement & operator=(JParserElement &value)
Assignment operator.
Definition: JParser.hh:1316
const JParserTemplateElement< JType_t, false > & operator=(const initialised &value)
Set initialised status to true.
Definition: JParser.hh:595
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:1148
do set_variable OUTPUT_DIRECTORY $WORKDIR T
int read(const JArgs &args)
Parse the program&#39;s command line options.
Definition: JParser.hh:1830
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:704
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
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:1017
JOption(const T &value)
Constructor.
Definition: JParser.hh:137
virtual std::ostream & write(std::ostream &out) const override
Stream output.
Definition: JParser.hh:1126
void check_status() const
Check if all required options have been set.
Definition: JParser.hh:1872
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:921
const JParserTemplateElement< JType_t, true > & operator=(const initialised &value)
Set initialised status to true.
Definition: JParser.hh:938
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:1523
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:1097
JCSV< JType_t > operator=(const JContainer_t< JType_t, JAllocator_t > &values)
Assignment to a default value and possible other values.
Definition: JParser.hh:985
virtual bool getStatus() const override
Get status of object.
Definition: JParser.hh:638
Empty structure for specification of parser element that is not initialised (i.e. does require input)...
Definition: JParser.hh:89
virtual std::istream & read(std::istream &in) override
Stream input.
Definition: JParser.hh:1215
virtual void terminate(const int status)
Terminate.
Definition: JParser.hh:1616
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:1525
Template specialisation of JPARSER::JParserTemplateElement for type bool.
Definition: JParser.hh:1084
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:580
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:1815
JCSV & operator,(const JContainer_t< JType_t, JAllocator_t > &values)
Parsing of additional possible values.
Definition: JParser.hh:534
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:1042
virtual std::istream & read(std::istream &in) override
Stream input.
Definition: JParser.hh:1112
JCSV< JType_t > operator=(const JCSV< JType_t > &value)
Assignment operator.
Definition: JParser.hh:1365
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:609
Auxiliary class to assign the remainder of a sequence of Comma Separated Values.
Definition: JParser.hh:490
JCSV< JType_t > operator=(const JType_t &value)
Assignment to a default value and possible other values.
Definition: JParser.hh:966
Template specialisation of JPARSER::JParserTemplateElement for type JCounter.
Definition: JParser.hh:1187
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:809
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:789
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:623
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:1528
const JParserElement & operator=(const not_initialised &value)
Set initialised status to false.
Definition: JParser.hh:1398
void print(std::ostream &out) const
Print.
Definition: JParser.hh:1444
possible values
virtual bool gcount() const
Read counter.
Definition: JParser.hh:466
JArgs operator()(const int argc, const char *const argv[])
Parse the program&#39;s command line options.
Definition: JParser.hh:1629
$WORKDIR ev_configure_domsimulator txt echo process $DOM_SIMULATOR $i $SOURCE_HOST[$index] csh c(setenv ROOTSYS $ROOTSYS &&source $JPP_DIR/setenv.csh $JPP_DIR &&($DOM_SIMULATOR\-u\$NAME\$\-H\$SERVER\$\-M\$LOGGER\$\-d $DEBUG</dev/null > &/dev/null &))'
T * clone() const
Get clone of this object.
Definition: JParser.hh:185
std::string pid
process name
Definition: JParser.hh:1887
virtual bool gcount() const override
Read counter.
Definition: JParser.hh:1270
Template class holder for I/O of parser element.
Definition: JParser.hh:483
JParser & join(const JParser &parser)
Join parser.
Definition: JParser.hh:1565
virtual void setInitialiationStatus(const bool value) override
Set initialisation status of parameter.
Definition: JParser.hh:1160
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:1429
JParserTemplateElement(JCounter &object, const std::string &name, const std::string &help="")
Constructor.
Definition: JParser.hh:1200
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:722
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:649
JCSV & operator,(const JContainer_t< JType_t, JCompare_t, JAllocator_t > &values)
Parsing of additional possible values.
Definition: JParser.hh:551
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:1000
static std::istream & read(std::istream &in, T &object)
Read object from input stream.
Definition: JParser.hh:775
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