Skip to content

Commit b70cc5d

Browse files
committed
Revamp option negating policy
1 parent db08edb commit b70cc5d

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

src/util/system.cpp

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,10 @@ class ArgsManagerHelper {
282282
* options that are not normally boolean (e.g. using -nodebuglogfile to request
283283
* that debug log output is not sent to any file at all).
284284
*/
285-
static void InterpretOption(std::string key, std::string val, std::map<std::string, std::vector<std::string>>& args)
285+
286+
NODISCARD static bool InterpretOption(std::string key, std::string val, unsigned int flags,
287+
std::map<std::string, std::vector<std::string>>& args,
288+
std::string& error)
286289
{
287290
assert(key[0] == '-');
288291

@@ -293,18 +296,22 @@ static void InterpretOption(std::string key, std::string val, std::map<std::stri
293296
++option_index;
294297
}
295298
if (key.substr(option_index, 2) == "no") {
296-
const bool bool_val = InterpretBool(val);
297299
key.erase(option_index, 2);
298-
if (!bool_val ) {
300+
if (flags & ArgsManager::ALLOW_BOOL) {
301+
if (InterpretBool(val)) {
302+
args[key].clear();
303+
return true;
304+
}
299305
// Double negatives like -nofoo=0 are supported (but discouraged)
300306
LogPrintf("Warning: parsed potentially confusing double-negative %s=%s\n", key, val);
301307
val = "1";
302308
} else {
303-
args[key].clear();
304-
return;
309+
error = strprintf("Negating of %s is meaningless and therefore forbidden", key.c_str());
310+
return false;
305311
}
306312
}
307313
args[key].push_back(val);
314+
return true;
308315
}
309316

310317
ArgsManager::ArgsManager()
@@ -395,7 +402,9 @@ bool ArgsManager::ParseParameters(int argc, const char* const argv[], std::strin
395402

396403
const unsigned int flags = FlagsOfKnownArg(key);
397404
if (flags) {
398-
InterpretOption(key, val, m_override_args);
405+
if (!InterpretOption(key, val, flags, m_override_args, error)) {
406+
return false;
407+
}
399408
} else {
400409
error = strprintf("Invalid parameter %s", key.c_str());
401410
return false;
@@ -839,7 +848,9 @@ bool ArgsManager::ReadConfigStream(std::istream& stream, const std::string& file
839848
const std::string strKey = std::string("-") + option.first;
840849
const unsigned int flags = FlagsOfKnownArg(strKey);
841850
if (flags) {
842-
InterpretOption(strKey, option.second, m_config_args);
851+
if (!InterpretOption(strKey, option.second, flags, m_config_args, error)) {
852+
return false;
853+
}
843854
} else {
844855
if (ignore_invalid_keys) {
845856
LogPrintf("Ignoring unknown configuration value %s\n", option.first);

0 commit comments

Comments
 (0)