common/args.h: automate check for multiple cli commands

Co-authored-by: Anthony Towns <aj@erisian.com.au>
This commit is contained in:
naiyoma
2024-05-18 23:16:31 +03:00
parent 538363738e
commit 8838c4f171
3 changed files with 34 additions and 4 deletions

View File

@@ -16,6 +16,7 @@
#include <util/fs.h>
#include <util/fs_helpers.h>
#include <util/strencodings.h>
#include <util/string.h>
#ifdef WIN32
#include <codecvt> /* for codecvt_utf8_utf16 */
@@ -587,6 +588,23 @@ void ArgsManager::AddHiddenArgs(const std::vector<std::string>& names)
}
}
void ArgsManager::CheckMultipleCLIArgs() const
{
LOCK(cs_args);
std::vector<std::string> found{};
auto cmds = m_available_args.find(OptionsCategory::CLI_COMMANDS);
if (cmds != m_available_args.end()) {
for (const auto& [cmd, argspec] : cmds->second) {
if (IsArgSet(cmd)) {
found.push_back(cmd);
}
}
if (found.size() > 1) {
throw std::runtime_error(strprintf("Only one of %s may be specified.", util::Join(found, ", ")));
}
}
}
std::string ArgsManager::GetHelpMessage() const
{
const bool show_debug = GetBoolArg("-help-debug", false);
@@ -634,6 +652,9 @@ std::string ArgsManager::GetHelpMessage() const
case OptionsCategory::REGISTER_COMMANDS:
usage += HelpMessageGroup("Register Commands:");
break;
case OptionsCategory::CLI_COMMANDS:
usage += HelpMessageGroup("CLI Commands:");
break;
default:
break;
}