util: improve bitcoin-wallet exit codes

This commit is contained in:
MacroFake 2022-09-12 13:11:33 +02:00
parent 5558d2f549
commit fa2b8ae0a2
No known key found for this signature in database
GPG Key ID: CE2B75697E69A548
2 changed files with 13 additions and 8 deletions

View File

@ -36,7 +36,7 @@ versions = []
for relpath in BINARIES: for relpath in BINARIES:
abspath = os.path.join(builddir, relpath) abspath = os.path.join(builddir, relpath)
try: try:
r = subprocess.run([abspath, '--version'], stdout=subprocess.PIPE, universal_newlines=True) r = subprocess.run([abspath, "--version"], stdout=subprocess.PIPE, check=True, universal_newlines=True)
except IOError: except IOError:
print(f'{abspath} not found or not an executable', file=sys.stderr) print(f'{abspath} not found or not an executable', file=sys.stderr)
sys.exit(1) sys.exit(1)

View File

@ -50,15 +50,16 @@ static void SetupWalletToolArgs(ArgsManager& argsman)
argsman.AddCommand("createfromdump", "Create new wallet file from dumped records"); argsman.AddCommand("createfromdump", "Create new wallet file from dumped records");
} }
static bool WalletAppInit(ArgsManager& args, int argc, char* argv[]) static std::optional<int> WalletAppInit(ArgsManager& args, int argc, char* argv[])
{ {
SetupWalletToolArgs(args); SetupWalletToolArgs(args);
std::string error_message; std::string error_message;
if (!args.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 EXIT_FAILURE;
} }
if (argc < 2 || HelpRequested(args) || args.IsArgSet("-version")) { const bool missing_args{argc < 2};
if (missing_args || 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 (args.IsArgSet("-version")) { if (args.IsArgSet("-version")) {
@ -73,7 +74,11 @@ static bool WalletAppInit(ArgsManager& args, int argc, char* argv[])
strUsage += "\n" + args.GetHelpMessage(); strUsage += "\n" + args.GetHelpMessage();
} }
tfm::format(std::cout, "%s", strUsage); tfm::format(std::cout, "%s", strUsage);
return false; if (missing_args) {
tfm::format(std::cerr, "Error: too few parameters\n");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
} }
// check for printtoconsole, allow -debug // check for printtoconsole, allow -debug
@ -81,12 +86,12 @@ static bool WalletAppInit(ArgsManager& args, int argc, char* argv[])
if (!CheckDataDirOption()) { if (!CheckDataDirOption()) {
tfm::format(std::cerr, "Error: Specified data directory \"%s\" does not exist.\n", args.GetArg("-datadir", "")); 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) // Check for chain settings (Params() calls are only valid after this clause)
SelectParams(args.GetChainName()); SelectParams(args.GetChainName());
return true; return std::nullopt;
} }
MAIN_FUNCTION MAIN_FUNCTION
@ -106,7 +111,7 @@ MAIN_FUNCTION
SetupEnvironment(); SetupEnvironment();
RandomInit(); RandomInit();
try { try {
if (!WalletAppInit(args, argc, argv)) return EXIT_FAILURE; if (const auto maybe_exit{WalletAppInit(args, argc, argv)}) return *maybe_exit;
} catch (const std::exception& e) { } catch (const std::exception& e) {
PrintExceptionContinue(&e, "WalletAppInit()"); PrintExceptionContinue(&e, "WalletAppInit()");
return EXIT_FAILURE; return EXIT_FAILURE;