| 161 | */ |
| 162 | |
| 163 | string CRPCTable::help(string strCommand) const |
| 164 | { |
| 165 | string strRet; |
| 166 | string category; |
| 167 | set<rpcfn_type> setDone; |
| 168 | vector<pair<string, const CRPCCommand*> > vCommands; |
| 169 | |
| 170 | for (map<string, const CRPCCommand*>::const_iterator mi = mapCommands.begin(); mi != mapCommands.end(); ++mi) |
| 171 | vCommands.push_back(make_pair(mi->second->category + mi->first, mi->second)); |
| 172 | sort(vCommands.begin(), vCommands.end()); |
| 173 | |
| 174 | for (const PAIRTYPE(string, const CRPCCommand*) & command : vCommands) { |
| 175 | const CRPCCommand* pcmd = command.second; |
| 176 | string strMethod = pcmd->name; |
| 177 | // We already filter duplicates, but these deprecated screw up the sort order |
| 178 | if (strMethod.find("label") != string::npos) |
| 179 | continue; |
| 180 | if ((strCommand != "" || pcmd->category == "hidden") && strMethod != strCommand) |
| 181 | continue; |
| 182 | try |
| 183 | { |
| 184 | UniValue params(UniValue::VARR); |
| 185 | rpcfn_type pfn = pcmd->actor; |
| 186 | if (setDone.insert(pfn).second) |
| 187 | (*pfn)(params, true); |
| 188 | } |
| 189 | catch (const std::exception& e) |
| 190 | { |
| 191 | // Help text is returned in an exception |
| 192 | string strHelp = string(e.what()); |
| 193 | if (strCommand == "") { |
| 194 | if (strHelp.find('\n') != string::npos) |
| 195 | strHelp = strHelp.substr(0, strHelp.find('\n')); |
| 196 | |
| 197 | if (category != pcmd->category) { |
| 198 | if (!category.empty()) |
| 199 | strRet += "\n"; |
| 200 | category = pcmd->category; |
| 201 | string firstLetter = category.substr(0, 1); |
| 202 | boost::to_upper(firstLetter); |
| 203 | strRet += "== " + firstLetter + category.substr(1) + " ==\n"; |
| 204 | } |
| 205 | } |
| 206 | strRet += strHelp + "\n"; |
| 207 | } |
| 208 | } |
| 209 | if (strRet == "") |
| 210 | strRet = strprintf("help: unknown command: %s\n", strCommand); |
| 211 | strRet = strRet.substr(0, strRet.size() - 1); |
| 212 | return strRet; |
| 213 | } |
| 214 | |
| 215 | UniValue help(const UniValue& params, bool fHelp) |
| 216 | { |
| 217 | if (fHelp || params.size() > 1) |
| 218 | throw runtime_error( |
| 219 | "help ( \"command\" )\n" |
| 220 | "\nList all commands, or get help for a specified command.\n" |
no test coverage detected