| 121 | } |
| 122 | |
| 123 | std::string CRPCTable::help(const Config &config, const std::string &strCommand, |
| 124 | const JSONRPCRequest &helpreq) const { |
| 125 | std::string strRet; |
| 126 | std::string category; |
| 127 | std::set<intptr_t> setDone; |
| 128 | std::vector<std::pair<std::string, const CRPCCommand *>> vCommands; |
| 129 | vCommands.reserve(mapCommands.size()); |
| 130 | |
| 131 | for (const auto &entry : mapCommands) { |
| 132 | vCommands.push_back( |
| 133 | std::make_pair(entry.second.front()->category + entry.first, |
| 134 | entry.second.front())); |
| 135 | } |
| 136 | sort(vCommands.begin(), vCommands.end()); |
| 137 | |
| 138 | JSONRPCRequest jreq = helpreq; |
| 139 | jreq.mode = JSONRPCRequest::GET_HELP; |
| 140 | jreq.params = UniValue(); |
| 141 | |
| 142 | for (const std::pair<std::string, const CRPCCommand *> &command : |
| 143 | vCommands) { |
| 144 | const CRPCCommand *pcmd = command.second; |
| 145 | std::string strMethod = pcmd->name; |
| 146 | if ((strCommand != "" || pcmd->category == "hidden") && |
| 147 | strMethod != strCommand) { |
| 148 | continue; |
| 149 | } |
| 150 | |
| 151 | jreq.strMethod = strMethod; |
| 152 | try { |
| 153 | UniValue unused_result; |
| 154 | if (setDone.insert(pcmd->unique_id).second) { |
| 155 | pcmd->actor(config, jreq, unused_result, |
| 156 | true /* last_handler */); |
| 157 | } |
| 158 | } catch (const std::exception &e) { |
| 159 | // Help text is returned in an exception |
| 160 | std::string strHelp = std::string(e.what()); |
| 161 | if (strCommand == "") { |
| 162 | if (strHelp.find('\n') != std::string::npos) { |
| 163 | strHelp = strHelp.substr(0, strHelp.find('\n')); |
| 164 | } |
| 165 | |
| 166 | if (category != pcmd->category) { |
| 167 | if (!category.empty()) { |
| 168 | strRet += "\n"; |
| 169 | } |
| 170 | category = pcmd->category; |
| 171 | strRet += "== " + Capitalize(category) + " ==\n"; |
| 172 | } |
| 173 | } |
| 174 | strRet += strHelp + "\n"; |
| 175 | } |
| 176 | } |
| 177 | if (strRet == "") { |
| 178 | strRet = strprintf("help: unknown command: %s\n", strCommand); |
| 179 | } |
| 180 | |