Merge bitcoin/bitcoin#29277: RPC: access RPC arguments by name

30a6c99935 rpc: access some args by name (stickies-v)
bbb31269bf rpc: add named arg helper (stickies-v)
13525e0c24 rpc: add arg helper unit test (stickies-v)

Pull request description:

  Adds string overloads for the `RPCHelpMan::Arg` and `RPCHelpMan::MaybeArg` helpers to be able to access RPC arguments by name instead of index number. Especially in RPCs with a large number of parameters, this can be quite helpful.

  Example usage:
  ```cpp
  const auto action{self.Arg<std::string>("action")};
  ```

  Most of the LoC is adding test coverage and documentation updates. No behaviour change.

  An alternative approach to #27788 with significantly less overhaul.

ACKs for top commit:
  fjahr:
    Code review ACK 30a6c99935
  maflcko:
    ACK 30a6c99935 🥑
  ryanofsky:
    Code review ACK 30a6c99935. Nice change! Implementation is surprisingly simple and additional unit test coverage is welcome, too.

Tree-SHA512: 4904f5f914fe1d421d32f60edb7c5a028c8ea0f140a2f207a106b4752d441164e073066a6bf2e17693f859fe847815a96609d3cf521e0ac4178d8cd09362ea3d
This commit is contained in:
Ryan Ofsky
2024-04-29 10:14:38 -04:00
8 changed files with 149 additions and 29 deletions

View File

@@ -24,6 +24,8 @@
#include <util/string.h>
#include <util/translation.h>
#include <algorithm>
#include <iterator>
#include <string_view>
#include <tuple>
@@ -729,6 +731,16 @@ std::vector<std::pair<std::string, bool>> RPCHelpMan::GetArgNames() const
return ret;
}
size_t RPCHelpMan::GetParamIndex(std::string_view key) const
{
auto it{std::find_if(
m_args.begin(), m_args.end(), [&key](const auto& arg) { return arg.GetName() == key;}
)};
CHECK_NONFATAL(it != m_args.end()); // TODO: ideally this is checked at compile time
return std::distance(m_args.begin(), it);
}
std::string RPCHelpMan::ToString() const
{
std::string ret;