ParamKit
A small library helping to parse commandline parameters (for Windows).
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
param.h
Go to the documentation of this file.
1 
6 #pragma once
7 
8 #include <windows.h>
9 
10 #include <iostream>
11 #include <string>
12 #include <sstream>
13 #include <map>
14 #include <set>
15 
16 #include "pk_util.h"
17 #include "strings_util.h"
18 
19 #define PARAM_UNINITIALIZED (-1)
20 #define INFO_SPACER "\t "
21 
22 #define PARAM_SWITCH1 '/'
23 #define PARAM_SWITCH2 '-'
24 
25 typedef unsigned __int64 uint64_t;
26 
27 namespace paramkit {
28 
30  class Param {
31  public:
32 
34 
38  Param(const std::string& _argStr, bool _isRequired)
39  {
40  isRequired = _isRequired;
41  argStr = _argStr;
42  requiredArg = false;
43  active = true;
44  }
45 
47 
52  Param(const std::string& _argStr, const std::string& _typeDescStr, bool _isRequired)
53  {
54  isRequired = _isRequired;
55  argStr = _argStr;
56  typeDescStr = _typeDescStr;
57  requiredArg = false;
58  active = true;
59  }
60 
62  virtual std::string valToString() const = 0;
63 
65  virtual std::string type() const = 0;
66 
68  virtual bool parse(const char *arg) = 0;
69 
71  virtual bool parse(const wchar_t *arg)
72  {
73  std::wstring value = arg;
74  std::string str(value.begin(), value.end());
75  return parse(str.c_str());
76  }
77 
78  void setActive(bool _active)
79  {
80  this->active = _active;
81  }
82 
84  virtual bool isActive() const
85  {
86  return this->active;
87  }
88 
90  virtual bool isSet() const = 0;
91 
92  virtual std::string info(bool isExtended) const
93  {
94  std::stringstream ss;
95  ss << m_info;
96  if (isExtended) {
97  const std::string eInfo = extendedInfo();
98  if (eInfo.length()) {
99  ss << "\n" << eInfo;
100  }
101  }
102  return ss.str();
103  }
104 
106  void printInColor(int color)
107  {
108  print_in_color(color, PARAM_SWITCH1 + this->argStr);
109  }
110 
111  protected:
112 
114  void printDesc(bool isExtended = true) const
115  {
116  if (requiredArg) {
117  if (typeDescStr.length()) {
118  std::cout << " <" << typeDescStr << ">";
119  }
120  else {
121  std::cout << " <" << type() << ">";
122  }
123  }
124  std::cout << "\n\t";
125  std::cout << " : " << info(isExtended);
126  std::cout << "\n";
127  }
128 
130  virtual bool isNameSimilar(const std::string &filter)
131  {
133  return (sim_type != util::SIM_NONE) ? true : false;
134  }
135 
137  virtual bool isKeywordInType(const std::string &keyword)
138  {
139  if (util::has_keyword(this->type(), keyword)) {
140  return true;
141  }
142  return false;
143  }
144 
146  virtual bool isKeywordInDescription(const std::string &keyword)
147  {
148  if (util::has_keyword(m_info, keyword)) return true;
149 
150  util::stringsim_type sim_type = util::has_keyword(m_extInfo, keyword);
151  return (sim_type != util::SIM_NONE) ? true : false;
152  }
153 
155  virtual std::string extendedInfo() const
156  {
157  return m_extInfo;
158  }
159 
160  std::string argStr;
161 
162  std::string typeDescStr;
163  std::string m_info;
164  std::string m_extInfo;
165 
166  bool isRequired;
167  bool requiredArg;
168  bool active;
169 
170  friend class Params;
171  friend class ParamCompare;
172  friend class ParamGroup;
173  };
174 
177  {
178  public:
179  bool operator()(Param* param1, Param* param2) const
180  {
181  if (!param1 || !param2) return false;
182  bool val = ((param1->argStr.compare(param2->argStr) < 0));
183  return val;
184  }
185  };
186 
188  class IntParam : public Param {
189  public:
190 
191  typedef enum
192  {
198 
199  IntParam(const std::string& _argStr, bool _isRequired, t_int_base _base = INT_BASE_ANY)
200  : Param(_argStr, _isRequired),
201  base(_base)
202  {
203  requiredArg = true;
205  }
206 
207  virtual std::string valToString() const
208  {
209  std::stringstream stream;
210  if (base == INT_BASE_HEX) {
211  stream << std::hex;
212  }
213  else {
214  stream << std::dec;
215  }
216  stream << value;
217  return stream.str();
218  }
219 
220  virtual std::string type() const {
221  if (base == INT_BASE_HEX) {
222  return "integer: hex";
223  }
224  if (base == INT_BASE_DEC) {
225  return "integer: dec";
226  }
227  return "integer: decimal, or hexadecimal with '0x' prefix";
228  }
229 
230  virtual bool isSet() const
231  {
232  return value != PARAM_UNINITIALIZED;
233  }
234 
235  virtual bool parse(const char *arg)
236  {
237  if (!arg) return false;
238  const size_t len = strlen(arg);
239 
240  if (!isValidNumber(arg, len)) {
241  return false;
242  }
243  bool isHex = (base == INT_BASE_HEX);
244  if (base == INT_BASE_ANY) {
245  if (paramkit::is_hex_with_prefix(arg)) {
246  isHex = true;
247  }
248  }
249  this->value = loadInt(arg, isHex);
250  return true;
251  }
252 
253  bool isValidNumber(const char *arg, const size_t len)
254  {
255  if (base == INT_BASE_ANY) {
256  if (paramkit::is_hex_with_prefix(arg) || paramkit::is_dec(arg, len)) {
257  return true;
258  }
259  }
260  if (base == INT_BASE_HEX) {
261  if (paramkit::is_hex(arg, len) || paramkit::is_hex_with_prefix(arg)) {
262  return true;
263  }
264  }
265  if (base == INT_BASE_DEC) {
266  if (paramkit::is_dec(arg, len)) return true;
267  }
268  return false;
269  }
270 
273  };
274 
276  class StringParam : public Param {
277  public:
278  StringParam(const std::string& _argStr, bool _isRequired)
279  : Param(_argStr, _isRequired)
280  {
281  requiredArg = true;
282  value = "";
283  }
284 
285  virtual std::string valToString() const
286  {
287  return "\"" + value + "\"";
288  }
289 
290  virtual std::string type() const {
291  return "string";
292  }
293 
294  virtual bool isSet() const
295  {
296  return value.length() > 0;
297  }
298 
299  virtual bool parse(const char *arg)
300  {
301  if (!arg) return false;
302 
303  this->value = arg;
304  return true;
305  }
306 
308  size_t copyToCStr(char *buf, size_t buf_max) const
309  {
310  return copy_to_cstr(value, buf, buf_max);
311  }
312 
313  std::string value;
314  };
315 
317  class WStringParam : public Param {
318  public:
319  WStringParam(const std::string& _argStr, bool _isRequired)
320  : Param(_argStr, _isRequired)
321  {
322  requiredArg = true;
323  value = L"";
324  }
325 
326  virtual std::string valToString() const
327  {
328  std::string str(value.begin(), value.end());
329  return "\"" + str + "\"";
330  }
331 
332  virtual std::string type() const
333  {
334  return "wstring";
335  }
336 
337  virtual bool isSet() const
338  {
339  return value.length() > 0;
340  }
341 
342  virtual bool parse(const wchar_t *arg)
343  {
344  if (!arg) return false;
345 
346  this->value = arg;
347  return true;
348  }
349 
350  virtual bool parse(const char *arg)
351  {
352  if (!arg) return false;
353 
354  std::string value = arg;
355  std::wstring str(value.begin(), value.end());
356 
357  this->value = str;
358  return true;
359  }
360 
362  size_t copyToCStr(wchar_t *buf, size_t buf_len) const
363  {
364  return copy_to_cstr(value, buf, buf_len);
365  }
366 
367  std::wstring value;
368  };
369 
371  class BoolParam : public Param {
372  public:
373  BoolParam(const std::string& _argStr, bool _isRequired)
374  : Param(_argStr, _isRequired)
375  {
376  requiredArg = false;
377  value = false;
378  isParsed = false;
379  }
380 
381  virtual std::string type() const
382  {
383  return "bool";
384  }
385 
386  virtual std::string valToString() const
387  {
388  std::stringstream stream;
389  stream << std::dec;
390  if (value) {
391  stream << "true";
392  }
393  else {
394  stream << "false";
395  }
396  return stream.str();
397  }
398 
399  virtual bool isSet() const
400  {
401  return isParsed;
402  }
403 
404  virtual bool parse(const char *arg = nullptr)
405  {
406  if (!arg) {
407  this->value = true;
408  this->isParsed = true;
409  return true;
410  }
411  this->isParsed = loadBoolean(arg, this->value);
412  return this->isParsed;
413  }
414 
415  bool value;
416  bool isParsed;
417  };
418 
419 
421  class EnumParam : public Param {
422  public:
423  EnumParam(const std::string& _argStr, const std::string _enumName, bool _isRequired)
424  : Param(_argStr, _isRequired), enumName(_enumName), m_isSet(false)
425  {
426  requiredArg = true;
428  }
429 
430  bool addEnumValue(int value, const std::string &info)
431  {
432  enumToInfo[value] = info;
433  return true;
434  }
435 
436  bool addEnumValue(int value, const std::string &str_val, const std::string &info)
437  {
438  if (addEnumValue(value, info)) {
439  enumToString[value] = str_val;
440  }
441  return true;
442  }
443 
444  virtual std::string valToString() const
445  {
446  if (!isSet()) {
447  return "(undefined)";
448  }
449  std::map<int, std::string>::const_iterator foundString = enumToString.find(value);
450  if (foundString != enumToString.end()) {
451  return foundString->second;
452  }
453  std::stringstream stream;
454  stream << std::dec << value;
455  return stream.str();
456  }
457 
458  virtual std::string type() const
459  {
460  return "*" + enumName;
461  }
462 
463  virtual bool isSet() const
464  {
465  if (!m_isSet) return false;
466  if (!isInEnumScope(value)) {
467  return false;
468  }
469  return true;
470  }
471 
472  virtual bool isKeywordInType(const std::string &keyword)
473  {
474  if (util::has_keyword(enumName, keyword) != util::SIM_NONE) {
475  return true;
476  }
477  return false;
478  }
479 
480  bool isKeywordInDescription(const std::string &keyword)
481  {
482  if (Param::isKeywordInDescription(keyword)) {
483  return true;
484  }
485  if (isKeywordInType(keyword)) {
486  return true;
487  }
488  // search the keyword also in the descriptions of particulat options:
489  std::map<int, std::string>::const_iterator itr;
490  for (itr = enumToInfo.begin(); itr != enumToInfo.end(); ++itr) {
491  const std::string valDesc = itr->second;
492  util::stringsim_type sim_type = util::has_keyword(valDesc, keyword);
493  if (sim_type != util::SIM_NONE) return true;
494  }
495  return false;
496  }
497 
498  virtual bool parse(const char *arg)
499  {
500  if (!arg) return false;
501 
502  //try to find by the string representation first:
503  const std::string strVal = arg;
504  std::map<int, std::string>::iterator itr;
505  for (itr = enumToString.begin(); itr != enumToString.end(); ++itr) {
506  if (strVal == itr->second) {
507  this->value = itr->first;
508  m_isSet = true;
509  return true;
510  }
511  }
512  //try to find by the integer representation:
513  if (!is_number(arg)) {
514  return false;
515  }
516  int intVal = loadInt(arg);
517  if (!isInEnumScope(intVal)) {
518  // out of the enum scope
519  return false;
520  }
521  this->value = intVal;
522  m_isSet = true;
523  return true;
524  }
525 
526  int value;
527 
528  protected:
529 
530  std::string extendedInfo() const
531  {
532  std::stringstream stream;
533  stream << Param::extendedInfo();
534  stream << optionsInfo();
535  return stream.str();
536  }
537 
538  std::string optionsInfo() const
539  {
540  std::stringstream stream;
541  std::map<int, std::string>::const_iterator itr;
542  stream << type() << ":\n";
543  for (itr = enumToInfo.begin(); itr != enumToInfo.end(); ) {
544  int val = itr->first;
545  std::map<int, std::string>::const_iterator foundString = enumToString.find(itr->first);
546 
547  stream << "\t" << std::dec << val;
548  if (foundString != enumToString.end()) {
549  stream << " (" << foundString->second << ")";
550  }
551  stream << " - ";
552  stream << itr->second;
553  ++itr;
554  if (itr != enumToInfo.end()) {
555  stream << "\n";
556  }
557  }
558  return stream.str();
559  }
560 
561  bool isInEnumScope(int intVal)const
562  {
563  if (enumToInfo.find(intVal) != enumToInfo.end()) {
564  return true;
565  }
566  return false;
567  }
568 
569  std::map<int, std::string> enumToString;
570  std::map<int, std::string> enumToInfo;
571 
572  std::string enumName;
573  bool m_isSet;
574  };
575 
576 
577  class StringListParam : public StringParam {
578  public:
579  StringListParam(const std::string& _argStr, bool _isRequired, char _delimiter)
580  : StringParam(_argStr, _isRequired),
581  delimiter(std::string(1, _delimiter))
582  {
583  }
584 
585  StringListParam(const std::string& _argStr, bool _isRequired, std::string _delimiter)
586  : StringParam(_argStr, _isRequired),
587  delimiter(_delimiter)
588  {
589  }
590 
591  virtual std::string type() const
592  {
593  return "list: separated by \'" + delimiter + "\'";
594  }
595 
596  size_t stripToElements(OUT std::set<std::string> &elements_list)
597  {
598  return strip_to_list(this->value, this->delimiter, elements_list);
599  }
600 
601  const std::string delimiter;
602  };
603 
604  class IntListParam : public StringListParam {
605  public:
606  IntListParam(const std::string& _argStr, bool _isRequired, char _delimiter)
607  : StringListParam(_argStr, _isRequired, _delimiter)
608  {
609  }
610 
611  IntListParam(const std::string& _argStr, bool _isRequired, std::string _delimiter)
612  : StringListParam(_argStr, _isRequired, _delimiter)
613  {
614  }
615 
616  virtual std::string type() const
617  {
618  return "list: dec or hex, separated by \'" + delimiter + "\'";
619  }
620 
621  virtual bool parse(const char *arg)
622  {
623  if (!arg) return false;
624 
625  std::set<std::string> str_list;
626  if (!strip_to_list(arg, this->delimiter, str_list)) {
627  return false;
628  }
629  std::set<std::string>::iterator itr;
630  for (itr = str_list.begin(); itr != str_list.end(); ++itr) {
631  std::string nextEl = *itr;
632  if (!paramkit::is_number(nextEl.c_str())) return false;
633  }
634  this->value = arg;
635  return true;
636  }
637 
638  size_t stripToIntElements(OUT std::set<long> &elements_list)
639  {
640  std::set<std::string> str_list;
641  if (!stripToElements(str_list)) {
642  return 0;
643  }
644  std::set<std::string>::iterator itr;
645  for (itr = str_list.begin(); itr != str_list.end(); ++itr) {
646  std::string nextEl = *itr;
647  if (!paramkit::is_number(nextEl.c_str())) continue;
648 
649  long number = paramkit::get_number(nextEl.c_str());
650  elements_list.insert(number);
651  }
652  return elements_list.size();
653  }
654 
655  };
656 };
657 
A parameter storing a boolean value.
Definition: param.h:371
virtual bool isSet() const
Returns true if the parameter is filled, false otherwise.
Definition: param.h:399
virtual std::string type() const
Returns the string representation of the parameter's type.
Definition: param.h:381
virtual std::string valToString() const
Returns the string representation of the parameter's value.
Definition: param.h:386
BoolParam(const std::string &_argStr, bool _isRequired)
Definition: param.h:373
virtual bool parse(const char *arg=nullptr)
Parses the parameter from the given string.
Definition: param.h:404
A parameter storing an enum value.
Definition: param.h:421
std::map< int, std::string > enumToInfo
required: info about the enum parameter
Definition: param.h:570
virtual std::string type() const
Returns the string representation of the parameter's type.
Definition: param.h:458
virtual bool parse(const char *arg)
Parses the parameter from the given string.
Definition: param.h:498
virtual std::string valToString() const
Returns the string representation of the parameter's value.
Definition: param.h:444
bool isKeywordInDescription(const std::string &keyword)
Checks if the description contains the keyword.
Definition: param.h:480
std::string enumName
Definition: param.h:572
EnumParam(const std::string &_argStr, const std::string _enumName, bool _isRequired)
Definition: param.h:423
bool addEnumValue(int value, const std::string &info)
Definition: param.h:430
virtual bool isSet() const
Returns true if the parameter is filled, false otherwise.
Definition: param.h:463
std::map< int, std::string > enumToString
optional: string representation of the enum parameter
Definition: param.h:569
bool isInEnumScope(int intVal) const
Definition: param.h:561
virtual bool isKeywordInType(const std::string &keyword)
Checks if the parameter type contains the keyword.
Definition: param.h:472
bool addEnumValue(int value, const std::string &str_val, const std::string &info)
Definition: param.h:436
std::string optionsInfo() const
Definition: param.h:538
std::string extendedInfo() const
Extended information.
Definition: param.h:530
size_t stripToIntElements(OUT std::set< long > &elements_list)
Definition: param.h:638
IntListParam(const std::string &_argStr, bool _isRequired, char _delimiter)
Definition: param.h:606
virtual bool parse(const char *arg)
Parses the parameter from the given string.
Definition: param.h:621
IntListParam(const std::string &_argStr, bool _isRequired, std::string _delimiter)
Definition: param.h:611
virtual std::string type() const
Returns the string representation of the parameter's type.
Definition: param.h:616
A parameter storing an integer value.
Definition: param.h:188
bool isValidNumber(const char *arg, const size_t len)
Definition: param.h:253
virtual bool parse(const char *arg)
Parses the parameter from the given string.
Definition: param.h:235
t_int_base base
Definition: param.h:271
virtual std::string valToString() const
Returns the string representation of the parameter's value.
Definition: param.h:207
virtual bool isSet() const
Returns true if the parameter is filled, false otherwise.
Definition: param.h:230
virtual std::string type() const
Returns the string representation of the parameter's type.
Definition: param.h:220
uint64_t value
Definition: param.h:272
IntParam(const std::string &_argStr, bool _isRequired, t_int_base _base=INT_BASE_ANY)
Definition: param.h:199
A comparator class for Param class.
Definition: param.h:177
bool operator()(Param *param1, Param *param2) const
Definition: param.h:179
The class responsible for grouping parameters (objects of the type Param)
Definition: param_group.h:24
The base class of a parameter.
Definition: param.h:30
virtual bool parse(const char *arg)=0
Parses the parameter from the given string.
virtual std::string type() const =0
Returns the string representation of the parameter's type.
std::string argStr
a unique name of the parameter
Definition: param.h:160
std::string m_extInfo
an extended information about the the parameter's purpose
Definition: param.h:164
virtual std::string valToString() const =0
Returns the string representation of the parameter's value.
bool isRequired
a flag indicating if this parameter is required
Definition: param.h:166
virtual bool isNameSimilar(const std::string &filter)
Checks if the param name is similar to the given filter.
Definition: param.h:130
virtual std::string extendedInfo() const
Extended information.
Definition: param.h:155
virtual bool isSet() const =0
Returns true if the parameter is filled, false otherwise.
bool active
a flag indicating if this parameter is available
Definition: param.h:168
virtual std::string info(bool isExtended) const
Definition: param.h:92
bool requiredArg
a flag indicating if this parameter needs to be followed by a value
Definition: param.h:167
void printInColor(int color)
Prints the parameter using the given color. Appends the parameter switch to the name.
Definition: param.h:106
virtual bool isKeywordInDescription(const std::string &keyword)
Checks if the description contains the keyword.
Definition: param.h:146
Param(const std::string &_argStr, bool _isRequired)
A constructor of a parameter.
Definition: param.h:38
void setActive(bool _active)
Definition: param.h:78
virtual bool isActive() const
Returns true if the parameter is active, false otherwise.
Definition: param.h:84
std::string m_info
a basic information about the the parameter's purpose
Definition: param.h:163
virtual bool parse(const wchar_t *arg)
Parses the parameter from the given wide string.
Definition: param.h:71
void printDesc(bool isExtended=true) const
Prints a formatted description of the parameter, including its unique name, type, and the info.
Definition: param.h:114
std::string typeDescStr
a description of the type of the parameter: what type of values are allowed
Definition: param.h:162
Param(const std::string &_argStr, const std::string &_typeDescStr, bool _isRequired)
A constructor of a parameter.
Definition: param.h:52
virtual bool isKeywordInType(const std::string &keyword)
Checks if the parameter type contains the keyword.
Definition: param.h:137
The class responsible for storing and parsing parameters (objects of the type Param),...
Definition: params.h:29
const std::string delimiter
Definition: param.h:601
virtual std::string type() const
Returns the string representation of the parameter's type.
Definition: param.h:591
size_t stripToElements(OUT std::set< std::string > &elements_list)
Definition: param.h:596
StringListParam(const std::string &_argStr, bool _isRequired, char _delimiter)
Definition: param.h:579
StringListParam(const std::string &_argStr, bool _isRequired, std::string _delimiter)
Definition: param.h:585
A parameter storing a string value.
Definition: param.h:276
virtual bool parse(const char *arg)
Parses the parameter from the given string.
Definition: param.h:299
StringParam(const std::string &_argStr, bool _isRequired)
Definition: param.h:278
virtual std::string valToString() const
Returns the string representation of the parameter's value.
Definition: param.h:285
virtual std::string type() const
Returns the string representation of the parameter's type.
Definition: param.h:290
virtual bool isSet() const
Returns true if the parameter is filled, false otherwise.
Definition: param.h:294
size_t copyToCStr(char *buf, size_t buf_max) const
Copy the stored string value into an external buffer of a given length.
Definition: param.h:308
std::string value
Definition: param.h:313
A parameter storing a wide string value.
Definition: param.h:317
virtual bool parse(const wchar_t *arg)
Parses the parameter from the given wide string.
Definition: param.h:342
virtual std::string valToString() const
Returns the string representation of the parameter's value.
Definition: param.h:326
virtual std::string type() const
Returns the string representation of the parameter's type.
Definition: param.h:332
std::wstring value
Definition: param.h:367
virtual bool parse(const char *arg)
Parses the parameter from the given string.
Definition: param.h:350
WStringParam(const std::string &_argStr, bool _isRequired)
Definition: param.h:319
size_t copyToCStr(wchar_t *buf, size_t buf_len) const
Copy the stored string value into an external buffer of a given length.
Definition: param.h:362
virtual bool isSet() const
Returns true if the parameter is filled, false otherwise.
Definition: param.h:337
stringsim_type is_string_similar(const std::string &param, const std::string &filter)
stringsim_type has_keyword(const std::string param, const std::string filter)
bool loadBoolean(IN const T_CHAR *str1, OUT bool &value)
Definition: pk_util.h:65
long get_number(const char *my_buf)
Definition: pk_util.cpp:55
bool is_dec(const char *buf, size_t len)
Definition: pk_util.cpp:16
void print_in_color(int color, const std::string &text)
Definition: pk_util.cpp:93
bool is_number(const char *my_buf)
Definition: pk_util.cpp:45
size_t strip_to_list(IN std::string s, IN std::string delim, OUT std::set< std::string > &elements_list)
Definition: pk_util.cpp:126
size_t copy_to_cstr(T_STR value, T_CHAR *buf, size_t buf_count)
Copy the std::string/std::wstring value into an buffer of a given character count.
Definition: pk_util.h:93
bool is_hex(const char *buf, size_t len)
Definition: pk_util.cpp:4
int loadInt(const T_CHAR *str1, bool isHex=false)
Definition: pk_util.h:49
bool is_hex_with_prefix(const char *buf)
Definition: pk_util.cpp:26
#define PARAM_UNINITIALIZED
Definition: param.h:19
#define PARAM_SWITCH1
The switch used to recognize that the given string should be treated as a parameter (variant 1)
Definition: param.h:22
unsigned __int64 uint64_t
Definition: param.h:25
The set of utility functions used by the ParamKit.
The set of utility functions related with string processing, and finding similarity between strings.