ArgsManager: support config file sections

This commit is contained in:
Anthony Towns 2018-04-04 18:03:00 +10:00
parent 4d34fcc713
commit 95eb66d584
3 changed files with 44 additions and 2 deletions

View File

@ -47,4 +47,5 @@ std::unique_ptr<CBaseChainParams> CreateBaseChainParams(const std::string& chain
void SelectBaseParams(const std::string& chain)
{
globalChainBaseParams = CreateBaseChainParams(chain);
gArgs.SelectConfigNetwork(chain);
}

View File

@ -460,6 +460,13 @@ class ArgsManagerHelper {
public:
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 */
static inline void AddArgs(std::vector<std::string>& 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<std::string> 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();

View File

@ -228,9 +228,15 @@ protected:
mutable CCriticalSection cs_args;
std::map<std::string, std::vector<std::string>> m_override_args;
std::map<std::string, std::vector<std::string>> 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);