Skip to content

Commit

Permalink
util: improve bitcoin-wallet exit codes
Browse files Browse the repository at this point in the history
Adjust the exit codes / functionality of bitcoin-wallet such that it's
not returning a non-zero exit code if there isn't a problem.

This is a followup from bitcoin#24263, and should allow us to add and
additional check=True to our gen-manpages.py script.
  • Loading branch information
fanquake committed Feb 23, 2022
1 parent 3c56530 commit 93c8b7a
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/bitcoin-wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <string>
#include <tuple>

static constexpr int CONTINUE_EXECUTION=-1;
const std::function<std::string(const char*)> G_TRANSLATION_FUN = nullptr;
UrlDecodeFn* const URL_DECODE = nullptr;

Expand All @@ -49,13 +50,13 @@ static void SetupWalletToolArgs(ArgsManager& argsman)
argsman.AddCommand("createfromdump", "Create new wallet file from dumped records");
}

static bool WalletAppInit(ArgsManager& args, int argc, char* argv[])
static int WalletAppInit(ArgsManager& args, int argc, char* argv[])
{
SetupWalletToolArgs(args);
std::string error_message;
if (!args.ParseParameters(argc, argv, error_message)) {
tfm::format(std::cerr, "Error parsing command line arguments: %s\n", error_message);
return false;
return EXIT_FAILURE;
}
if (argc < 2 || HelpRequested(args) || args.IsArgSet("-version")) {
std::string strUsage = strprintf("%s bitcoin-wallet version", PACKAGE_NAME) + " " + FormatFullVersion() + "\n";
Expand All @@ -72,20 +73,24 @@ static bool WalletAppInit(ArgsManager& args, int argc, char* argv[])
strUsage += "\n" + args.GetHelpMessage();
}
tfm::format(std::cout, "%s", strUsage);
return false;
if (argc < 2) {
tfm::format(std::cerr, "Error: too few parameters\n");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}

// check for printtoconsole, allow -debug
LogInstance().m_print_to_console = args.GetBoolArg("-printtoconsole", args.GetBoolArg("-debug", false));

if (!CheckDataDirOption()) {
tfm::format(std::cerr, "Error: Specified data directory \"%s\" does not exist.\n", args.GetArg("-datadir", ""));
return false;
return EXIT_FAILURE;
}
// Check for chain settings (Params() calls are only valid after this clause)
SelectParams(args.GetChainName());

return true;
return CONTINUE_EXECUTION;
}

int main(int argc, char* argv[])
Expand All @@ -105,7 +110,10 @@ int main(int argc, char* argv[])
SetupEnvironment();
RandomInit();
try {
if (!WalletAppInit(args, argc, argv)) return EXIT_FAILURE;
int ret = WalletAppInit(args, argc, argv);
if (ret != CONTINUE_EXECUTION) {
return ret;
}
} catch (const std::exception& e) {
PrintExceptionContinue(&e, "WalletAppInit()");
return EXIT_FAILURE;
Expand Down

0 comments on commit 93c8b7a

Please sign in to comment.