ParamKit
A small library helping to parse commandline parameters (for Windows).
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
param_group.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 "color_scheme.h"
17 #include "param.h"
18 #include "strings_util.h"
19 
20 namespace paramkit {
21 
22  //---
24  class ParamGroup {
25  public:
27 
30  ParamGroup(const std::string& _name)
32  {
33  this->name = _name;
34  }
35 
37 
45  size_t printGroup(bool printGroupName, bool printRequired, bool hilightMissing, const std::string &filter = "", bool isExtended = false)
46  {
47  if (countParams(printRequired, hilightMissing, filter) == 0) {
48  return 0;
49  }
50  const bool has_filter = filter.length() > 0 ? true : false;
51  size_t printed = 0;
52 
53  if (printGroupName && name.length()) {
54  print_in_color(separatorColor, "\n---" + name + "---\n");
55  }
56  std::set<Param*, ParamCompare>::iterator itr;
57  for (itr = params.begin(); itr != params.end(); ++itr) {
58  Param* param = (*itr);
59 
60  if (!param) continue;
61  if (printRequired != param->isRequired) continue;
62 
63  bool should_print = hilightMissing ? false : true;
64  int color = paramColor;
65  if (hilightMissing && param->isRequired && !param->isSet()) {
66  color = WARNING_COLOR;
67  should_print = true;
68  }
69  if (has_filter) {
70  bool has_any = param->isNameSimilar(filter);
71  color = has_any ? PARAM_SIMILAR_NAME : paramColor;
72  if (!has_any) {
73  has_any = param->isKeywordInDescription(filter);
74  color = has_any ? PARAM_SIMILAR_DESC : paramColor;
75  }
76  if (!has_any) continue;
77  }
78  if (should_print) {
79  if (!param->isActive()) {
80  color = INACTIVE_COLOR;
81  }
82  param->printInColor(color);
83  param->printDesc(isExtended);
84  printed++;
85  }
86  }
87  return printed;
88  }
89 
90  protected:
91 
92  size_t countParams(bool printRequired, bool hilightMissing, const std::string &filter)
93  {
94  const bool has_filter = filter.length() > 0 ? true : false;
95  size_t printed = 0;
96  std::set<Param*, ParamCompare>::iterator itr;
97  for (itr = params.begin(); itr != params.end(); ++itr) {
98  Param* param = (*itr);
99 
100  if (!param) continue;
101  if (printRequired != param->isRequired) continue;
102  bool should_print = hilightMissing ? false : true;
103  if (hilightMissing && param->isRequired && !param->isSet()) {
104  should_print = true;
105  }
106  if (has_filter) {
107  should_print = false;
108  if (param->isNameSimilar(filter) || param->isKeywordInDescription(filter)) {
109  should_print = true;
110  }
111  }
112  if (should_print) {
113  printed++;
114  }
115  }
116  return printed;
117  }
118 
119  bool hasParam(Param *param)
120  {
121  std::set<Param*, ParamCompare>::iterator itr = params.find(param);
122  if (itr != params.end()) {
123  return true;
124  }
125  return false;
126  }
127 
128  bool addParam(Param *param)
129  {
130  if (hasParam(param)) return false;
131  this->params.insert(param);
132  return false;
133  }
134 
135  bool removeParam(Param *param)
136  {
137  std::set<Param*, ParamCompare>::iterator itr = params.find(param);
138  if (itr != params.end()) {
139  params.erase(itr);
140  return true;
141  }
142  return false;
143  }
144 
145  std::string name;
146  std::set<Param*, ParamCompare> params;
147 
148  const int hdrColor;
149  const int paramColor;
150  const int separatorColor;
151 
152  friend class Params;
153  };
154 
155 };
The class responsible for grouping parameters (objects of the type Param)
Definition: param_group.h:24
size_t countParams(bool printRequired, bool hilightMissing, const std::string &filter)
Definition: param_group.h:92
std::set< Param *, ParamCompare > params
Definition: param_group.h:146
bool addParam(Param *param)
Definition: param_group.h:128
size_t printGroup(bool printGroupName, bool printRequired, bool hilightMissing, const std::string &filter="", bool isExtended=false)
Prints the whole group of parameters (their names and descriptions), optionally with the group name.
Definition: param_group.h:45
bool hasParam(Param *param)
Definition: param_group.h:119
bool removeParam(Param *param)
Definition: param_group.h:135
const int separatorColor
Definition: param_group.h:150
ParamGroup(const std::string &_name)
A constructor of a ParamGroup.
Definition: param_group.h:30
The base class of a parameter.
Definition: param.h:30
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 bool isSet() const =0
Returns true if the parameter is filled, false otherwise.
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
virtual bool isActive() const
Returns true if the parameter is active, false otherwise.
Definition: param.h:84
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
The class responsible for storing and parsing parameters (objects of the type Param),...
Definition: params.h:29
const WORD HEADER_COLOR
Definition: color_scheme.h:11
const WORD WARNING_COLOR
Definition: color_scheme.h:8
const WORD PARAM_SIMILAR_DESC
Definition: color_scheme.h:16
void print_in_color(int color, const std::string &text)
Definition: pk_util.cpp:93
const WORD INACTIVE_COLOR
Definition: color_scheme.h:13
const WORD PARAM_SIMILAR_NAME
Definition: color_scheme.h:15
const WORD SEPARATOR_COLOR
Definition: color_scheme.h:12
const WORD HILIGHTED_COLOR
Definition: color_scheme.h:9
Basic parameter types.
The set of utility functions related with string processing, and finding similarity between strings.