Skip to content

Commit fa05626

Browse files
author
MarcoFalke
committed
rpc: Add RPCHelpMan::IsValidNumArgs()
1 parent f9775a8 commit fa05626

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

src/rpc/util.cpp

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2017-2018 The Bitcoin Core developers
1+
// Copyright (c) 2017-2019 The Bitcoin Core developers
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

@@ -315,6 +315,17 @@ std::string RPCExamples::ToDescriptionString() const
315315
return m_examples.empty() ? m_examples : "\nExamples:\n" + m_examples;
316316
}
317317

318+
bool RPCHelpMan::IsValidNumArgs(size_t num_args) const
319+
{
320+
size_t num_required_args = 0;
321+
for (size_t n = m_args.size(); n > 0; --n) {
322+
if (!m_args.at(n - 1).IsOptional()) {
323+
num_required_args = n;
324+
break;
325+
}
326+
}
327+
return num_required_args <= num_args && num_args <= m_args.size();
328+
}
318329
std::string RPCHelpMan::ToString() const
319330
{
320331
std::string ret;
@@ -323,12 +334,7 @@ std::string RPCHelpMan::ToString() const
323334
ret += m_name;
324335
bool was_optional{false};
325336
for (const auto& arg : m_args) {
326-
bool optional;
327-
if (arg.m_fallback.which() == 1) {
328-
optional = true;
329-
} else {
330-
optional = RPCArg::Optional::NO != boost::get<RPCArg::Optional>(arg.m_fallback);
331-
}
337+
const bool optional = arg.IsOptional();
332338
ret += " ";
333339
if (optional) {
334340
if (!was_optional) ret += "( ";
@@ -370,6 +376,15 @@ std::string RPCHelpMan::ToString() const
370376
return ret;
371377
}
372378

379+
bool RPCArg::IsOptional() const
380+
{
381+
if (m_fallback.which() == 1) {
382+
return true;
383+
} else {
384+
return RPCArg::Optional::NO != boost::get<RPCArg::Optional>(m_fallback);
385+
}
386+
}
387+
373388
std::string RPCArg::ToDescriptionString() const
374389
{
375390
std::string ret;

src/rpc/util.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2017-2018 The Bitcoin Core developers
1+
// Copyright (c) 2017-2019 The Bitcoin Core developers
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

@@ -53,7 +53,7 @@ struct RPCArg {
5353
/** Required arg */
5454
NO,
5555
/**
56-
* Optinal arg that is a named argument and has a default value of
56+
* Optional arg that is a named argument and has a default value of
5757
* `null`. When possible, the default value should be specified.
5858
*/
5959
OMITTED_NAMED_ARG,
@@ -110,6 +110,8 @@ struct RPCArg {
110110
assert(type == Type::ARR || type == Type::OBJ);
111111
}
112112

113+
bool IsOptional() const;
114+
113115
/**
114116
* Return the type string of the argument.
115117
* Set oneline to allow it to be overridden by a custom oneline type string (m_oneline_description).
@@ -185,6 +187,8 @@ class RPCHelpMan
185187
RPCHelpMan(std::string name, std::string description, std::vector<RPCArg> args, RPCResults results, RPCExamples examples);
186188

187189
std::string ToString() const;
190+
/** If the supplied number of args is neither too small nor too high */
191+
bool IsValidNumArgs(size_t num_args) const;
188192

189193
private:
190194
const std::string m_name;

0 commit comments

Comments
 (0)