| 768 | } |
| 769 | |
| 770 | std::string RPCMethod::ToString() const |
| 771 | { |
| 772 | std::string ret; |
| 773 | |
| 774 | // Oneline summary |
| 775 | ret += m_name; |
| 776 | bool was_optional{false}; |
| 777 | for (const auto& arg : m_args) { |
| 778 | if (arg.m_opts.hidden) break; // Any arg that follows is also hidden |
| 779 | const bool optional = arg.IsOptional(); |
| 780 | ret += " "; |
| 781 | if (optional) { |
| 782 | if (!was_optional) ret += "( "; |
| 783 | was_optional = true; |
| 784 | } else { |
| 785 | if (was_optional) ret += ") "; |
| 786 | was_optional = false; |
| 787 | } |
| 788 | ret += arg.ToString(/*oneline=*/true); |
| 789 | } |
| 790 | if (was_optional) ret += " )"; |
| 791 | |
| 792 | // Description |
| 793 | CHECK_NONFATAL(!m_description.starts_with('\n')); // Historically \n was required, but reject it for new code. |
| 794 | ret += "\n\n" + TrimString(m_description) + "\n"; |
| 795 | |
| 796 | // Arguments |
| 797 | Sections sections; |
| 798 | Sections named_only_sections; |
| 799 | for (size_t i{0}; i < m_args.size(); ++i) { |
| 800 | const auto& arg = m_args.at(i); |
| 801 | if (arg.m_opts.hidden) break; // Any arg that follows is also hidden |
| 802 | |
| 803 | // Push named argument name and description |
| 804 | sections.m_sections.emplace_back(util::ToString(i + 1) + ". " + arg.GetFirstName(), arg.ToDescriptionString(/*is_named_arg=*/true)); |
| 805 | sections.m_max_pad = std::max(sections.m_max_pad, sections.m_sections.back().m_left.size()); |
| 806 | |
| 807 | // Recursively push nested args |
| 808 | sections.Push(arg); |
| 809 | |
| 810 | // Push named-only argument sections |
| 811 | if (arg.m_type == RPCArg::Type::OBJ_NAMED_PARAMS) { |
| 812 | for (const auto& arg_inner : arg.m_inner) { |
| 813 | named_only_sections.PushSection({arg_inner.GetFirstName(), arg_inner.ToDescriptionString(/*is_named_arg=*/true)}); |
| 814 | named_only_sections.Push(arg_inner); |
| 815 | } |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | if (!sections.m_sections.empty()) ret += "\nArguments:\n"; |
| 820 | ret += sections.ToString(); |
| 821 | if (!named_only_sections.m_sections.empty()) ret += "\nNamed Arguments:\n"; |
| 822 | ret += named_only_sections.ToString(); |
| 823 | |
| 824 | // Result |
| 825 | ret += m_results.ToDescriptionString(); |
| 826 | |
| 827 | // Examples |