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.
This commit is contained in:
Andrew Chow 2018-05-16 15:15:18 -04:00
parent fd96d54f39
commit 174f7c8080
2 changed files with 66 additions and 25 deletions

View File

@ -549,48 +549,80 @@ void ArgsManager::ForceSetArg(const std::string& strArg, const std::string& strV
void ArgsManager::AddArg(const std::string& name, const std::string& help, const bool debug_only, const OptionsCategory& cat) void ArgsManager::AddArg(const std::string& name, const std::string& help, const bool debug_only, const OptionsCategory& cat)
{ {
std::pair<OptionsCategory, std::string> key(cat, name); // Split arg name from its help param
assert(m_available_args.count(key) == 0); size_t eq_index = name.find('=');
m_available_args.emplace(key, std::pair<std::string, bool>(help, debug_only)); if (eq_index == std::string::npos) {
eq_index = name.size();
}
std::map<std::string, Arg>& arg_map = m_available_args[cat];
auto ret = arg_map.emplace(name.substr(0, eq_index), Arg(name.substr(eq_index, name.size() - eq_index), help, debug_only));
assert(ret.second); // Make sure an insertion actually happened
} }
std::string ArgsManager::GetHelpMessage() std::string ArgsManager::GetHelpMessage()
{ {
const bool show_debug = gArgs.GetBoolArg("-help-debug", false); const bool show_debug = gArgs.GetBoolArg("-help-debug", false);
std::string usage = HelpMessageGroup("Options:"); std::string usage = "";
for (const auto& arg_map : m_available_args) {
OptionsCategory last_cat = OptionsCategory::OPTIONS; switch(arg_map.first) {
for (auto& arg : m_available_args) { case OptionsCategory::OPTIONS:
if (arg.first.first != last_cat) { usage += HelpMessageGroup("Options:");
last_cat = arg.first.first; break;
if (last_cat == OptionsCategory::CONNECTION) case OptionsCategory::CONNECTION:
usage += HelpMessageGroup("Connection options:"); usage += HelpMessageGroup("Connection options:");
else if (last_cat == OptionsCategory::ZMQ) break;
case OptionsCategory::ZMQ:
usage += HelpMessageGroup("ZeroMQ notification options:"); usage += HelpMessageGroup("ZeroMQ notification options:");
else if (last_cat == OptionsCategory::DEBUG_TEST) break;
case OptionsCategory::DEBUG_TEST:
usage += HelpMessageGroup("Debugging/Testing options:"); usage += HelpMessageGroup("Debugging/Testing options:");
else if (last_cat == OptionsCategory::NODE_RELAY) break;
case OptionsCategory::NODE_RELAY:
usage += HelpMessageGroup("Node relay options:"); usage += HelpMessageGroup("Node relay options:");
else if (last_cat == OptionsCategory::BLOCK_CREATION) break;
case OptionsCategory::BLOCK_CREATION:
usage += HelpMessageGroup("Block creation options:"); usage += HelpMessageGroup("Block creation options:");
else if (last_cat == OptionsCategory::RPC) break;
case OptionsCategory::RPC:
usage += HelpMessageGroup("RPC server options:"); usage += HelpMessageGroup("RPC server options:");
else if (last_cat == OptionsCategory::WALLET) break;
case OptionsCategory::WALLET:
usage += HelpMessageGroup("Wallet options:"); usage += HelpMessageGroup("Wallet options:");
else if (last_cat == OptionsCategory::WALLET_DEBUG_TEST && show_debug) break;
usage += HelpMessageGroup("Wallet debugging/testing options:"); case OptionsCategory::WALLET_DEBUG_TEST:
else if (last_cat == OptionsCategory::CHAINPARAMS) if (show_debug) usage += HelpMessageGroup("Wallet debugging/testing options:");
break;
case OptionsCategory::CHAINPARAMS:
usage += HelpMessageGroup("Chain selection options:"); usage += HelpMessageGroup("Chain selection options:");
else if (last_cat == OptionsCategory::GUI) break;
case OptionsCategory::GUI:
usage += HelpMessageGroup("UI Options:"); usage += HelpMessageGroup("UI Options:");
else if (last_cat == OptionsCategory::COMMANDS) break;
case OptionsCategory::COMMANDS:
usage += HelpMessageGroup("Commands:"); usage += HelpMessageGroup("Commands:");
else if (last_cat == OptionsCategory::REGISTER_COMMANDS) break;
case OptionsCategory::REGISTER_COMMANDS:
usage += HelpMessageGroup("Register Commands:"); usage += HelpMessageGroup("Register Commands:");
break;
default:
break;
}
// When we get to the hidden options, stop
if (arg_map.first == OptionsCategory::HIDDEN) break;
for (const auto& arg : arg_map.second) {
if (show_debug || !arg.second.m_debug_only) {
std::string name;
if (arg.second.m_help_param.empty()) {
name = arg.first;
} else {
name = arg.first + arg.second.m_help_param;
}
usage += HelpMessageOpt(name, arg.second.m_help_text);
} }
if (show_debug || !arg.second.second) {
usage += HelpMessageOpt(arg.first.second, arg.second.first);
} }
} }
return usage; return usage;

View File

@ -140,12 +140,21 @@ class ArgsManager
protected: protected:
friend class ArgsManagerHelper; friend class ArgsManagerHelper;
struct Arg
{
std::string m_help_param;
std::string m_help_text;
bool m_debug_only;
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) {};
};
mutable CCriticalSection cs_args; mutable CCriticalSection cs_args;
std::map<std::string, std::vector<std::string>> m_override_args; std::map<std::string, std::vector<std::string>> m_override_args;
std::map<std::string, std::vector<std::string>> m_config_args; std::map<std::string, std::vector<std::string>> m_config_args;
std::string m_network; std::string m_network;
std::set<std::string> m_network_only_args; std::set<std::string> m_network_only_args;
std::map<std::pair<OptionsCategory, std::string>, std::pair<std::string, bool>> m_available_args; std::map<OptionsCategory, std::map<std::string, Arg>> m_available_args;
void ReadConfigStream(std::istream& stream); void ReadConfigStream(std::istream& stream);