wallet: [refactor] Pass ArgsManager to WalletAppInit

This commit is contained in:
MarcoFalke 2020-12-18 15:14:08 +01:00
parent 7f653c3b22
commit fac05ccdad
No known key found for this signature in database
GPG key ID: CE2B75697E69A548

View file

@ -40,44 +40,45 @@ static void SetupWalletToolArgs(ArgsManager& argsman)
argsman.AddArg("createfromdump", "Create new wallet file from dumped records", ArgsManager::ALLOW_ANY, OptionsCategory::COMMANDS); argsman.AddArg("createfromdump", "Create new wallet file from dumped records", ArgsManager::ALLOW_ANY, OptionsCategory::COMMANDS);
} }
static bool WalletAppInit(int argc, char* argv[]) static bool WalletAppInit(ArgsManager& args, int argc, char* argv[])
{ {
SetupWalletToolArgs(gArgs); SetupWalletToolArgs(args);
std::string error_message; std::string error_message;
if (!gArgs.ParseParameters(argc, argv, error_message)) { if (!args.ParseParameters(argc, argv, error_message)) {
tfm::format(std::cerr, "Error parsing command line arguments: %s\n", error_message); tfm::format(std::cerr, "Error parsing command line arguments: %s\n", error_message);
return false; return false;
} }
if (argc < 2 || HelpRequested(gArgs) || gArgs.IsArgSet("-version")) { if (argc < 2 || HelpRequested(args) || args.IsArgSet("-version")) {
std::string strUsage = strprintf("%s bitcoin-wallet version", PACKAGE_NAME) + " " + FormatFullVersion() + "\n"; std::string strUsage = strprintf("%s bitcoin-wallet version", PACKAGE_NAME) + " " + FormatFullVersion() + "\n";
if (!gArgs.IsArgSet("-version")) { if (!args.IsArgSet("-version")) {
strUsage += "\n" strUsage += "\n"
"bitcoin-wallet is an offline tool for creating and interacting with " PACKAGE_NAME " wallet files.\n" "bitcoin-wallet is an offline tool for creating and interacting with " PACKAGE_NAME " wallet files.\n"
"By default bitcoin-wallet will act on wallets in the default mainnet wallet directory in the datadir.\n" "By default bitcoin-wallet will act on wallets in the default mainnet wallet directory in the datadir.\n"
"To change the target wallet, use the -datadir, -wallet and -testnet/-regtest arguments.\n\n" "To change the target wallet, use the -datadir, -wallet and -testnet/-regtest arguments.\n\n"
"Usage:\n" "Usage:\n"
" bitcoin-wallet [options] <command>\n"; " bitcoin-wallet [options] <command>\n";
strUsage += "\n" + gArgs.GetHelpMessage(); strUsage += "\n" + args.GetHelpMessage();
} }
tfm::format(std::cout, "%s", strUsage); tfm::format(std::cout, "%s", strUsage);
return false; return false;
} }
// check for printtoconsole, allow -debug // check for printtoconsole, allow -debug
LogInstance().m_print_to_console = gArgs.GetBoolArg("-printtoconsole", gArgs.GetBoolArg("-debug", false)); LogInstance().m_print_to_console = args.GetBoolArg("-printtoconsole", args.GetBoolArg("-debug", false));
if (!CheckDataDirOption()) { if (!CheckDataDirOption()) {
tfm::format(std::cerr, "Error: Specified data directory \"%s\" does not exist.\n", gArgs.GetArg("-datadir", "")); tfm::format(std::cerr, "Error: Specified data directory \"%s\" does not exist.\n", args.GetArg("-datadir", ""));
return false; return false;
} }
// Check for chain settings (Params() calls are only valid after this clause) // Check for chain settings (Params() calls are only valid after this clause)
SelectParams(gArgs.GetChainName()); SelectParams(args.GetChainName());
return true; return true;
} }
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
ArgsManager& args = gArgs;
#ifdef WIN32 #ifdef WIN32
util::WinCmdLineArgs winArgs; util::WinCmdLineArgs winArgs;
std::tie(argc, argv) = winArgs.get(); std::tie(argc, argv) = winArgs.get();
@ -85,7 +86,7 @@ int main(int argc, char* argv[])
SetupEnvironment(); SetupEnvironment();
RandomInit(); RandomInit();
try { try {
if (!WalletAppInit(argc, argv)) return EXIT_FAILURE; if (!WalletAppInit(args, argc, argv)) return EXIT_FAILURE;
} catch (const std::exception& e) { } catch (const std::exception& e) {
PrintExceptionContinue(&e, "WalletAppInit()"); PrintExceptionContinue(&e, "WalletAppInit()");
return EXIT_FAILURE; return EXIT_FAILURE;
@ -111,16 +112,16 @@ int main(int argc, char* argv[])
} }
// A name must be provided when creating a file // A name must be provided when creating a file
if (method == "create" && !gArgs.IsArgSet("-wallet")) { if (method == "create" && !args.IsArgSet("-wallet")) {
tfm::format(std::cerr, "Wallet name must be provided when creating a new wallet.\n"); tfm::format(std::cerr, "Wallet name must be provided when creating a new wallet.\n");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
std::string name = gArgs.GetArg("-wallet", ""); std::string name = args.GetArg("-wallet", "");
ECCVerifyHandle globalVerifyHandle; ECCVerifyHandle globalVerifyHandle;
ECC_Start(); ECC_Start();
if (!WalletTool::ExecuteWalletToolFunc(gArgs, method, name)) { if (!WalletTool::ExecuteWalletToolFunc(args, method, name)) {
return EXIT_FAILURE; return EXIT_FAILURE;
} }
ECC_Stop(); ECC_Stop();