Changeset View
Changeset View
Standalone View
Standalone View
src/util.cpp
Show First 20 Lines • Show All 167 Lines • ▼ Show 20 Lines | static bool InterpretBool(const std::string &strValue) { | ||||
return (atoi(strValue) != 0); | return (atoi(strValue) != 0); | ||||
} | } | ||||
/** Internal helper functions for ArgsManager */ | /** Internal helper functions for ArgsManager */ | ||||
class ArgsManagerHelper { | class ArgsManagerHelper { | ||||
public: | public: | ||||
typedef std::map<std::string, std::vector<std::string>> MapArgs; | typedef std::map<std::string, std::vector<std::string>> MapArgs; | ||||
/** Convert regular argument into the network-specific setting */ | |||||
static inline std::string NetworkArg(const ArgsManager &am, | |||||
const std::string &arg) { | |||||
assert(arg.length() > 1 && arg[0] == '-'); | |||||
return "-" + am.m_network + "." + arg.substr(1); | |||||
} | |||||
/** Find arguments in a map and add them to a vector */ | /** Find arguments in a map and add them to a vector */ | ||||
static inline void AddArgs(std::vector<std::string> &res, | static inline void AddArgs(std::vector<std::string> &res, | ||||
const MapArgs &map_args, | const MapArgs &map_args, | ||||
const std::string &arg) { | const std::string &arg) { | ||||
auto it = map_args.find(arg); | auto it = map_args.find(arg); | ||||
if (it != map_args.end()) { | if (it != map_args.end()) { | ||||
res.insert(res.end(), it->second.begin(), it->second.end()); | res.insert(res.end(), it->second.begin(), it->second.end()); | ||||
} | } | ||||
Show All 35 Lines | static inline std::pair<bool, std::string> GetArg(const ArgsManager &am, | ||||
found_result = GetArgHelper(am.m_override_args, arg, true); | found_result = GetArgHelper(am.m_override_args, arg, true); | ||||
if (found_result.first) { | if (found_result.first) { | ||||
return found_result; | return found_result; | ||||
} | } | ||||
// But in contrast we return the first argument seen in a config file, | // But in contrast we return the first argument seen in a config file, | ||||
// so "foo=bar \n foo=baz" in the config file gives | // so "foo=bar \n foo=baz" in the config file gives | ||||
// GetArg(am,"foo")={true,"bar"} | // GetArg(am,"foo")={true,"bar"} | ||||
if (!am.m_network.empty()) { | |||||
found_result = GetArgHelper(am.m_config_args, NetworkArg(am, arg)); | |||||
if (found_result.first) { | |||||
return found_result; | |||||
} | |||||
} | |||||
found_result = GetArgHelper(am.m_config_args, arg); | found_result = GetArgHelper(am.m_config_args, arg); | ||||
if (found_result.first) { | if (found_result.first) { | ||||
return found_result; | return found_result; | ||||
} | } | ||||
return found_result; | return found_result; | ||||
} | } | ||||
}; | }; | ||||
Show All 14 Lines | |||||
* If there was no "no", it leaves key and value untouched and returns false. | * If there was no "no", it leaves key and value untouched and returns false. | ||||
* | * | ||||
* 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 bool InterpretNegatedOption(std::string &key, std::string &val) { | static bool InterpretNegatedOption(std::string &key, std::string &val) { | ||||
if (key.substr(0, 3) == "-no") { | assert(key[0] == '-'); | ||||
size_t option_index = key.find('.'); | |||||
if (option_index == std::string::npos) { | |||||
option_index = 1; | |||||
} else { | |||||
++option_index; | |||||
} | |||||
if (key.substr(option_index, 2) == "no") { | |||||
bool bool_val = InterpretBool(val); | bool bool_val = InterpretBool(val); | ||||
key.erase(1, 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( | 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 { | ||||
return true; | return true; | ||||
} | } | ||||
} | } | ||||
return false; | return false; | ||||
} | } | ||||
void ArgsManager::SelectConfigNetwork(const std::string &network) { | |||||
m_network = network; | |||||
} | |||||
void ArgsManager::ParseParameters(int argc, const char *const argv[]) { | void ArgsManager::ParseParameters(int argc, const char *const argv[]) { | ||||
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]); | ||||
std::string val; | std::string val; | ||||
size_t is_index = key.find('='); | size_t is_index = key.find('='); | ||||
Show All 30 Lines | 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; | ||||
} | } | ||||
LOCK(cs_args); | LOCK(cs_args); | ||||
ArgsManagerHelper::AddArgs(result, m_override_args, strArg); | ArgsManagerHelper::AddArgs(result, m_override_args, strArg); | ||||
if (!m_network.empty()) { | |||||
ArgsManagerHelper::AddArgs( | |||||
result, m_config_args, | |||||
ArgsManagerHelper::NetworkArg(*this, strArg)); | |||||
} | |||||
ArgsManagerHelper::AddArgs(result, m_config_args, strArg); | ArgsManagerHelper::AddArgs(result, m_config_args, strArg); | ||||
return result; | return result; | ||||
} | } | ||||
bool ArgsManager::IsArgSet(const std::string &strArg) const { | bool ArgsManager::IsArgSet(const std::string &strArg) const { | ||||
// special case | // special case | ||||
if (IsArgNegated(strArg)) { | if (IsArgNegated(strArg)) { | ||||
return true; | return true; | ||||
} | } | ||||
return ArgsManagerHelper::GetArg(*this, strArg).first; | return ArgsManagerHelper::GetArg(*this, strArg).first; | ||||
} | } | ||||
bool ArgsManager::IsArgNegated(const std::string &strArg) const { | bool ArgsManager::IsArgNegated(const std::string &strArg) const { | ||||
LOCK(cs_args); | LOCK(cs_args); | ||||
const auto &ov = m_override_args.find(strArg); | const auto &ov = m_override_args.find(strArg); | ||||
if (ov != m_override_args.end()) { | if (ov != m_override_args.end()) { | ||||
return ov->second.empty(); | return ov->second.empty(); | ||||
} | } | ||||
if (!m_network.empty()) { | |||||
const auto &cfs = | |||||
m_config_args.find(ArgsManagerHelper::NetworkArg(*this, strArg)); | |||||
if (cfs != m_config_args.end()) { | |||||
return cfs->second.empty(); | |||||
} | |||||
} | |||||
const auto &cf = m_config_args.find(strArg); | const auto &cf = m_config_args.find(strArg); | ||||
if (cf != m_config_args.end()) { | if (cf != m_config_args.end()) { | ||||
return cf->second.empty(); | return cf->second.empty(); | ||||
} | } | ||||
return false; | return false; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 503 Lines • Show Last 20 Lines |