Skip to content

Commit 8838c4f

Browse files
naiyomaajtowns
andcommitted
common/args.h: automate check for multiple cli commands
Co-authored-by: Anthony Towns <aj@erisian.com.au>
1 parent 5383637 commit 8838c4f

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

src/bitcoin-cli.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,10 @@ static void SetupCliArgs(ArgsManager& argsman)
8686
"arguments are number of blocks to generate (default: %s) and maximum iterations to try (default: %s), equivalent to "
8787
"RPC generatetoaddress nblocks and maxtries arguments. Example: bitcoin-cli -generate 4 1000",
8888
DEFAULT_NBLOCKS, DEFAULT_MAX_TRIES),
89-
ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
90-
argsman.AddArg("-addrinfo", "Get the number of addresses known to the node, per network and total, after filtering for quality and recency. The total number of addresses known to the node may be higher.", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
91-
argsman.AddArg("-getinfo", "Get general information from the remote server. Note that unlike server-side RPC calls, the output of -getinfo is the result of multiple non-atomic requests. Some entries in the output may represent results from different states (e.g. wallet balance may be as of a different block from the chain state reported)", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
92-
argsman.AddArg("-netinfo", "Get network peer connection information from the remote server. An optional integer argument from 0 to 4 can be passed for different peers listings (default: 0). Pass \"help\" for detailed help documentation.", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
89+
ArgsManager::ALLOW_ANY, OptionsCategory::CLI_COMMANDS);
90+
argsman.AddArg("-addrinfo", "Get the number of addresses known to the node, per network and total, after filtering for quality and recency. The total number of addresses known to the node may be higher.", ArgsManager::ALLOW_ANY, OptionsCategory::CLI_COMMANDS);
91+
argsman.AddArg("-getinfo", "Get general information from the remote server. Note that unlike server-side RPC calls, the output of -getinfo is the result of multiple non-atomic requests. Some entries in the output may represent results from different states (e.g. wallet balance may be as of a different block from the chain state reported)", ArgsManager::ALLOW_ANY, OptionsCategory::CLI_COMMANDS);
92+
argsman.AddArg("-netinfo", "Get network peer connection information from the remote server. An optional integer argument from 0 to 4 can be passed for different peers listings (default: 0). Pass \"help\" for detailed help documentation.", ArgsManager::ALLOW_ANY, OptionsCategory::CLI_COMMANDS);
9393

9494
SetupChainParamsBaseOptions(argsman);
9595
argsman.AddArg("-color=<when>", strprintf("Color setting for CLI output (default: %s). Valid values: always, auto (add color codes when standard output is connected to a terminal and OS is not WIN32), never.", DEFAULT_COLOR_SETTING), ArgsManager::ALLOW_ANY | ArgsManager::DISALLOW_NEGATION, OptionsCategory::OPTIONS);
@@ -1209,6 +1209,7 @@ static int CommandLineRPC(int argc, char *argv[])
12091209
fputc('\n', stdout);
12101210
}
12111211
}
1212+
gArgs.CheckMultipleCLIArgs();
12121213
std::unique_ptr<BaseRequestHandler> rh;
12131214
std::string method;
12141215
if (gArgs.IsArgSet("-getinfo")) {

src/common/args.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <util/fs.h>
1717
#include <util/fs_helpers.h>
1818
#include <util/strencodings.h>
19+
#include <util/string.h>
1920

2021
#ifdef WIN32
2122
#include <codecvt> /* for codecvt_utf8_utf16 */
@@ -587,6 +588,23 @@ void ArgsManager::AddHiddenArgs(const std::vector<std::string>& names)
587588
}
588589
}
589590

591+
void ArgsManager::CheckMultipleCLIArgs() const
592+
{
593+
LOCK(cs_args);
594+
std::vector<std::string> found{};
595+
auto cmds = m_available_args.find(OptionsCategory::CLI_COMMANDS);
596+
if (cmds != m_available_args.end()) {
597+
for (const auto& [cmd, argspec] : cmds->second) {
598+
if (IsArgSet(cmd)) {
599+
found.push_back(cmd);
600+
}
601+
}
602+
if (found.size() > 1) {
603+
throw std::runtime_error(strprintf("Only one of %s may be specified.", util::Join(found, ", ")));
604+
}
605+
}
606+
}
607+
590608
std::string ArgsManager::GetHelpMessage() const
591609
{
592610
const bool show_debug = GetBoolArg("-help-debug", false);
@@ -634,6 +652,9 @@ std::string ArgsManager::GetHelpMessage() const
634652
case OptionsCategory::REGISTER_COMMANDS:
635653
usage += HelpMessageGroup("Register Commands:");
636654
break;
655+
case OptionsCategory::CLI_COMMANDS:
656+
usage += HelpMessageGroup("CLI Commands:");
657+
break;
637658
default:
638659
break;
639660
}

src/common/args.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ enum class OptionsCategory {
6363
GUI,
6464
COMMANDS,
6565
REGISTER_COMMANDS,
66+
CLI_COMMANDS,
6667

6768
HIDDEN // Always the last option to avoid printing these in the help
6869
};
@@ -363,6 +364,13 @@ class ArgsManager
363364
m_network_only_args.clear();
364365
}
365366

367+
/**
368+
* Check CLI command args
369+
*
370+
* @throws std::runtime_error when multiple CLI_COMMAND arguments are specified
371+
*/
372+
void CheckMultipleCLIArgs() const;
373+
366374
/**
367375
* Get the help string
368376
*/

0 commit comments

Comments
 (0)