Skip to content

Commit 174f7c8

Browse files
committed
Use a struct for arguments and nested map for categories
Instead of a single map with the category and name as the key, make m_available_args contain maps. The key will be the category and the value is a map which actually contains the arguments for that category. The nested map's key is the argument name, while the value is a struct that contains the help text and whether the argument is a debug only argument.
1 parent fd96d54 commit 174f7c8

File tree

2 files changed

+66
-25
lines changed

2 files changed

+66
-25
lines changed

src/util.cpp

Lines changed: 56 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -549,48 +549,80 @@ void ArgsManager::ForceSetArg(const std::string& strArg, const std::string& strV
549549

550550
void ArgsManager::AddArg(const std::string& name, const std::string& help, const bool debug_only, const OptionsCategory& cat)
551551
{
552-
std::pair<OptionsCategory, std::string> key(cat, name);
553-
assert(m_available_args.count(key) == 0);
554-
m_available_args.emplace(key, std::pair<std::string, bool>(help, debug_only));
552+
// Split arg name from its help param
553+
size_t eq_index = name.find('=');
554+
if (eq_index == std::string::npos) {
555+
eq_index = name.size();
556+
}
557+
558+
std::map<std::string, Arg>& arg_map = m_available_args[cat];
559+
auto ret = arg_map.emplace(name.substr(0, eq_index), Arg(name.substr(eq_index, name.size() - eq_index), help, debug_only));
560+
assert(ret.second); // Make sure an insertion actually happened
555561
}
556562

557563
std::string ArgsManager::GetHelpMessage()
558564
{
559565
const bool show_debug = gArgs.GetBoolArg("-help-debug", false);
560566

561-
std::string usage = HelpMessageGroup("Options:");
562-
563-
OptionsCategory last_cat = OptionsCategory::OPTIONS;
564-
for (auto& arg : m_available_args) {
565-
if (arg.first.first != last_cat) {
566-
last_cat = arg.first.first;
567-
if (last_cat == OptionsCategory::CONNECTION)
567+
std::string usage = "";
568+
for (const auto& arg_map : m_available_args) {
569+
switch(arg_map.first) {
570+
case OptionsCategory::OPTIONS:
571+
usage += HelpMessageGroup("Options:");
572+
break;
573+
case OptionsCategory::CONNECTION:
568574
usage += HelpMessageGroup("Connection options:");
569-
else if (last_cat == OptionsCategory::ZMQ)
575+
break;
576+
case OptionsCategory::ZMQ:
570577
usage += HelpMessageGroup("ZeroMQ notification options:");
571-
else if (last_cat == OptionsCategory::DEBUG_TEST)
578+
break;
579+
case OptionsCategory::DEBUG_TEST:
572580
usage += HelpMessageGroup("Debugging/Testing options:");
573-
else if (last_cat == OptionsCategory::NODE_RELAY)
581+
break;
582+
case OptionsCategory::NODE_RELAY:
574583
usage += HelpMessageGroup("Node relay options:");
575-
else if (last_cat == OptionsCategory::BLOCK_CREATION)
584+
break;
585+
case OptionsCategory::BLOCK_CREATION:
576586
usage += HelpMessageGroup("Block creation options:");
577-
else if (last_cat == OptionsCategory::RPC)
587+
break;
588+
case OptionsCategory::RPC:
578589
usage += HelpMessageGroup("RPC server options:");
579-
else if (last_cat == OptionsCategory::WALLET)
590+
break;
591+
case OptionsCategory::WALLET:
580592
usage += HelpMessageGroup("Wallet options:");
581-
else if (last_cat == OptionsCategory::WALLET_DEBUG_TEST && show_debug)
582-
usage += HelpMessageGroup("Wallet debugging/testing options:");
583-
else if (last_cat == OptionsCategory::CHAINPARAMS)
593+
break;
594+
case OptionsCategory::WALLET_DEBUG_TEST:
595+
if (show_debug) usage += HelpMessageGroup("Wallet debugging/testing options:");
596+
break;
597+
case OptionsCategory::CHAINPARAMS:
584598
usage += HelpMessageGroup("Chain selection options:");
585-
else if (last_cat == OptionsCategory::GUI)
599+
break;
600+
case OptionsCategory::GUI:
586601
usage += HelpMessageGroup("UI Options:");
587-
else if (last_cat == OptionsCategory::COMMANDS)
602+
break;
603+
case OptionsCategory::COMMANDS:
588604
usage += HelpMessageGroup("Commands:");
589-
else if (last_cat == OptionsCategory::REGISTER_COMMANDS)
605+
break;
606+
case OptionsCategory::REGISTER_COMMANDS:
590607
usage += HelpMessageGroup("Register Commands:");
608+
break;
609+
default:
610+
break;
591611
}
592-
if (show_debug || !arg.second.second) {
593-
usage += HelpMessageOpt(arg.first.second, arg.second.first);
612+
613+
// When we get to the hidden options, stop
614+
if (arg_map.first == OptionsCategory::HIDDEN) break;
615+
616+
for (const auto& arg : arg_map.second) {
617+
if (show_debug || !arg.second.m_debug_only) {
618+
std::string name;
619+
if (arg.second.m_help_param.empty()) {
620+
name = arg.first;
621+
} else {
622+
name = arg.first + arg.second.m_help_param;
623+
}
624+
usage += HelpMessageOpt(name, arg.second.m_help_text);
625+
}
594626
}
595627
}
596628
return usage;

src/util.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,21 @@ class ArgsManager
140140
protected:
141141
friend class ArgsManagerHelper;
142142

143+
struct Arg
144+
{
145+
std::string m_help_param;
146+
std::string m_help_text;
147+
bool m_debug_only;
148+
149+
Arg(const std::string& help_param, const std::string& help_text, bool debug_only) : m_help_param(help_param), m_help_text(help_text), m_debug_only(debug_only) {};
150+
};
151+
143152
mutable CCriticalSection cs_args;
144153
std::map<std::string, std::vector<std::string>> m_override_args;
145154
std::map<std::string, std::vector<std::string>> m_config_args;
146155
std::string m_network;
147156
std::set<std::string> m_network_only_args;
148-
std::map<std::pair<OptionsCategory, std::string>, std::pair<std::string, bool>> m_available_args;
157+
std::map<OptionsCategory, std::map<std::string, Arg>> m_available_args;
149158

150159
void ReadConfigStream(std::istream& stream);
151160

0 commit comments

Comments
 (0)