21 #define PARAM_HELP1 "?"
22 #define PARAM_HELP2 "help"
23 #define PARAM_VERSION "version"
24 #define PARAM_VERSION2 "ver"
31 Params(
const std::string &version =
"")
63 if(!group)
return false;
73 std::map<std::string, ParamGroup*>::iterator itr = this->
paramGroups.find(str);
94 const std::string argStr = param->
argStr;
110 bool setInfo(
const std::string& paramName,
const std::string& basic_info,
const std::string& extended_info =
"")
113 if (!p)
return false;
126 void printInfo(
bool hilightMissing=
false,
const std::string &filter =
"",
bool isExtended =
true)
128 std::cout <<
"---" << std::endl;
129 _info(
true, hilightMissing, filter, isExtended);
130 _info(
false, hilightMissing, filter, isExtended);
131 const bool extendedInfoS = (filter.empty() && !hilightMissing) ? isExtended :
false;
133 std::cout <<
"---" << std::endl;
151 if (!p)
return false;
167 std::map<std::string, Param*>::iterator itr = this->
myParams.find(paramName);
182 virtual bool isSet(
const std::string& paramName)
184 std::map<std::string, Param*>::iterator itr = this->
myParams.find(paramName);
185 if (itr == this->
myParams.end())
return false;
187 Param *param = itr->second;
191 return param->
isSet();
197 std::map<std::string, Param*>::iterator itr;
199 Param *param = itr->second;
212 std::map<std::string, ParamGroup*>::iterator itr;
224 std::map<std::string, Param*>::iterator itr;
226 Param *param = itr->second;
233 template <
typename T_CHAR>
234 bool parse(
int argc, T_CHAR* argv[])
236 bool helpRequested =
false;
238 for (
int i = 1; i < argc; i++) {
239 std::string param_str =
to_string(argv[i]);
247 std::map<std::string, Param*>::iterator itr;
250 Param *param = itr->second;
255 const std::string nextVal =
to_string(argv[i + 1]);
260 const bool shouldExpand = (param_str ==
PARAM_HELP1) ?
false :
true;
270 if (param_str == param->
argStr) {
275 const bool hasArg = (i + 1) < argc &&
278 const std::string nextVal =
to_string(argv[i + 1]);
281 bool isParsed =
false;
285 helpRequested =
true;
289 isParsed = param->
parse(nextVal.c_str());
292 helpRequested =
true;
309 param->
parse((
char*)
nullptr);
316 helpRequested =
true;
341 std::stringstream ss1;
352 std::map<std::string, Param*>::iterator itr;
354 if (!
isSet(itr->first))
continue;
356 Param *param = itr->second;
357 if (!param)
continue;
361 std::cout << std::hex << param->
valToString() <<
"\n";
365 template <
class PARAM_T,
typename FIELD_T>
366 bool copyVal(
const std::string ¶mId, FIELD_T &toFill)
368 PARAM_T *myParam =
dynamic_cast<PARAM_T*
>(this->
getParam(paramId));
372 if (!myParam->isSet()) {
375 toFill =
static_cast<FIELD_T
>(myParam->value);
379 template <
class PARAM_T,
typename FIELD_T>
380 bool copyCStr(
const std::string ¶mId, FIELD_T &toFill,
size_t toFillLen)
382 PARAM_T *myStr =
dynamic_cast<PARAM_T*
>(this->
getParam(paramId));
383 if (!myStr || !myStr->isSet()) {
386 myStr->copyToCStr(toFill, toFillLen);
395 std::map<std::string, Param*>::iterator itr;
397 Param *param = itr->second;
398 if (param->
isRequired != isRequired)
continue;
399 if (param->
isSet()) {
406 size_t _info(
bool isRequired,
bool hilightMissing,
const std::string &filter,
bool isExtended)
408 const bool has_filter = filter.length() > 0 ? true :
false;
409 std::map<std::string, Param*>::iterator itr;
412 const std::string desc = isRequired ?
"Required:" :
"Optional:";
415 size_t total_count = 0;
416 bool printGroupName = (
countGroups(isRequired, hilightMissing, filter)) ?
true :
false;
418 std::map<std::string, ParamGroup*>::iterator groupItr;
421 if (!group)
continue;
422 printed += group->
printGroup(printGroupName, isRequired, hilightMissing, filter, isExtended);
423 total_count += group->
countParams(isRequired,
false,
"");
425 if (printed < total_count) {
433 bool printHelp(
const std::string helpArg,
bool shouldExpand)
435 if (helpArg.empty()) {
437 this->
printInfo(
false,
"", shouldExpand);
451 std::cout <<
"Application version is not set\n";
455 this->
printInfo(
false, helpArg, shouldExpand);
464 std::map<std::string, Param*>::iterator itr =
myParams.begin();
465 std::stringstream ss1;
485 if (!param || !group) {
488 std::map<Param*, ParamGroup*>::iterator itr =
paramToGroup.find(param);
491 if (currentGroup != group) {
496 group->
params.insert(param);
501 size_t countGroups(
bool required,
bool hilightMissing,
const std::string &filter)
const
503 size_t groups_count = 0;
504 std::map<std::string, ParamGroup*>::const_iterator itr;
508 if (group->
countParams(required, hilightMissing, filter) > 0) {
519 std::map<std::string, Param*>::iterator itr;
521 Param *param = itr->second;
530 std::cout << param <<
"\n";
536 std::cout << str <<
"\n";
542 std::map<std::string, Param*>::iterator itr = this->
myParams.find(str);
552 const size_t prefixLen = 1;
553 const size_t len = str.length();
554 if (len <= prefixLen)
return false;
565 size_t prefixLen = 1;
566 const size_t len = str.length();
567 if (len < prefixLen)
return str;
577 return str.substr(prefixLen);
A parameter storing a boolean value.
A parameter storing an integer value.
The class responsible for grouping parameters (objects of the type Param)
size_t countParams(bool printRequired, bool hilightMissing, const std::string &filter)
std::set< Param *, ParamCompare > params
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.
bool removeParam(Param *param)
The base class of a parameter.
virtual bool parse(const char *arg)=0
Parses the parameter from the given string.
std::string argStr
a unique name of the parameter
std::string m_extInfo
an extended information about the the parameter's purpose
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
virtual bool isSet() const =0
Returns true if the parameter is filled, false otherwise.
bool requiredArg
a flag indicating if this parameter needs to be followed by a value
void printInColor(int color)
Prints the parameter using the given color. Appends the parameter switch to the name.
virtual bool isActive() const
Returns true if the parameter is active, false otherwise.
std::string m_info
a basic information about the the parameter's purpose
void printDesc(bool isExtended=true) const
Prints a formatted description of the parameter, including its unique name, type, and the info.
The class responsible for storing and parsing parameters (objects of the type Param),...
std::string skipParamPrefix(std::string &str)
Skip the parameter prefix. Example: "/param", '-param', or "--param" is converted to "param".
std::map< std::string, Param * > myParams
bool addGroup(ParamGroup *group)
bool addParamToGroup(const std::string paramName, const std::string groupName)
void printUnknownParam(const std::string ¶m)
static bool isParam(const std::string &str)
Checks if the string starts from the parameter switch.
void printInfoSection(bool isExtended)
void printInfo(bool hilightMissing=false, const std::string &filter="", bool isExtended=true)
Prints info about all the parameters. Optionally hilights the required ones that are missing.
virtual void printBanner()
bool setInfo(const std::string ¶mName, const std::string &basic_info, const std::string &extended_info="")
Sets the information about the parameter, defined by its name.
size_t _info(bool isRequired, bool hilightMissing, const std::string &filter, bool isExtended)
Param * getParam(const std::string &str)
Retrieve the parameter by its unique name. Returns nullptr if such parameter does not exist.
bool printHelp(const std::string helpArg, bool shouldExpand)
virtual size_t countFilled(bool isRequired)
uint64_t getIntValue(const std::string ¶mName)
Gets an integer value of the IntParam defined by its name. If such parameter does not exist,...
void print()
Prints the values of all the parameters that are currently set.
bool addParamToGroup(Param *param, ParamGroup *group)
void printUnknownArgument(const std::string &str)
size_t countCategory(bool isRequired)
Returns the number of parameters of particular category: required or optional.
bool setIntValue(const std::string ¶mName, uint64_t val)
Fills an IntParam defined by its name with the given value. If such parameter does not exist,...
bool copyCStr(const std::string ¶mId, FIELD_T &toFill, size_t toFillLen)
ParamGroup * generalGroup
size_t countGroups(bool required, bool hilightMissing, const std::string &filter) const
void releaseParams()
Deletes all the added parameters.
virtual bool hasRequiredFilled()
Checks if all the required parameters are filled.
virtual bool isSet(const std::string ¶mName)
Checks if the parameter with the given name is set (filled).
ParamGroup * getParamGroup(const std::string &str)
virtual void printVersionInfo()
Params(const std::string &version="")
bool copyVal(const std::string ¶mId, FIELD_T &toFill)
void addParam(Param *param)
Adds a parameter into the storage.
void printBriefInfo()
Prints brief info about all the parameters. Wrapper for printInfo.
std::map< Param *, ParamGroup * > paramToGroup
void releaseGroups()
Deletes all the parameters groups.
std::map< std::string, ParamGroup * > paramGroups
bool parse(int argc, T_CHAR *argv[])
Parses the parameters. Prints a warning if an undefined parameter was supplied.
A parameter storing a string value.
void print_in_color(int color, const std::string &text)
const WORD INACTIVE_COLOR
std::string to_string(T_CHAR *str1)
const WORD HILIGHTED_COLOR
#define PARAM_UNINITIALIZED
#define PARAM_SWITCH1
The switch used to recognize that the given string should be treated as a parameter (variant 1)
#define PARAM_SWITCH2
The switch used to recognize that the given string should be treated as a parameter (variant 2)
unsigned __int64 uint64_t
The set of utility functions used by the ParamKit.