0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-02 09:46:52 -05:00

Refactor InterpretNegatedOption() function

- added args parameter
- renamed to InterpretOption()
- removed code duplication
This commit is contained in:
Hennadii Stepanov 2019-07-27 09:56:45 +03:00
parent e0e18a1017
commit e0d187dfeb
No known key found for this signature in database
GPG key ID: 410108112E7EA81F

View file

@ -268,22 +268,21 @@ public:
* This method also tracks when the -no form was supplied, and if so, * This method also tracks when the -no form was supplied, and if so,
* checks whether there was a double-negative (-nofoo=0 -> -foo=1). * checks whether there was a double-negative (-nofoo=0 -> -foo=1).
* *
* If there was not a double negative, it removes the "no" from the key, * If there was not a double negative, it removes the "no" from the key
* and returns true, indicating the caller should clear the args vector * and clears the args vector to indicate a negated option.
* to indicate a negated option.
* *
* If there was a double negative, it removes "no" from the key, sets the * If there was a double negative, it removes "no" from the key, sets the
* value to "1" and returns false. * value to "1" and pushes the key and the updated value to the args vector.
* *
* If there was no "no", it leaves key and value untouched and returns * If there was no "no", it leaves key and value untouched and pushes them
* false. * to the args vector.
* *
* Where an option was negated can be later checked using the * Where an option was negated can be later checked using the
* IsArgNegated() method. One use case for this is to have a way to disable * IsArgNegated() method. One use case for this is to have a way to disable
* options that are not normally boolean (e.g. using -nodebuglogfile to request * options that are not normally boolean (e.g. using -nodebuglogfile to request
* that debug log output is not sent to any file at all). * that debug log output is not sent to any file at all).
*/ */
static bool InterpretNegatedOption(std::string& key, std::string& val) static void InterpretOption(std::string key, std::string val, std::map<std::string, std::vector<std::string>>& args)
{ {
assert(key[0] == '-'); assert(key[0] == '-');
@ -294,17 +293,18 @@ static bool InterpretNegatedOption(std::string& key, std::string& val)
++option_index; ++option_index;
} }
if (key.substr(option_index, 2) == "no") { if (key.substr(option_index, 2) == "no") {
bool bool_val = InterpretBool(val); const bool bool_val = InterpretBool(val);
key.erase(option_index, 2); key.erase(option_index, 2);
if (!bool_val ) { if (!bool_val ) {
// Double negatives like -nofoo=0 are supported (but discouraged) // Double negatives like -nofoo=0 are supported (but discouraged)
LogPrintf("Warning: parsed potentially confusing double-negative %s=%s\n", key, val); LogPrintf("Warning: parsed potentially confusing double-negative %s=%s\n", key, val);
val = "1"; val = "1";
} else { } else {
return true; args[key].clear();
return;
} }
} }
return false; args[key].push_back(val);
} }
ArgsManager::ArgsManager() : ArgsManager::ArgsManager() :
@ -411,12 +411,7 @@ bool ArgsManager::ParseParameters(int argc, const char* const argv[], std::strin
} }
} }
// Check for -nofoo InterpretOption(key, val, m_override_args);
if (InterpretNegatedOption(key, val)) {
m_override_args[key].clear();
} else {
m_override_args[key].push_back(val);
}
} }
// we do not allow -includeconf from command line, so we clear it here // we do not allow -includeconf from command line, so we clear it here
@ -845,7 +840,7 @@ bool ArgsManager::ReadConfigStream(std::istream& stream, const std::string& file
return false; return false;
} }
for (const std::pair<std::string, std::string>& option : options) { for (const std::pair<std::string, std::string>& option : options) {
std::string strKey = std::string("-") + option.first; const std::string strKey = std::string("-") + option.first;
// Check that the arg is known // Check that the arg is known
if (!IsArgKnown(strKey)) { if (!IsArgKnown(strKey)) {
if (!ignore_invalid_keys) { if (!ignore_invalid_keys) {
@ -857,12 +852,7 @@ bool ArgsManager::ReadConfigStream(std::istream& stream, const std::string& file
} }
} }
std::string strValue = option.second; InterpretOption(strKey, option.second, m_config_args);
if (InterpretNegatedOption(strKey, strValue)) {
m_config_args[strKey].clear();
} else {
m_config_args[strKey].push_back(strValue);
}
} }
return true; return true;
} }