Skip to content

Commit

Permalink
common/args.h: automate check for multiple cli commands
Browse files Browse the repository at this point in the history
  • Loading branch information
ajtowns committed May 27, 2024
1 parent d77cc43 commit 6f9392c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
7 changes: 1 addition & 6 deletions src/bitcoin-cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1175,12 +1175,7 @@ static int CommandLineRPC(int argc, char *argv[])
fputc('\n', stdout);
}
}
if (gArgs.IsArgSet("-getinfo") +
gArgs.GetBoolArg("-netinfo", false) +
gArgs.GetBoolArg("-generate", false) +
gArgs.GetBoolArg("-addrinfo", false) > 1) {
throw std::runtime_error("Only one of \"-getinfo\", \"-netinfo\", \"-generate\", and \"-addrinfo\" may be specified.");
}
gArgs.CheckMultipleCLIArgs();
std::unique_ptr<BaseRequestHandler> rh;
std::string method;
if (gArgs.IsArgSet("-getinfo")) {
Expand Down
17 changes: 17 additions & 0 deletions src/common/args.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,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.", Join(found, ", ")));
}
}
}

std::string ArgsManager::GetHelpMessage() const
{
const bool show_debug = GetBoolArg("-help-debug", false);
Expand Down
6 changes: 6 additions & 0 deletions src/common/args.h
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,12 @@ class ArgsManager
m_network_only_args.clear();
}

/**
* Check CLI command args
* (throws std::runtime_error when multiple CLI_COMMAND arguments are specified)
*/
void CheckMultipleCLIArgs() const;

/**
* Get the help string
*/
Expand Down

0 comments on commit 6f9392c

Please sign in to comment.