From 95eb66d5842e5ccdeb7481b9ee92ac4aface6b0f Mon Sep 17 00:00:00 2001 From: Anthony Towns Date: Wed, 4 Apr 2018 18:03:00 +1000 Subject: [PATCH] ArgsManager: support config file sections --- src/chainparamsbase.cpp | 1 + src/util.cpp | 39 +++++++++++++++++++++++++++++++++++++-- src/util.h | 6 ++++++ 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/chainparamsbase.cpp b/src/chainparamsbase.cpp index e840a2ed30..3ef9c2cfe5 100644 --- a/src/chainparamsbase.cpp +++ b/src/chainparamsbase.cpp @@ -47,4 +47,5 @@ std::unique_ptr CreateBaseChainParams(const std::string& chain void SelectBaseParams(const std::string& chain) { globalChainBaseParams = CreateBaseChainParams(chain); + gArgs.SelectConfigNetwork(chain); } diff --git a/src/util.cpp b/src/util.cpp index 3e69574022..85c3d221af 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -460,6 +460,13 @@ class ArgsManagerHelper { public: typedef std::map> 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 */ static inline void AddArgs(std::vector& res, const MapArgs& map_args, const std::string& arg) { @@ -507,6 +514,13 @@ public: // But in contrast we return the first argument seen in a config file, // so "foo=bar \n foo=baz" in the config file gives // 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); if (found_result.first) { return found_result; @@ -539,9 +553,17 @@ public: */ 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); - key.erase(1, 2); + key.erase(option_index, 2); if (!bool_val ) { // Double negatives like -nofoo=0 are supported (but discouraged) LogPrintf("Warning: parsed potentially confusing double-negative %s=%s\n", key, val); @@ -553,6 +575,11 @@ static bool InterpretNegatedOption(std::string& key, std::string& val) return false; } +void ArgsManager::SelectConfigNetwork(const std::string& network) +{ + m_network = network; +} + void ArgsManager::ParseParameters(int argc, const char* const argv[]) { LOCK(cs_args); @@ -595,6 +622,9 @@ std::vector ArgsManager::GetArgs(const std::string& strArg) const LOCK(cs_args); 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); return result; } @@ -612,6 +642,11 @@ bool ArgsManager::IsArgNegated(const std::string& strArg) const const auto& ov = m_override_args.find(strArg); if (ov != m_override_args.end()) 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); if (cf != m_config_args.end()) return cf->second.empty(); diff --git a/src/util.h b/src/util.h index 6b80afc58f..ca6523ab14 100644 --- a/src/util.h +++ b/src/util.h @@ -228,9 +228,15 @@ protected: mutable CCriticalSection cs_args; std::map> m_override_args; std::map> m_config_args; + std::string m_network; void ReadConfigStream(std::istream& stream); public: + /** + * Select the network in use + */ + void SelectConfigNetwork(const std::string& network); + void ParseParameters(int argc, const char*const argv[]); void ReadConfigFile(const std::string& confPath);