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 
168  Long get(cpStr path, Long def) const;
174  LongLong get(cpStr path, LongLong def) const;
180  ULong get(cpStr path, ULong def) const;
186  ULongLong get(cpStr path, ULongLong def) const;
192  Double get(cpStr path, Double def) const;
198  cpStr get(cpStr path, cpStr def) const;
204  Bool get(cpStr path, Bool def) const;
205 
210  UInt getCount(cpStr path) const;
218  template<typename T>
219  T get(UInt idx, cpStr path, cpStr member, T def) const
220  {
221  EString mbr;
222  mbr.format("%u/%s", idx, member);
223  EString pth = combinePath(path, mbr.c_str());
224  return get(pth, def);
225  }
226 
228  Void print() const;
229 
230 private:
231  EString combinePath(cpStr path1, cpStr path2) const;
232  const Option *findOption(cpStr name, const Option *options);
233 
234  pVoid m_json;
235  EString m_prefix;
236 };
237 
240 
242 class EGetOptError_MissingRequiredArgument : public EError
243 {
244 public:
245  EGetOptError_MissingRequiredArgument(cpStr pszFile);
246  virtual const cpStr Name() const { return "EGetOptError_MissingRequiredArgument"; }
247 };
248 
249 class EGetOptError_UnsupportedArgType : public EError
250 {
251 public:
252  EGetOptError_UnsupportedArgType(EGetOpt::ArgType argType);
253  virtual const cpStr Name() const { return "EGetOptError_UnsupportedArgType"; }
254 };
255 
256 class EGetOptError_UnsupportedDataType : public EError
257 {
258 public:
259  EGetOptError_UnsupportedDataType(EGetOpt::DataType argType);
260  virtual const cpStr Name() const { return "EGetOptError_UnsupportedDataType"; }
261 };
262 
263 class EGetOptError_UnsupportedBooleanValue : public EError
264 {
265 public:
266  EGetOptError_UnsupportedBooleanValue(cpStr val);
267  virtual const cpStr Name() const { return "EGetOptError_UnsupportedBooleanValue"; }
268 };
269 
270 class EGetOptError_FileParsing : public EError
271 {
272 public:
273  EGetOptError_FileParsing(cpStr val);
274  virtual const cpStr Name() const { return "EGetOptError_FileParsing"; }
275 };
277 
278 #endif // #define __egetopt_h_included
EGetOpt::dtString
string
Definition: egetopt.h:51
EGetOpt::dtDouble
8-byte floating point number
Definition: egetopt.h:61
EGetOpt::getCount
UInt getCount(cpStr path) const
returns the number of configuration entries specified in a JSON array.
Definition: egetopt.cpp:577
EGetOpt::Option::argType
ArgType argType
indicates if an argument is required or not
Definition: egetopt.h:74
eerror.h
Defines base class for exceptions and declaration helper macros.
EGetOpt::getCmdLine
Long getCmdLine(cpStr path, Long def) const
Returns the value of the specified command line argument as a 32-bit integer.
Definition: egetopt.cpp:302
EGetOpt::Option::dataType
DataType dataType
the data type of the argument
Definition: egetopt.h:76
EGetOpt::ArgType
ArgType
Indicates if an argument associated with a command line argument is required.
Definition: egetopt.h:35
EGetOpt::no_argument
no argument required
Definition: egetopt.h:38
EGetOpt::Option::longName
EString longName
the long name of the argument e.g.: –argument
Definition: egetopt.h:72
EGetOpt::setPrefix
Void setPrefix(cpStr path)
Sets the search prefix.
Definition: egetopt.h:93
EGetOpt::required_argument
an argument is required
Definition: egetopt.h:40
EGetOpt::Option
Describes the defined command line arguments.
Definition: egetopt.h:67
EGetOpt::dtUInt32
32-bit unsigned integer
Definition: egetopt.h:57
EGetOpt
Definition: egetopt.h:31
EGetOpt::dtInt32
32-bit integer
Definition: egetopt.h:53
EGetOpt::EGetOpt
EGetOpt()
Class constructor.
Definition: egetopt.cpp:260
EError::Name
virtual const cpStr Name() const
Returns the name of this object.
Definition: eerror.h:189
EGetOpt::loadCmdLine
Void loadCmdLine(Int argc, pStr *argv, const EGetOpt::Option *options)
Parses and loads the command line arguments.
Definition: egetopt.cpp:613
EGetOpt::dtBool
boolean
Definition: egetopt.h:63
EGetOpt::get
Long get(cpStr path, Long def) const
Returns the value of the specified configuration value as a 32-bit integer.
Definition: egetopt.cpp:455
EGetOpt::Option::shortName
EString shortName
the short name of the argument e.g.: -a
Definition: egetopt.h:70
EGetOpt::print
Void print() const
Prints the current loaded values (command line and file).
Definition: egetopt.cpp:297
EGetOpt::setPrefix
Void setPrefix(const EString &path)
Sets the search prefix.
Definition: egetopt.h:88
EGetOpt::~EGetOpt
~EGetOpt()
Class destructor.
Definition: egetopt.cpp:284
EGetOpt::optional_argument
the argument is optional *** NOT IMPLEMENTED ***
Definition: egetopt.h:42
EGetOpt::getCmdLineArgs
std::vector< EString > getCmdLineArgs() const
Returns a std::vector containing positional command line arguments.
Definition: egetopt.cpp:415
EGetOpt::get
T get(UInt idx, cpStr path, cpStr member, T def) const
Returns the value associated with an array.
Definition: egetopt.h:219
EGetOpt::dtUInt64
64-bit unsigned integer
Definition: egetopt.h:59
EString::format
EString & format(cpChar pszFormat,...)
Sets the value to the string using a "printf" style format string and arguments.
Definition: estring.cpp:38
EString
String class.
Definition: estring.h:30
EGetOpt::DataType
DataType
The data type of the command line argument.
Definition: egetopt.h:46
EGetOpt::loadFile
Void loadFile(cpStr filename)
Parses and loads configuration values from the specified JSON file.
Definition: egetopt.cpp:748
EGetOpt::dtNone
no argument
Definition: egetopt.h:49
EError
The base class for exceptions derived from std::exception.
Definition: eerror.h:92
EGetOpt::dtInt64
64-bit integer
Definition: egetopt.h:55
estring.h
Encapsulates and extends a std::string object.
EGetOpt::getCmdLineRaw
std::vector< EString > getCmdLineRaw() const
Returns a std::vector containing the "raw" string command line arguments.
Definition: egetopt.cpp:435