Changeset View
Changeset View
Standalone View
Standalone View
src/util/system.cpp
Show First 20 Lines • Show All 429 Lines • ▼ Show 20 Lines | |||||
bool ArgsManager::ParseParameters(int argc, const char *const argv[], | bool ArgsManager::ParseParameters(int argc, const char *const argv[], | ||||
std::string &error) { | std::string &error) { | ||||
LOCK(cs_args); | LOCK(cs_args); | ||||
m_override_args.clear(); | m_override_args.clear(); | ||||
for (int i = 1; i < argc; i++) { | for (int i = 1; i < argc; i++) { | ||||
std::string key(argv[i]); | std::string key(argv[i]); | ||||
if (key == "-") { | |||||
// bitcoin-tx using stdin | |||||
break; | |||||
} | |||||
std::string val; | std::string val; | ||||
if (!ParseKeyValue(key, val)) { | if (!ParseKeyValue(key, val)) { | ||||
break; | break; | ||||
} | } | ||||
// Check that the arg is known | const unsigned int flags = FlagsOfKnownArg(key); | ||||
if (!(IsSwitchChar(key[0]) && key.size() == 1)) { | if (flags) { | ||||
if (!IsArgKnown(key)) { | InterpretOption(key, val, m_override_args); | ||||
} else { | |||||
error = strprintf("Invalid parameter %s", key.c_str()); | error = strprintf("Invalid parameter %s", key.c_str()); | ||||
return false; | return false; | ||||
} | } | ||||
} | } | ||||
InterpretOption(key, val, m_override_args); | |||||
} | |||||
// 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"); | ||||
if (it != m_override_args.end()) { | if (it != m_override_args.end()) { | ||||
if (it->second.size() > 0) { | if (it->second.size() > 0) { | ||||
for (const auto &ic : it->second) { | for (const auto &ic : it->second) { | ||||
error += "-includeconf cannot be used from commandline; " | error += "-includeconf cannot be used from commandline; " | ||||
"-includeconf=" + | "-includeconf=" + | ||||
ic + "\n"; | ic + "\n"; | ||||
} | } | ||||
return false; | return false; | ||||
} | } | ||||
} | } | ||||
return true; | return true; | ||||
} | } | ||||
bool ArgsManager::IsArgKnown(const std::string &key) const { | unsigned int ArgsManager::FlagsOfKnownArg(const std::string &key) const { | ||||
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") { | ||||
option_index += 2; | option_index += 2; | ||||
} | } | ||||
const std::string base_arg_name = '-' + key.substr(option_index); | const std::string base_arg_name = '-' + key.substr(option_index); | ||||
LOCK(cs_args); | LOCK(cs_args); | ||||
for (const auto &arg_map : m_available_args) { | for (const auto &arg_map : m_available_args) { | ||||
if (arg_map.second.count(base_arg_name)) { | const auto search = arg_map.second.find(base_arg_name); | ||||
return true; | if (search != arg_map.second.end()) { | ||||
return search->second.m_flags; | |||||
} | } | ||||
} | } | ||||
return false; | return ArgsManager::NONE; | ||||
} | } | ||||
std::vector<std::string> ArgsManager::GetArgs(const std::string &strArg) const { | std::vector<std::string> ArgsManager::GetArgs(const std::string &strArg) const { | ||||
std::vector<std::string> result = {}; | std::vector<std::string> result = {}; | ||||
// special case | // special case | ||||
if (IsArgNegated(strArg)) { | if (IsArgNegated(strArg)) { | ||||
return result; | return result; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 454 Lines • ▼ Show 20 Lines | bool ArgsManager::ReadConfigStream(std::istream &stream, | ||||
LOCK(cs_args); | LOCK(cs_args); | ||||
std::vector<std::pair<std::string, std::string>> options; | std::vector<std::pair<std::string, std::string>> options; | ||||
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; | ||||
// Check that the arg is known | const unsigned int flags = FlagsOfKnownArg(strKey); | ||||
if (!IsArgKnown(strKey)) { | if (flags) { | ||||
if (!ignore_invalid_keys) { | InterpretOption(strKey, option.second, m_config_args); | ||||
error = strprintf("Invalid configuration value %s", | |||||
option.first.c_str()); | |||||
return false; | |||||
} else { | } else { | ||||
if (ignore_invalid_keys) { | |||||
LogPrintf("Ignoring unknown configuration value %s\n", | LogPrintf("Ignoring unknown configuration value %s\n", | ||||
option.first); | option.first); | ||||
continue; | } else { | ||||
error = strprintf("Invalid configuration value %s", | |||||
option.first.c_str()); | |||||
return false; | |||||
} | } | ||||
} | } | ||||
InterpretOption(strKey, option.second, m_config_args); | |||||
} | } | ||||
return true; | return true; | ||||
} | } | ||||
bool ArgsManager::ReadConfigFiles(std::string &error, | bool ArgsManager::ReadConfigFiles(std::string &error, | ||||
bool ignore_invalid_keys) { | bool ignore_invalid_keys) { | ||||
{ | { | ||||
LOCK(cs_args); | LOCK(cs_args); | ||||
▲ Show 20 Lines • Show All 400 Lines • Show Last 20 Lines |