Skip to content

Commit e0d187d

Browse files
committed
Refactor InterpretNegatedOption() function
- added args parameter - renamed to InterpretOption() - removed code duplication
1 parent e0e18a1 commit e0d187d

File tree

1 file changed

+13
-23
lines changed

1 file changed

+13
-23
lines changed

src/util/system.cpp

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -268,22 +268,21 @@ class ArgsManagerHelper {
268268
* This method also tracks when the -no form was supplied, and if so,
269269
* checks whether there was a double-negative (-nofoo=0 -> -foo=1).
270270
*
271-
* If there was not a double negative, it removes the "no" from the key,
272-
* and returns true, indicating the caller should clear the args vector
273-
* to indicate a negated option.
271+
* If there was not a double negative, it removes the "no" from the key
272+
* and clears the args vector to indicate a negated option.
274273
*
275274
* If there was a double negative, it removes "no" from the key, sets the
276-
* value to "1" and returns false.
275+
* value to "1" and pushes the key and the updated value to the args vector.
277276
*
278-
* If there was no "no", it leaves key and value untouched and returns
279-
* false.
277+
* If there was no "no", it leaves key and value untouched and pushes them
278+
* to the args vector.
280279
*
281280
* Where an option was negated can be later checked using the
282281
* IsArgNegated() method. One use case for this is to have a way to disable
283282
* options that are not normally boolean (e.g. using -nodebuglogfile to request
284283
* that debug log output is not sent to any file at all).
285284
*/
286-
static bool InterpretNegatedOption(std::string& key, std::string& val)
285+
static void InterpretOption(std::string key, std::string val, std::map<std::string, std::vector<std::string>>& args)
287286
{
288287
assert(key[0] == '-');
289288

@@ -294,17 +293,18 @@ static bool InterpretNegatedOption(std::string& key, std::string& val)
294293
++option_index;
295294
}
296295
if (key.substr(option_index, 2) == "no") {
297-
bool bool_val = InterpretBool(val);
296+
const bool bool_val = InterpretBool(val);
298297
key.erase(option_index, 2);
299298
if (!bool_val ) {
300299
// Double negatives like -nofoo=0 are supported (but discouraged)
301300
LogPrintf("Warning: parsed potentially confusing double-negative %s=%s\n", key, val);
302301
val = "1";
303302
} else {
304-
return true;
303+
args[key].clear();
304+
return;
305305
}
306306
}
307-
return false;
307+
args[key].push_back(val);
308308
}
309309

310310
ArgsManager::ArgsManager() :
@@ -411,12 +411,7 @@ bool ArgsManager::ParseParameters(int argc, const char* const argv[], std::strin
411411
}
412412
}
413413

414-
// Check for -nofoo
415-
if (InterpretNegatedOption(key, val)) {
416-
m_override_args[key].clear();
417-
} else {
418-
m_override_args[key].push_back(val);
419-
}
414+
InterpretOption(key, val, m_override_args);
420415
}
421416

422417
// 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
845840
return false;
846841
}
847842
for (const std::pair<std::string, std::string>& option : options) {
848-
std::string strKey = std::string("-") + option.first;
843+
const std::string strKey = std::string("-") + option.first;
849844
// Check that the arg is known
850845
if (!IsArgKnown(strKey)) {
851846
if (!ignore_invalid_keys) {
@@ -857,12 +852,7 @@ bool ArgsManager::ReadConfigStream(std::istream& stream, const std::string& file
857852
}
858853
}
859854

860-
std::string strValue = option.second;
861-
if (InterpretNegatedOption(strKey, strValue)) {
862-
m_config_args[strKey].clear();
863-
} else {
864-
m_config_args[strKey].push_back(strValue);
865-
}
855+
InterpretOption(strKey, option.second, m_config_args);
866856
}
867857
return true;
868858
}

0 commit comments

Comments
 (0)