mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-12 06:58:57 +01:00
rpc: Add RPCHelpMan for machine-generated help
This commit is contained in:
@@ -128,3 +128,95 @@ UniValue DescribeAddress(const CTxDestination& dest)
|
||||
{
|
||||
return boost::apply_visitor(DescribeAddressVisitor(), dest);
|
||||
}
|
||||
|
||||
std::string RPCHelpMan::ToString() const
|
||||
{
|
||||
std::string ret;
|
||||
|
||||
ret += m_name;
|
||||
bool is_optional{false};
|
||||
for (const auto& arg : m_args) {
|
||||
ret += " ";
|
||||
if (arg.m_optional) {
|
||||
if (!is_optional) ret += "( ";
|
||||
is_optional = true;
|
||||
} else {
|
||||
// Currently we still support unnamed arguments, so any argument following an optional argument must also be optional
|
||||
// If support for positional arguments is deprecated in the future, remove this line
|
||||
assert(!is_optional);
|
||||
}
|
||||
ret += arg.ToString();
|
||||
}
|
||||
if (is_optional) ret += " )";
|
||||
ret += "\n";
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::string RPCArg::ToStringObj() const
|
||||
{
|
||||
std::string res = "\"" + m_name + "\":";
|
||||
switch (m_type) {
|
||||
case Type::STR:
|
||||
return res + "\"str\"";
|
||||
case Type::STR_HEX:
|
||||
return res + "\"hex\"";
|
||||
case Type::NUM:
|
||||
return res + "n";
|
||||
case Type::AMOUNT:
|
||||
return res + "amount";
|
||||
case Type::BOOL:
|
||||
return res + "bool";
|
||||
case Type::ARR:
|
||||
res += "[";
|
||||
for (const auto& i : m_inner) {
|
||||
res += i.ToString() + ",";
|
||||
}
|
||||
return res + "...]";
|
||||
case Type::OBJ:
|
||||
case Type::OBJ_USER_KEYS:
|
||||
// Currently unused, so avoid writing dead code
|
||||
assert(false);
|
||||
|
||||
// no default case, so the compiler can warn about missing cases
|
||||
}
|
||||
assert(false);
|
||||
}
|
||||
|
||||
std::string RPCArg::ToString() const
|
||||
{
|
||||
switch (m_type) {
|
||||
case Type::STR_HEX:
|
||||
case Type::STR: {
|
||||
return "\"" + m_name + "\"";
|
||||
}
|
||||
case Type::NUM:
|
||||
case Type::AMOUNT:
|
||||
case Type::BOOL: {
|
||||
return m_name;
|
||||
}
|
||||
case Type::OBJ:
|
||||
case Type::OBJ_USER_KEYS: {
|
||||
std::string res;
|
||||
for (size_t i = 0; i < m_inner.size();) {
|
||||
res += m_inner[i].ToStringObj();
|
||||
if (++i < m_inner.size()) res += ",";
|
||||
}
|
||||
if (m_type == Type::OBJ) {
|
||||
return "{" + res + "}";
|
||||
} else {
|
||||
return "{" + res + ",...}";
|
||||
}
|
||||
}
|
||||
case Type::ARR: {
|
||||
std::string res;
|
||||
for (const auto& i : m_inner) {
|
||||
res += i.ToString() + ",";
|
||||
}
|
||||
return "[" + res + "...]";
|
||||
}
|
||||
|
||||
// no default case, so the compiler can warn about missing cases
|
||||
}
|
||||
assert(false);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user