Skip to content

Commit fa2b8ae

Browse files
author
MacroFake
committed
util: improve bitcoin-wallet exit codes
1 parent 5558d2f commit fa2b8ae

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

contrib/devtools/gen-manpages.py

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

src/bitcoin-wallet.cpp

+12-7
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,16 @@ static void SetupWalletToolArgs(ArgsManager& argsman)
5050
argsman.AddCommand("createfromdump", "Create new wallet file from dumped records");
5151
}
5252

53-
static bool WalletAppInit(ArgsManager& args, int argc, char* argv[])
53+
static std::optional<int> WalletAppInit(ArgsManager& args, int argc, char* argv[])
5454
{
5555
SetupWalletToolArgs(args);
5656
std::string error_message;
5757
if (!args.ParseParameters(argc, argv, error_message)) {
5858
tfm::format(std::cerr, "Error parsing command line arguments: %s\n", error_message);
59-
return false;
59+
return EXIT_FAILURE;
6060
}
61-
if (argc < 2 || HelpRequested(args) || args.IsArgSet("-version")) {
61+
const bool missing_args{argc < 2};
62+
if (missing_args || HelpRequested(args) || args.IsArgSet("-version")) {
6263
std::string strUsage = strprintf("%s bitcoin-wallet version", PACKAGE_NAME) + " " + FormatFullVersion() + "\n";
6364

6465
if (args.IsArgSet("-version")) {
@@ -73,20 +74,24 @@ static bool WalletAppInit(ArgsManager& args, int argc, char* argv[])
7374
strUsage += "\n" + args.GetHelpMessage();
7475
}
7576
tfm::format(std::cout, "%s", strUsage);
76-
return false;
77+
if (missing_args) {
78+
tfm::format(std::cerr, "Error: too few parameters\n");
79+
return EXIT_FAILURE;
80+
}
81+
return EXIT_SUCCESS;
7782
}
7883

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

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

89-
return true;
94+
return std::nullopt;
9095
}
9196

9297
MAIN_FUNCTION
@@ -106,7 +111,7 @@ MAIN_FUNCTION
106111
SetupEnvironment();
107112
RandomInit();
108113
try {
109-
if (!WalletAppInit(args, argc, argv)) return EXIT_FAILURE;
114+
if (const auto maybe_exit{WalletAppInit(args, argc, argv)}) return *maybe_exit;
110115
} catch (const std::exception& e) {
111116
PrintExceptionContinue(&e, "WalletAppInit()");
112117
return EXIT_FAILURE;

0 commit comments

Comments
 (0)