mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-01-11 04:12:36 -03:00
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:
parent
fd96d54f39
commit
174f7c8080
2 changed files with 66 additions and 25 deletions
80
src/util.cpp
80
src/util.cpp
|
@ -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)
|
||||
{
|
||||
std::pair<OptionsCategory, std::string> key(cat, name);
|
||||
assert(m_available_args.count(key) == 0);
|
||||
m_available_args.emplace(key, std::pair<std::string, bool>(help, debug_only));
|
||||
// Split arg name from its help param
|
||||
size_t eq_index = name.find('=');
|
||||
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()
|
||||
{
|
||||
const bool show_debug = gArgs.GetBoolArg("-help-debug", false);
|
||||
|
||||
std::string usage = HelpMessageGroup("Options:");
|
||||
|
||||
OptionsCategory last_cat = OptionsCategory::OPTIONS;
|
||||
for (auto& arg : m_available_args) {
|
||||
if (arg.first.first != last_cat) {
|
||||
last_cat = arg.first.first;
|
||||
if (last_cat == OptionsCategory::CONNECTION)
|
||||
std::string usage = "";
|
||||
for (const auto& arg_map : m_available_args) {
|
||||
switch(arg_map.first) {
|
||||
case OptionsCategory::OPTIONS:
|
||||
usage += HelpMessageGroup("Options:");
|
||||
break;
|
||||
case OptionsCategory::CONNECTION:
|
||||
usage += HelpMessageGroup("Connection options:");
|
||||
else if (last_cat == OptionsCategory::ZMQ)
|
||||
break;
|
||||
case OptionsCategory::ZMQ:
|
||||
usage += HelpMessageGroup("ZeroMQ notification options:");
|
||||
else if (last_cat == OptionsCategory::DEBUG_TEST)
|
||||
break;
|
||||
case OptionsCategory::DEBUG_TEST:
|
||||
usage += HelpMessageGroup("Debugging/Testing options:");
|
||||
else if (last_cat == OptionsCategory::NODE_RELAY)
|
||||
break;
|
||||
case OptionsCategory::NODE_RELAY:
|
||||
usage += HelpMessageGroup("Node relay options:");
|
||||
else if (last_cat == OptionsCategory::BLOCK_CREATION)
|
||||
break;
|
||||
case OptionsCategory::BLOCK_CREATION:
|
||||
usage += HelpMessageGroup("Block creation options:");
|
||||
else if (last_cat == OptionsCategory::RPC)
|
||||
break;
|
||||
case OptionsCategory::RPC:
|
||||
usage += HelpMessageGroup("RPC server options:");
|
||||
else if (last_cat == OptionsCategory::WALLET)
|
||||
break;
|
||||
case OptionsCategory::WALLET:
|
||||
usage += HelpMessageGroup("Wallet options:");
|
||||
else if (last_cat == OptionsCategory::WALLET_DEBUG_TEST && show_debug)
|
||||
usage += HelpMessageGroup("Wallet debugging/testing options:");
|
||||
else if (last_cat == OptionsCategory::CHAINPARAMS)
|
||||
break;
|
||||
case OptionsCategory::WALLET_DEBUG_TEST:
|
||||
if (show_debug) usage += HelpMessageGroup("Wallet debugging/testing options:");
|
||||
break;
|
||||
case OptionsCategory::CHAINPARAMS:
|
||||
usage += HelpMessageGroup("Chain selection options:");
|
||||
else if (last_cat == OptionsCategory::GUI)
|
||||
break;
|
||||
case OptionsCategory::GUI:
|
||||
usage += HelpMessageGroup("UI Options:");
|
||||
else if (last_cat == OptionsCategory::COMMANDS)
|
||||
break;
|
||||
case OptionsCategory::COMMANDS:
|
||||
usage += HelpMessageGroup("Commands:");
|
||||
else if (last_cat == OptionsCategory::REGISTER_COMMANDS)
|
||||
break;
|
||||
case OptionsCategory::REGISTER_COMMANDS:
|
||||
usage += HelpMessageGroup("Register Commands:");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if (show_debug || !arg.second.second) {
|
||||
usage += HelpMessageOpt(arg.first.second, arg.second.first);
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
return usage;
|
||||
|
|
11
src/util.h
11
src/util.h
|
@ -140,12 +140,21 @@ class ArgsManager
|
|||
protected:
|
||||
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;
|
||||
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;
|
||||
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);
|
||||
|
||||
|
|
Loading…
Reference in a new issue