Skip to content

Commit

Permalink
merge bitcoin#16097: Revamp option negating policy
Browse files Browse the repository at this point in the history
  • Loading branch information
kwvg committed Nov 6, 2021
1 parent aa6ca7a commit ea196a8
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions src/util/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,10 @@ class ArgsManagerHelper {
* options that are not normally boolean (e.g. using -nodebuglogfile to request
* that debug log output is not sent to any file at all).
*/
static void InterpretOption(std::string key, std::string val, std::map<std::string, std::vector<std::string>>& args)

[[nodiscard]] static bool InterpretOption(std::string key, std::string val, unsigned int flags,
std::map<std::string, std::vector<std::string>>& args,
std::string& error)
{
assert(key[0] == '-');

Expand All @@ -319,18 +322,22 @@ static void InterpretOption(std::string key, std::string val, std::map<std::stri
++option_index;
}
if (key.substr(option_index, 2) == "no") {
const bool bool_val = InterpretBool(val);
key.erase(option_index, 2);
if (!bool_val ) {
if (flags & ArgsManager::ALLOW_BOOL) {
if (InterpretBool(val)) {
args[key].clear();
return true;
}
// Double negatives like -nofoo=0 are supported (but discouraged)
LogPrintf("Warning: parsed potentially confusing double-negative %s=%s\n", key, val);
val = "1";
} else {
args[key].clear();
return;
error = strprintf("Negating of %s is meaningless and therefore forbidden", key.c_str());
return false;
}
}
args[key].push_back(val);
return true;
}

ArgsManager::ArgsManager()
Expand Down Expand Up @@ -431,7 +438,9 @@ bool ArgsManager::ParseParameters(int argc, const char* const argv[], std::strin

const unsigned int flags = FlagsOfKnownArg(key);
if (flags) {
InterpretOption(key, val, m_override_args);
if (!InterpretOption(key, val, flags, m_override_args, error)) {
return false;
}
} else {
error = strprintf("Invalid parameter %s", key.c_str());
return false;
Expand Down Expand Up @@ -909,7 +918,9 @@ bool ArgsManager::ReadConfigStream(std::istream& stream, const std::string& file
const std::string strKey = std::string("-") + option.first;
const unsigned int flags = FlagsOfKnownArg(strKey);
if (flags) {
InterpretOption(strKey, option.second, m_config_args);
if (!InterpretOption(strKey, option.second, flags, m_config_args, error)) {
return false;
}
} else {
if (ignore_invalid_keys) {
LogPrintf("Ignoring unknown configuration value %s\n", option.first);
Expand Down

0 comments on commit ea196a8

Please sign in to comment.