EpcTools
An event based multi-threaded C++ development framework.
egetopt.h
Go to the documentation of this file.
1 /*
2 * Copyright (c) 2009-2019 Brian Waters
3 * Copyright (c) 2019 Sprint
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 
18 #ifndef __egetopt_h_included
19 #define __egetopt_h_included
20 
21 #include "eerror.h"
22 #include "estring.h"
23 
31 class EGetOpt
32 {
33 public:
35  enum ArgType
36  {
43  };
44 
46  enum DataType
47  {
64  };
65 
67  struct Option
68  {
77  };
78 
80  EGetOpt();
82  ~EGetOpt();
83 
88  Void setPrefix(const EString &path) { setPrefix(path.c_str()); }
93  Void setPrefix(cpStr path = "") { m_prefix = path; }
94 
105  Void loadCmdLine(Int argc, pStr *argv, const EGetOpt::Option *options);
112  Void loadFile(cpStr filename);
113 
119  Long getCmdLine(cpStr path, Long def) const;
125  LongLong getCmdLine(cpStr path, LongLong def) const;
131  ULong getCmdLine(cpStr path, ULong def) const;
137  ULongLong getCmdLine(cpStr path, ULongLong def) const;
143  Double getCmdLine(cpStr path, Double def) const;
149  cpStr getCmdLine(cpStr path, cpStr def) const;
155  Bool getCmdLine(cpStr path, Bool def) const;
156 
158  std::vector<EString> getCmdLineArgs() const;
159 
161  std::vector<EString> getCmdLineRaw() const;
162 
164  std::vector<EString> getMembers(cpStr path) const;
166  std::vector<EString> getMembers(UInt idx, cpStr path, cpStr member) const;
167 
173  Long get(cpStr path, Long def) const;
179  LongLong get(cpStr path, LongLong def) const;
185  ULong get(cpStr path, ULong def) const;
191  ULongLong get(cpStr path, ULongLong def) const;
197  Double get(cpStr path, Double def) const;
203  cpStr get(cpStr path, cpStr def) const;
209  Bool get(cpStr path, Bool def) const;
210 
215  UInt getCount(cpStr path) const;
223  template<typename T>
224  T get(UInt idx, cpStr path, cpStr member, T def) const
225  {
226  EString mbr;
227  mbr.format("%u/%s", idx, member);
228  EString pth = combinePath(path, mbr.c_str());
229  return get(pth, def);
230  }
235  template<typename T>
236  std::vector<T> getArray(cpStr path) const;
244  template<typename T>
245  std::vector<T> getArray(UInt idx, cpStr path, cpStr member) const
246  {
247  EString mbr;
248  mbr.format("%u/%s", idx, member);
249  EString pth = combinePath(path, mbr.c_str());
250  return getArray<T>(pth);
251  }
252 
254  Void print() const;
255 
256 private:
257  EString combinePath(cpStr path1, cpStr path2) const;
258  const Option *findOption(cpStr name, const Option *options);
259 
260  pVoid m_json;
261  EString m_prefix;
262 };
263 
266 
268 class EGetOptError_MissingRequiredArgument : public EError
269 {
270 public:
271  EGetOptError_MissingRequiredArgument(cpStr pszFile);
272  virtual const cpStr Name() const { return "EGetOptError_MissingRequiredArgument"; }
273 };
274 
275 class EGetOptError_UnsupportedArgType : public EError
276 {
277 public:
278  EGetOptError_UnsupportedArgType(EGetOpt::ArgType argType);
279  virtual const cpStr Name() const { return "EGetOptError_UnsupportedArgType"; }
280 };
281 
282 class EGetOptError_UnsupportedDataType : public EError
283 {
284 public:
285  EGetOptError_UnsupportedDataType(EGetOpt::DataType argType);
286  virtual const cpStr Name() const { return "EGetOptError_UnsupportedDataType"; }
287 };
288 
289 class EGetOptError_UnsupportedBooleanValue : public EError
290 {
291 public:
292  EGetOptError_UnsupportedBooleanValue(cpStr val);
293  virtual const cpStr Name() const { return "EGetOptError_UnsupportedBooleanValue"; }
294 };
295 
296 class EGetOptError_FileParsing : public EError
297 {
298 public:
299  EGetOptError_FileParsing(cpStr val);
300  virtual const cpStr Name() const { return "EGetOptError_FileParsing"; }
301 };
302 
303 class EGetOptError_UnsupportedArrayType : public EError
304 {
305 public:
306  EGetOptError_UnsupportedArrayType(cpStr val);
307  virtual const cpStr Name() const { return "EGetOptError_UnsupportedArrayType"; }
308 };
309 
310 class EGetOptError_UnexpectedArrayElementType : public EError
311 {
312 public:
313  EGetOptError_UnexpectedArrayElementType(cpStr rcvd, cpStr expctd);
314  virtual const cpStr Name() const { return "EGetOptError_UnexpectedArrayElementType"; }
315 };
316 
317 class EGetOptError_NotArray : public EError
318 {
319 public:
320  EGetOptError_NotArray();
321  virtual const cpStr Name() const { return "EGetOptError_NotArray"; }
322 };
324 
325 #endif // #define __egetopt_h_included
Long getCmdLine(cpStr path, Long def) const
Returns the value of the specified command line argument as a 32-bit integer.
Definition: egetopt.cpp:320
Encapsulates and extends a std::string object.
DataType
The data type of the command line argument.
Definition: egetopt.h:46
32-bit unsigned integer
Definition: egetopt.h:57
EString shortName
the short name of the argument e.g.: -a
Definition: egetopt.h:70
32-bit integer
Definition: egetopt.h:53
Void loadCmdLine(Int argc, pStr *argv, const EGetOpt::Option *options)
Parses and loads the command line arguments.
Definition: egetopt.cpp:907
EString longName
the long name of the argument e.g.: –argument
Definition: egetopt.h:72
string
Definition: egetopt.h:51
no argument required
Definition: egetopt.h:38
Definition: egetopt.h:31
ArgType argType
indicates if an argument is required or not
Definition: egetopt.h:74
DataType dataType
the data type of the argument
Definition: egetopt.h:76
8-byte floating point number
Definition: egetopt.h:61
EGetOpt()
Class constructor.
Definition: egetopt.cpp:278
an argument is required
Definition: egetopt.h:40
Void setPrefix(const EString &path)
Sets the search prefix.
Definition: egetopt.h:88
Describes the defined command line arguments.
Definition: egetopt.h:67
Defines base class for exceptions and declaration helper macros.
Void loadFile(cpStr filename)
Parses and loads configuration values from the specified JSON file.
Definition: egetopt.cpp:1042
boolean
Definition: egetopt.h:63
std::vector< T > getArray(UInt idx, cpStr path, cpStr member) const
Returns the values of an array.
Definition: egetopt.h:245
The base class for exceptions derived from std::exception.
Definition: eerror.h:94
ArgType
Indicates if an argument associated with a command line argument is required.
Definition: egetopt.h:35
64-bit unsigned integer
Definition: egetopt.h:59
std::vector< EString > getCmdLineRaw() const
Returns a std::vector containing the "raw" string command line arguments.
Definition: egetopt.cpp:453
std::vector< T > getArray(cpStr path) const
Returns the values of an array.
Definition: egetopt.cpp:793
~EGetOpt()
Class destructor.
Definition: egetopt.cpp:302
std::vector< EString > getCmdLineArgs() const
Returns a std::vector containing positional command line arguments.
Definition: egetopt.cpp:433
64-bit integer
Definition: egetopt.h:55
UInt getCount(cpStr path) const
returns the number of configuration entries specified in a JSON array.
Definition: egetopt.cpp:636
std::vector< EString > getMembers(cpStr path) const
Returns a std::vector containing the member names based on the provided path.
Definition: egetopt.cpp:473
Void setPrefix(cpStr path="")
Sets the search prefix.
Definition: egetopt.h:93
EString & format(cpChar pszFormat,...)
Sets the value to the string using a "printf" style format string and arguments.
Definition: estring.cpp:38
String class.
Definition: estring.h:31
the argument is optional *** NOT IMPLEMENTED ***
Definition: egetopt.h:42
no argument
Definition: egetopt.h:49
Void print() const
Prints the current loaded values (command line and file).
Definition: egetopt.cpp:315