Changeset View
Changeset View
Standalone View
Standalone View
src/bitcoin-cli.cpp
Show First 20 Lines • Show All 147 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)) { | ||||
fprintf(stderr, "Error parsing command line arguments: %s\n", | tfm::format(std::cerr, "Error parsing command line arguments: %s\n", | ||||
error.c_str()); | error.c_str()); | ||||
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(); | ||||
} | } | ||||
fprintf(stdout, "%s", strUsage.c_str()); | tfm::format(std::cout, "%s", strUsage.c_str()); | ||||
if (argc < 2) { | if (argc < 2) { | ||||
fprintf(stderr, "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 (!fs::is_directory(GetDataDir(false))) { | if (!fs::is_directory(GetDataDir(false))) { | ||||
fprintf(stderr, | 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", "").c_str()); | ||||
return EXIT_FAILURE; | return EXIT_FAILURE; | ||||
} | } | ||||
if (!gArgs.ReadConfigFiles(error, true)) { | if (!gArgs.ReadConfigFiles(error, true)) { | ||||
fprintf(stderr, "Error reading configuration file: %s\n", | tfm::format(std::cerr, "Error reading configuration file: %s\n", | ||||
error.c_str()); | error.c_str()); | ||||
return EXIT_FAILURE; | return EXIT_FAILURE; | ||||
} | } | ||||
// Check for -testnet or -regtest parameter (BaseParams() calls are only | // Check for -testnet or -regtest parameter (BaseParams() calls are only | ||||
// valid after this clause) | // valid after this clause) | ||||
try { | try { | ||||
SelectBaseParams(gArgs.GetChainName()); | SelectBaseParams(gArgs.GetChainName()); | ||||
} catch (const std::exception &e) { | } catch (const std::exception &e) { | ||||
fprintf(stderr, "Error: %s\n", e.what()); | tfm::format(std::cerr, "Error: %s\n", e.what()); | ||||
return EXIT_FAILURE; | return EXIT_FAILURE; | ||||
} | } | ||||
return CONTINUE_EXECUTION; | return CONTINUE_EXECUTION; | ||||
} | } | ||||
/** Reply structure for request_done to fill in */ | /** Reply structure for request_done to fill in */ | ||||
struct HTTPReply { | struct HTTPReply { | ||||
HTTPReply() : status(0), error(-1) {} | HTTPReply() : status(0), error(-1) {} | ||||
▲ Show 20 Lines • Show All 400 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 != "") { | ||||
fprintf((nRet == 0 ? stdout : stderr), "%s\n", strPrint.c_str()); | tfm::format(nRet == 0 ? std::cout : std::cerr, "%s\n", | ||||
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(); | ||||
#endif | #endif | ||||
SetupEnvironment(); | SetupEnvironment(); | ||||
if (!SetupNetworking()) { | if (!SetupNetworking()) { | ||||
fprintf(stderr, "Error: Initializing networking failed\n"); | tfm::format(std::cerr, "Error: Initializing networking failed\n"); | ||||
return EXIT_FAILURE; | return EXIT_FAILURE; | ||||
} | } | ||||
event_set_log_callback(&libevent_log_cb); | event_set_log_callback(&libevent_log_cb); | ||||
try { | try { | ||||
int ret = AppInitRPC(argc, argv); | int ret = AppInitRPC(argc, argv); | ||||
if (ret != CONTINUE_EXECUTION) { | if (ret != CONTINUE_EXECUTION) { | ||||
return ret; | return ret; | ||||
Show All 19 Lines |