Replace IsArgKnown() with FlagsOfKnownArg()

This commit is contained in:
Hennadii Stepanov 2019-07-27 14:41:27 +03:00
parent dde80c272a
commit db08edb303
No known key found for this signature in database
GPG key ID: 410108112E7EA81F
2 changed files with 23 additions and 21 deletions

View file

@ -373,6 +373,7 @@ bool ArgsManager::ParseParameters(int argc, const char* const argv[], std::strin
for (int i = 1; i < argc; i++) {
std::string key(argv[i]);
if (key == "-") break; //bitcoin-tx using stdin
std::string val;
size_t is_index = key.find('=');
if (is_index != std::string::npos) {
@ -392,15 +393,13 @@ bool ArgsManager::ParseParameters(int argc, const char* const argv[], std::strin
if (key.length() > 1 && key[1] == '-')
key.erase(0, 1);
// Check that the arg is known
if (!(IsSwitchChar(key[0]) && key.size() == 1)) {
if (!IsArgKnown(key)) {
error = strprintf("Invalid parameter %s", key.c_str());
return false;
}
const unsigned int flags = FlagsOfKnownArg(key);
if (flags) {
InterpretOption(key, val, m_override_args);
} else {
error = strprintf("Invalid parameter %s", key.c_str());
return false;
}
InterpretOption(key, val, m_override_args);
}
// we do not allow -includeconf from command line, so we clear it here
@ -416,7 +415,7 @@ bool ArgsManager::ParseParameters(int argc, const char* const argv[], std::strin
return true;
}
bool ArgsManager::IsArgKnown(const std::string& key) const
unsigned int ArgsManager::FlagsOfKnownArg(const std::string& key) const
{
assert(key[0] == '-');
@ -434,9 +433,12 @@ bool ArgsManager::IsArgKnown(const std::string& key) const
LOCK(cs_args);
for (const auto& arg_map : m_available_args) {
if (arg_map.second.count(base_arg_name)) return true;
const auto search = arg_map.second.find(base_arg_name);
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
@ -835,18 +837,17 @@ bool ArgsManager::ReadConfigStream(std::istream& stream, const std::string& file
}
for (const std::pair<std::string, std::string>& option : options) {
const std::string strKey = std::string("-") + option.first;
// Check that the arg is known
if (!IsArgKnown(strKey)) {
if (!ignore_invalid_keys) {
const unsigned int flags = FlagsOfKnownArg(strKey);
if (flags) {
InterpretOption(strKey, option.second, m_config_args);
} else {
if (ignore_invalid_keys) {
LogPrintf("Ignoring unknown configuration value %s\n", option.first);
} else {
error = strprintf("Invalid configuration value %s", option.first.c_str());
return false;
} else {
LogPrintf("Ignoring unknown configuration value %s\n", option.first);
continue;
}
}
InterpretOption(strKey, option.second, m_config_args);
}
return true;
}

View file

@ -293,9 +293,10 @@ public:
std::string GetHelpMessage() const;
/**
* Check whether we know of this arg
* Return Flags for known arg.
* Return ArgsManager::NONE for unknown arg.
*/
bool IsArgKnown(const std::string& key) const;
unsigned int FlagsOfKnownArg(const std::string& key) const;
};
extern ArgsManager gArgs;