aboutsummaryrefslogtreecommitdiff
path: root/src/rpc/util.cpp
diff options
context:
space:
mode:
authorWladimir J. van der Laan <laanwj@protonmail.com>2021-02-26 15:03:50 +0100
committerIvan Metlushko <metlushko@gmail.com>2021-03-03 09:40:06 +0100
commit591735ef0bf13b94643b794518406f981fa5dcb7 (patch)
tree1e846cc4c1af06829d68ad35b2c73f406d33a1e3 /src/rpc/util.cpp
parent5d5a90e819d23a302f9bec6b995a3116ead6ae94 (diff)
downloadbitcoin-591735ef0bf13b94643b794518406f981fa5dcb7.tar.xz
rpc: Add HelpExampleCliNamed and use it for `createwallet` doc
Diffstat (limited to 'src/rpc/util.cpp')
-rw-r--r--src/rpc/util.cpp52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/rpc/util.cpp b/src/rpc/util.cpp
index b4533c14d0..d844074601 100644
--- a/src/rpc/util.cpp
+++ b/src/rpc/util.cpp
@@ -113,11 +113,63 @@ std::vector<unsigned char> ParseHexO(const UniValue& o, std::string strKey)
return ParseHexV(find_value(o, strKey), strKey);
}
+namespace {
+
+/**
+ * Quote an argument for shell.
+ *
+ * @note This is intended for help, not for security-sensitive purposes.
+ */
+std::string ShellQuote(const std::string& s)
+{
+ std::string result;
+ result.reserve(s.size() * 2);
+ for (const char ch: s) {
+ if (ch == '\'') {
+ result += "'\''";
+ } else {
+ result += ch;
+ }
+ }
+ return "'" + result + "'";
+}
+
+/**
+ * Shell-quotes the argument if it needs quoting, else returns it literally, to save typing.
+ *
+ * @note This is intended for help, not for security-sensitive purposes.
+ */
+std::string ShellQuoteIfNeeded(const std::string& s)
+{
+ for (const char ch: s) {
+ if (ch == ' ' || ch == '\'' || ch == '"') {
+ return ShellQuote(s);
+ }
+ }
+
+ return s;
+}
+
+}
+
std::string HelpExampleCli(const std::string& methodname, const std::string& args)
{
return "> bitcoin-cli " + methodname + " " + args + "\n";
}
+std::string HelpExampleCliNamed(const std::string& methodname, const RPCArgList& args)
+{
+ std::string result = "> bitcoin-cli -named " + methodname;
+ for (const auto& argpair: args) {
+ const auto& value = argpair.second.isStr()
+ ? argpair.second.get_str()
+ : argpair.second.write();
+ result += " " + argpair.first + "=" + ShellQuoteIfNeeded(value);
+ }
+ result += "\n";
+ return result;
+}
+
std::string HelpExampleRpc(const std::string& methodname, const std::string& args)
{
return "> curl --user myusername --data-binary '{\"jsonrpc\": \"1.0\", \"id\": \"curltest\", "