Changeset View
Changeset View
Standalone View
Standalone View
src/util/system.cpp
Show First 20 Lines • Show All 303 Lines • ▼ Show 20 Lines | |||||
* If there was no "no", it leaves key and value untouched and pushes them | * If there was no "no", it leaves key and value untouched and pushes them | ||||
* to the args vector. | * to the args vector. | ||||
* | * | ||||
* Where an option was negated can be later checked using the IsArgNegated() | * 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 options that are | * 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 that debug log | * not normally boolean (e.g. using -nodebuglogfile to request that debug log | ||||
* output is not sent to any file at all). | * output is not sent to any file at all). | ||||
*/ | */ | ||||
static void | |||||
InterpretOption(std::string key, std::string val, | NODISCARD static bool | ||||
std::map<std::string, std::vector<std::string>> &args) { | 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] == '-'); | assert(key[0] == '-'); | ||||
size_t option_index = key.find('.'); | size_t option_index = key.find('.'); | ||||
if (option_index == std::string::npos) { | if (option_index == std::string::npos) { | ||||
option_index = 1; | option_index = 1; | ||||
} else { | } else { | ||||
++option_index; | ++option_index; | ||||
} | } | ||||
if (key.substr(option_index, 2) == "no") { | if (key.substr(option_index, 2) == "no") { | ||||
const bool bool_val = InterpretBool(val); | |||||
key.erase(option_index, 2); | 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) | // Double negatives like -nofoo=0 are supported (but discouraged) | ||||
LogPrintf( | LogPrintf( | ||||
"Warning: parsed potentially confusing double-negative %s=%s\n", | "Warning: parsed potentially confusing double-negative %s=%s\n", | ||||
key, val); | key, val); | ||||
val = "1"; | val = "1"; | ||||
} else { | } else { | ||||
args[key].clear(); | error = strprintf( | ||||
return; | "Negating of %s is meaningless and therefore forbidden", | ||||
key.c_str()); | |||||
return false; | |||||
} | } | ||||
} | } | ||||
args[key].push_back(val); | args[key].push_back(val); | ||||
return true; | |||||
} | } | ||||
ArgsManager::ArgsManager() { | ArgsManager::ArgsManager() { | ||||
// nothing to do | // nothing to do | ||||
} | } | ||||
const std::set<std::string> ArgsManager::GetUnsuitableSectionOnlyArgs() const { | const std::set<std::string> ArgsManager::GetUnsuitableSectionOnlyArgs() const { | ||||
std::set<std::string> unsuitables; | std::set<std::string> unsuitables; | ||||
▲ Show 20 Lines • Show All 95 Lines • ▼ Show 20 Lines | for (int i = 1; i < argc; i++) { | ||||
} | } | ||||
std::string val; | std::string val; | ||||
if (!ParseKeyValue(key, val)) { | if (!ParseKeyValue(key, val)) { | ||||
break; | break; | ||||
} | } | ||||
const unsigned int flags = FlagsOfKnownArg(key); | const unsigned int flags = FlagsOfKnownArg(key); | ||||
if (flags) { | if (flags) { | ||||
InterpretOption(key, val, m_override_args); | if (!InterpretOption(key, val, flags, m_override_args, error)) { | ||||
return false; | |||||
} | |||||
} else { | } else { | ||||
error = strprintf("Invalid parameter %s", key.c_str()); | error = strprintf("Invalid parameter %s", key.c_str()); | ||||
return false; | return false; | ||||
} | } | ||||
} | } | ||||
// 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 | ||||
auto it = m_override_args.find("-includeconf"); | auto it = m_override_args.find("-includeconf"); | ||||
▲ Show 20 Lines • Show All 500 Lines • ▼ Show 20 Lines | bool ArgsManager::ReadConfigStream(std::istream &stream, | ||||
if (!GetConfigOptions(stream, filepath, error, options, | if (!GetConfigOptions(stream, filepath, error, options, | ||||
m_config_sections)) { | m_config_sections)) { | ||||
return false; | return false; | ||||
} | } | ||||
for (const std::pair<std::string, std::string> &option : options) { | for (const std::pair<std::string, std::string> &option : options) { | ||||
const std::string strKey = std::string("-") + option.first; | const std::string strKey = std::string("-") + option.first; | ||||
const unsigned int flags = FlagsOfKnownArg(strKey); | const unsigned int flags = FlagsOfKnownArg(strKey); | ||||
if (flags) { | if (flags) { | ||||
InterpretOption(strKey, option.second, m_config_args); | if (!InterpretOption(strKey, option.second, flags, m_config_args, | ||||
error)) { | |||||
return false; | |||||
} | |||||
} else { | } else { | ||||
if (ignore_invalid_keys) { | if (ignore_invalid_keys) { | ||||
LogPrintf("Ignoring unknown configuration value %s\n", | LogPrintf("Ignoring unknown configuration value %s\n", | ||||
option.first); | option.first); | ||||
} else { | } else { | ||||
error = strprintf("Invalid configuration value %s", | error = strprintf("Invalid configuration value %s", | ||||
option.first.c_str()); | option.first.c_str()); | ||||
return false; | return false; | ||||
▲ Show 20 Lines • Show All 410 Lines • Show Last 20 Lines |