Skip to content

Commit

Permalink
bitcoin-cli helpdetail to show full help for all RPCs
Browse files Browse the repository at this point in the history
Prints to stdout a concatenation of full help text as if you had run
help on every RPC. This allows you to search help text when you
can't remember the name of an RPC.
  • Loading branch information
LarryRuane committed Feb 23, 2024
1 parent 4b1196a commit b5c9008
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 4 deletions.
9 changes: 9 additions & 0 deletions doc/release-notes-29163.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Tools and Utilities
---

A new argument, `helpdetail` has been added to `bitcoin-cli` that
displays the full help text for all RPC methods on standard output.
Typical usage:
```
bitcoin-cli helpdetail | less
```
76 changes: 72 additions & 4 deletions src/rpc/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,15 @@ std::string CRPCTable::help(const std::string& strCommand, const JSONRPCRequest&
{
const CRPCCommand *pcmd = command.second;
std::string strMethod = pcmd->name;
if ((strCommand != "" || pcmd->category == "hidden") && strMethod != strCommand)
continue;

const bool specific_command{strCommand != "" && strCommand != "helpdetail"};
const bool match{strMethod == strCommand};
const bool hidden{pcmd->category == "hidden"};
const bool detail{strCommand != ""};

if (specific_command && !match) continue;
if (!specific_command && hidden) continue;

jreq.strMethod = strMethod;
try
{
Expand All @@ -114,8 +121,14 @@ std::string CRPCTable::help(const std::string& strCommand, const JSONRPCRequest&
{
// Help text is returned in an exception
std::string strHelp = std::string(e.what());
if (strCommand == "")
{
if (detail && !specific_command) {
// Full help for all commands, preface each with its category.
strRet += "== " + Capitalize(pcmd->category) + " ==\n";
}
if (!detail) {
// Print only the one-line summary for each RPC, which is
// the first line of its full help text, and also the
// category when entering a new category.
if (strHelp.find('\n') != std::string::npos)
strHelp = strHelp.substr(0, strHelp.find('\n'));

Expand Down Expand Up @@ -164,6 +177,60 @@ static RPCHelpMan help()
};
}

std::string CRPCTable::helpdetail(const JSONRPCRequest& helpreq) const
{
std::string strRet;
std::set<intptr_t> setDone;
std::vector<std::pair<std::string, const CRPCCommand*> > vCommands;
vCommands.reserve(mapCommands.size());

for (const auto& entry : mapCommands)
vCommands.emplace_back(entry.second.front()->category + entry.first, entry.second.front());
sort(vCommands.begin(), vCommands.end());

JSONRPCRequest jreq = helpreq;
jreq.mode = JSONRPCRequest::GET_HELP;
jreq.params = UniValue();

for (const std::pair<std::string, const CRPCCommand*>& command : vCommands)
{
const CRPCCommand *pcmd = command.second;
jreq.strMethod = pcmd->name;
try
{
UniValue unused_result;
if (setDone.insert(pcmd->unique_id).second || true)
pcmd->actor(jreq, unused_result, /*last_handler=*/true);
}
catch (const std::exception& e)
{
// Help text is returned in an exception
strRet += "== " + Capitalize(pcmd->category) + " ==\n";
strRet += std::string(e.what()) + '\n';
}
}
if (strRet.size() > 0) strRet = strRet.substr(0, strRet.size()-1);
return strRet;
}

static RPCHelpMan helpdetail()
{
return RPCHelpMan{"helpdetail",
"\nList full help for all commands\n",
{},
{
RPCResult{RPCResult::Type::STR, "", "The full help text"},
RPCResult{RPCResult::Type::ANY, "", ""},
},
RPCExamples{""},
[&](const RPCHelpMan& self, const JSONRPCRequest& jsonRequest) -> UniValue
{
return tableRPC.help("helpdetail", jsonRequest);
return tableRPC.helpdetail(jsonRequest);
},
};
}

static RPCHelpMan stop()
{
static const std::string RESULT{PACKAGE_NAME " stopping"};
Expand Down Expand Up @@ -258,6 +325,7 @@ static const CRPCCommand vRPCCommands[]{
/* Overall control/query calls */
{"control", &getrpcinfo},
{"control", &help},
{"control", &helpdetail},
{"control", &stop},
{"control", &uptime},
};
Expand Down
1 change: 1 addition & 0 deletions src/rpc/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ class CRPCTable
public:
CRPCTable();
std::string help(const std::string& name, const JSONRPCRequest& helpreq) const;
std::string helpdetail(const JSONRPCRequest& helpreq) const;

/**
* Execute a method.
Expand Down

0 comments on commit b5c9008

Please sign in to comment.