Changeset View
Changeset View
Standalone View
Standalone View
src/bitcoin-cli.cpp
Show First 20 Lines • Show All 148 Lines • ▼ Show 20 Lines | |||||
static int AppInitRPC(int argc, char *argv[]) { | static int AppInitRPC(int argc, char *argv[]) { | ||||
// | // | ||||
// Parameters | // Parameters | ||||
// | // | ||||
SetupCliArgs(); | SetupCliArgs(); | ||||
std::string error; | std::string error; | ||||
if (!gArgs.ParseParameters(argc, argv, error)) { | if (!gArgs.ParseParameters(argc, argv, error)) { | ||||
tfm::format(std::cerr, "Error parsing command line arguments: %s\n", | tfm::format(std::cerr, "Error parsing command line arguments: %s\n", | ||||
error.c_str()); | error); | ||||
return EXIT_FAILURE; | return EXIT_FAILURE; | ||||
} | } | ||||
if (argc < 2 || HelpRequested(gArgs) || gArgs.IsArgSet("-version")) { | if (argc < 2 || HelpRequested(gArgs) || gArgs.IsArgSet("-version")) { | ||||
std::string strUsage = | std::string strUsage = | ||||
PACKAGE_NAME " RPC client version " + FormatFullVersion() + "\n"; | PACKAGE_NAME " RPC client version " + FormatFullVersion() + "\n"; | ||||
if (!gArgs.IsArgSet("-version")) { | if (!gArgs.IsArgSet("-version")) { | ||||
strUsage += "\n" | strUsage += "\n" | ||||
"Usage: bitcoin-cli [options] <command> [params] " | "Usage: bitcoin-cli [options] <command> [params] " | ||||
"Send command to " PACKAGE_NAME "\n" | "Send command to " PACKAGE_NAME "\n" | ||||
"or: bitcoin-cli [options] -named <command> " | "or: bitcoin-cli [options] -named <command> " | ||||
"[name=value]... Send command to " PACKAGE_NAME | "[name=value]... Send command to " PACKAGE_NAME | ||||
" (with named arguments)\n" | " (with named arguments)\n" | ||||
"or: bitcoin-cli [options] help " | "or: bitcoin-cli [options] help " | ||||
"List commands\n" | "List commands\n" | ||||
"or: bitcoin-cli [options] help <command> Get " | "or: bitcoin-cli [options] help <command> Get " | ||||
"help for a command\n"; | "help for a command\n"; | ||||
strUsage += "\n" + gArgs.GetHelpMessage(); | strUsage += "\n" + gArgs.GetHelpMessage(); | ||||
} | } | ||||
tfm::format(std::cout, "%s", strUsage.c_str()); | tfm::format(std::cout, "%s", strUsage); | ||||
if (argc < 2) { | if (argc < 2) { | ||||
tfm::format(std::cerr, "Error: too few parameters\n"); | tfm::format(std::cerr, "Error: too few parameters\n"); | ||||
return EXIT_FAILURE; | return EXIT_FAILURE; | ||||
} | } | ||||
return EXIT_SUCCESS; | return EXIT_SUCCESS; | ||||
} | } | ||||
if (!CheckDataDirOption()) { | if (!CheckDataDirOption()) { | ||||
tfm::format(std::cerr, | tfm::format(std::cerr, | ||||
"Error: Specified data directory \"%s\" does not exist.\n", | "Error: Specified data directory \"%s\" does not exist.\n", | ||||
gArgs.GetArg("-datadir", "").c_str()); | gArgs.GetArg("-datadir", "")); | ||||
return EXIT_FAILURE; | return EXIT_FAILURE; | ||||
} | } | ||||
if (!gArgs.ReadConfigFiles(error, true)) { | if (!gArgs.ReadConfigFiles(error, true)) { | ||||
tfm::format(std::cerr, "Error reading configuration file: %s\n", | tfm::format(std::cerr, "Error reading configuration file: %s\n", error); | ||||
error.c_str()); | |||||
return EXIT_FAILURE; | return EXIT_FAILURE; | ||||
} | } | ||||
// Check for -chain, -testnet or -regtest parameter (BaseParams() calls are | // Check for -chain, -testnet or -regtest parameter (BaseParams() calls are | ||||
// only valid after this clause) | // only valid after this clause) | ||||
try { | try { | ||||
SelectBaseParams(gArgs.GetChainName()); | SelectBaseParams(gArgs.GetChainName()); | ||||
} catch (const std::exception &e) { | } catch (const std::exception &e) { | ||||
tfm::format(std::cerr, "Error: %s\n", e.what()); | tfm::format(std::cerr, "Error: %s\n", e.what()); | ||||
▲ Show 20 Lines • Show All 271 Lines • ▼ Show 20 Lines | if (response.status == 0) { | ||||
host, port, responseErrorMessage)); | host, port, responseErrorMessage)); | ||||
} else if (response.status == HTTP_UNAUTHORIZED) { | } else if (response.status == HTTP_UNAUTHORIZED) { | ||||
if (failedToGetAuthCookie) { | if (failedToGetAuthCookie) { | ||||
throw std::runtime_error(strprintf( | throw std::runtime_error(strprintf( | ||||
"Could not locate RPC credentials. No authentication cookie " | "Could not locate RPC credentials. No authentication cookie " | ||||
"could be found, and RPC password is not set. See " | "could be found, and RPC password is not set. See " | ||||
"-rpcpassword and -stdinrpcpass. Configuration file: (%s)", | "-rpcpassword and -stdinrpcpass. Configuration file: (%s)", | ||||
GetConfigFile(gArgs.GetArg("-conf", BITCOIN_CONF_FILENAME)) | GetConfigFile(gArgs.GetArg("-conf", BITCOIN_CONF_FILENAME)) | ||||
.string() | .string())); | ||||
.c_str())); | |||||
} else { | } else { | ||||
throw std::runtime_error( | throw std::runtime_error( | ||||
"Authorization failed: Incorrect rpcuser or rpcpassword"); | "Authorization failed: Incorrect rpcuser or rpcpassword"); | ||||
} | } | ||||
} else if (response.status >= 400 && response.status != HTTP_BAD_REQUEST && | } else if (response.status >= 400 && response.status != HTTP_BAD_REQUEST && | ||||
response.status != HTTP_NOT_FOUND && | response.status != HTTP_NOT_FOUND && | ||||
response.status != HTTP_INTERNAL_SERVER_ERROR) { | response.status != HTTP_INTERNAL_SERVER_ERROR) { | ||||
throw std::runtime_error( | throw std::runtime_error( | ||||
▲ Show 20 Lines • Show All 119 Lines • ▼ Show 20 Lines | try { | ||||
strPrint = std::string("error: ") + e.what(); | strPrint = std::string("error: ") + e.what(); | ||||
nRet = EXIT_FAILURE; | nRet = EXIT_FAILURE; | ||||
} catch (...) { | } catch (...) { | ||||
PrintExceptionContinue(nullptr, "CommandLineRPC()"); | PrintExceptionContinue(nullptr, "CommandLineRPC()"); | ||||
throw; | throw; | ||||
} | } | ||||
if (strPrint != "") { | if (strPrint != "") { | ||||
tfm::format(nRet == 0 ? std::cout : std::cerr, "%s\n", | tfm::format(nRet == 0 ? std::cout : std::cerr, "%s\n", strPrint); | ||||
strPrint.c_str()); | |||||
} | } | ||||
return nRet; | return nRet; | ||||
} | } | ||||
int main(int argc, char *argv[]) { | int main(int argc, char *argv[]) { | ||||
#ifdef WIN32 | #ifdef WIN32 | ||||
util::WinCmdLineArgs winArgs; | util::WinCmdLineArgs winArgs; | ||||
std::tie(argc, argv) = winArgs.get(); | std::tie(argc, argv) = winArgs.get(); | ||||
Show All 31 Lines |