Skip to content

Commit

Permalink
[FEATURE] Allow to set some parameters as inactive
Browse files Browse the repository at this point in the history
  • Loading branch information
hasherezade committed May 5, 2022
1 parent 0f1d447 commit fd0b043
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
14 changes: 14 additions & 0 deletions paramkit/include/param.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ namespace paramkit {
isRequired = _isRequired;
argStr = _argStr;
requiredArg = false;
active = true;
}

//! A constructor of a parameter
Expand All @@ -54,6 +55,7 @@ namespace paramkit {
argStr = _argStr;
typeDescStr = _typeDescStr;
requiredArg = false;
active = true;
}

//! Returns the string representation of the parameter's value
Expand All @@ -73,6 +75,17 @@ namespace paramkit {
return parse(str.c_str());
}

void setActive(bool _active)
{
this->active = _active;
}

//! Returns true if the parameter is active, false otherwise.
virtual bool isActive() const
{
return this->active;
}

//! Returns true if the parameter is filled, false otherwise.
virtual bool isSet() const = 0;

Expand Down Expand Up @@ -152,6 +165,7 @@ namespace paramkit {

bool isRequired; ///< a flag indicating if this parameter is required
bool requiredArg; ///< a flag indicating if this parameter needs to be followed by a value
bool active; ///< a flag indicating if this parameter is available

friend class Params;
friend class ParamCompare;
Expand Down
3 changes: 3 additions & 0 deletions paramkit/include/param_group.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ namespace paramkit {
if (!has_any) continue;
}
if (should_print) {
if (!param->isActive()) {
color = INACTIVE_COLOR;
}
param->printInColor(color);
param->printDesc(isExtended);
printed++;
Expand Down
22 changes: 19 additions & 3 deletions paramkit/include/params.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ namespace paramkit {
std::map<std::string, Param*>::iterator itr;
for (itr = myParams.begin(); itr != myParams.end(); itr++) {
Param *param = itr->second;
if (param->isRequired && !param->isSet()) {
if (param->isRequired && param->isActive() && !param->isSet()) {
return false;
}
}
Expand Down Expand Up @@ -267,16 +267,32 @@ namespace paramkit {
}
}
if (param_str == param->argStr) {
if (!param->isActive()) {
paramkit::print_in_color(RED, "WARNING: chosen inactive parameter: " + param_str + "\n");
}
// has an argument:
const bool hasArg = (i + 1) < argc &&
( param->requiredArg || !(isParam(to_string(argv[i + 1]))) );
if (hasArg) {
const std::string nextVal = to_string(argv[i + 1]);
i++; // icrement index: move to the next argument
found = true;
//help requested explicitly or parsing failed
if (nextVal == PARAM_HELP1 || !param->parse(nextVal.c_str()) ) {
bool isParsed = false;

if (nextVal == PARAM_HELP1) {
paramHelp = true;
isParsed = true;
}
else {
isParsed = param->parse(nextVal.c_str());
if (!isParsed) paramHelp = true;
}

//help requested explicitly or parsing failed
if (paramHelp) {
if (!isParsed) {
paramkit::print_in_color(RED, "Parsing the parameter failed. Correct options:\n");
}
paramkit::print_in_color(RED, param_str);
param->printDesc();
break;
Expand Down

0 comments on commit fd0b043

Please sign in to comment.