Skip to content

Commit 95eb66d

Browse files
committed
ArgsManager: support config file sections
1 parent 4d34fcc commit 95eb66d

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

src/chainparamsbase.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,5 @@ std::unique_ptr<CBaseChainParams> CreateBaseChainParams(const std::string& chain
4747
void SelectBaseParams(const std::string& chain)
4848
{
4949
globalChainBaseParams = CreateBaseChainParams(chain);
50+
gArgs.SelectConfigNetwork(chain);
5051
}

src/util.cpp

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,13 @@ class ArgsManagerHelper {
460460
public:
461461
typedef std::map<std::string, std::vector<std::string>> MapArgs;
462462

463+
/** Convert regular argument into the network-specific setting */
464+
static inline std::string NetworkArg(const ArgsManager& am, const std::string& arg)
465+
{
466+
assert(arg.length() > 1 && arg[0] == '-');
467+
return "-" + am.m_network + "." + arg.substr(1);
468+
}
469+
463470
/** Find arguments in a map and add them to a vector */
464471
static inline void AddArgs(std::vector<std::string>& res, const MapArgs& map_args, const std::string& arg)
465472
{
@@ -507,6 +514,13 @@ class ArgsManagerHelper {
507514
// But in contrast we return the first argument seen in a config file,
508515
// so "foo=bar \n foo=baz" in the config file gives
509516
// GetArg(am,"foo")={true,"bar"}
517+
if (!am.m_network.empty()) {
518+
found_result = GetArgHelper(am.m_config_args, NetworkArg(am, arg));
519+
if (found_result.first) {
520+
return found_result;
521+
}
522+
}
523+
510524
found_result = GetArgHelper(am.m_config_args, arg);
511525
if (found_result.first) {
512526
return found_result;
@@ -539,9 +553,17 @@ class ArgsManagerHelper {
539553
*/
540554
static bool InterpretNegatedOption(std::string& key, std::string& val)
541555
{
542-
if (key.substr(0, 3) == "-no") {
556+
assert(key[0] == '-');
557+
558+
size_t option_index = key.find('.');
559+
if (option_index == std::string::npos) {
560+
option_index = 1;
561+
} else {
562+
++option_index;
563+
}
564+
if (key.substr(option_index, 2) == "no") {
543565
bool bool_val = InterpretBool(val);
544-
key.erase(1, 2);
566+
key.erase(option_index, 2);
545567
if (!bool_val ) {
546568
// Double negatives like -nofoo=0 are supported (but discouraged)
547569
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)
553575
return false;
554576
}
555577

578+
void ArgsManager::SelectConfigNetwork(const std::string& network)
579+
{
580+
m_network = network;
581+
}
582+
556583
void ArgsManager::ParseParameters(int argc, const char* const argv[])
557584
{
558585
LOCK(cs_args);
@@ -595,6 +622,9 @@ std::vector<std::string> ArgsManager::GetArgs(const std::string& strArg) const
595622

596623
LOCK(cs_args);
597624
ArgsManagerHelper::AddArgs(result, m_override_args, strArg);
625+
if (!m_network.empty()) {
626+
ArgsManagerHelper::AddArgs(result, m_config_args, ArgsManagerHelper::NetworkArg(*this, strArg));
627+
}
598628
ArgsManagerHelper::AddArgs(result, m_config_args, strArg);
599629
return result;
600630
}
@@ -612,6 +642,11 @@ bool ArgsManager::IsArgNegated(const std::string& strArg) const
612642
const auto& ov = m_override_args.find(strArg);
613643
if (ov != m_override_args.end()) return ov->second.empty();
614644

645+
if (!m_network.empty()) {
646+
const auto& cfs = m_config_args.find(ArgsManagerHelper::NetworkArg(*this, strArg));
647+
if (cfs != m_config_args.end()) return cfs->second.empty();
648+
}
649+
615650
const auto& cf = m_config_args.find(strArg);
616651
if (cf != m_config_args.end()) return cf->second.empty();
617652

src/util.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,15 @@ class ArgsManager
228228
mutable CCriticalSection cs_args;
229229
std::map<std::string, std::vector<std::string>> m_override_args;
230230
std::map<std::string, std::vector<std::string>> m_config_args;
231+
std::string m_network;
231232
void ReadConfigStream(std::istream& stream);
232233

233234
public:
235+
/**
236+
* Select the network in use
237+
*/
238+
void SelectConfigNetwork(const std::string& network);
239+
234240
void ParseParameters(int argc, const char*const argv[]);
235241
void ReadConfigFile(const std::string& confPath);
236242

0 commit comments

Comments
 (0)