Fix exit codes:

- `--help`, `--version` etc should exit with `0` i.e. no error ("not enough args" case should still trigger an error)
- error reading config file should exit with `1`

Slightly refactor AppInitRPC/AppInitRawTx to return standard exit codes (EXIT_FAILURE/EXIT_SUCCESS) or CONTINUE_EXECUTION (-1)
This commit is contained in:
UdjinM6
2016-11-02 21:59:09 +03:00
committed by Jack Grigg
parent f11fb32967
commit fadbbe1e04
3 changed files with 35 additions and 15 deletions

View File

@@ -22,6 +22,7 @@
using namespace std; using namespace std;
static const int DEFAULT_HTTP_CLIENT_TIMEOUT=900; static const int DEFAULT_HTTP_CLIENT_TIMEOUT=900;
static const int CONTINUE_EXECUTION=-1;
std::string HelpMessageCli() std::string HelpMessageCli()
{ {
@@ -63,7 +64,11 @@ public:
}; };
static bool AppInitRPC(int argc, char* argv[]) //
// This function returns either one of EXIT_ codes when it's expected to stop the process or
// CONTINUE_EXECUTION when it's expected to continue further.
//
static int AppInitRPC(int argc, char* argv[])
{ {
// //
// Parameters // Parameters
@@ -83,29 +88,33 @@ static bool AppInitRPC(int argc, char* argv[])
} }
fprintf(stdout, "%s", strUsage.c_str()); fprintf(stdout, "%s", strUsage.c_str());
return false; if (argc < 2) {
fprintf(stderr, "Error: too few parameters\n");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
} }
if (!boost::filesystem::is_directory(GetDataDir(false))) { if (!boost::filesystem::is_directory(GetDataDir(false))) {
fprintf(stderr, "Error: Specified data directory \"%s\" does not exist.\n", mapArgs["-datadir"].c_str()); fprintf(stderr, "Error: Specified data directory \"%s\" does not exist.\n", mapArgs["-datadir"].c_str());
return false; return EXIT_FAILURE;
} }
try { try {
ReadConfigFile(mapArgs, mapMultiArgs); ReadConfigFile(mapArgs, mapMultiArgs);
} catch (const std::exception& e) { } catch (const std::exception& e) {
fprintf(stderr,"Error reading configuration file: %s\n", e.what()); fprintf(stderr,"Error reading configuration file: %s\n", e.what());
return false; return EXIT_FAILURE;
} }
// Check for -testnet or -regtest parameter (BaseParams() calls are only valid after this clause) // Check for -testnet or -regtest parameter (BaseParams() calls are only valid after this clause)
if (!SelectBaseParamsFromCommandLine()) { if (!SelectBaseParamsFromCommandLine()) {
fprintf(stderr, "Error: Invalid combination of -regtest and -testnet.\n"); fprintf(stderr, "Error: Invalid combination of -regtest and -testnet.\n");
return false; return EXIT_FAILURE;
} }
if (GetBoolArg("-rpcssl", false)) if (GetBoolArg("-rpcssl", false))
{ {
fprintf(stderr, "Error: SSL mode for RPC (-rpcssl) is no longer supported.\n"); fprintf(stderr, "Error: SSL mode for RPC (-rpcssl) is no longer supported.\n");
return false; return EXIT_FAILURE;
} }
return true; return CONTINUE_EXECUTION;
} }
@@ -344,8 +353,9 @@ int main(int argc, char* argv[])
} }
try { try {
if(!AppInitRPC(argc, argv)) int ret = AppInitRPC(argc, argv);
return EXIT_FAILURE; if (ret != CONTINUE_EXECUTION)
return ret;
} }
catch (const std::exception& e) { catch (const std::exception& e) {
PrintExceptionContinue(&e, "AppInitRPC()"); PrintExceptionContinue(&e, "AppInitRPC()");

View File

@@ -26,8 +26,13 @@ using namespace std;
static bool fCreateBlank; static bool fCreateBlank;
static map<string,UniValue> registers; static map<string,UniValue> registers;
static const int CONTINUE_EXECUTION=-1;
static bool AppInitRawTx(int argc, char* argv[]) //
// This function returns either one of EXIT_ codes when it's expected to stop the process or
// CONTINUE_EXECUTION when it's expected to continue further.
//
static int AppInitRawTx(int argc, char* argv[])
{ {
// //
// Parameters // Parameters
@@ -83,9 +88,13 @@ static bool AppInitRawTx(int argc, char* argv[])
strUsage += HelpMessageOpt("set=NAME:JSON-STRING", _("Set register NAME to given JSON-STRING")); strUsage += HelpMessageOpt("set=NAME:JSON-STRING", _("Set register NAME to given JSON-STRING"));
fprintf(stdout, "%s", strUsage.c_str()); fprintf(stdout, "%s", strUsage.c_str());
return false; if (argc < 2) {
fprintf(stderr, "Error: too few parameters\n");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
} }
return true; return CONTINUE_EXECUTION;
} }
static void RegisterSetJson(const string& key, const string& rawJson) static void RegisterSetJson(const string& key, const string& rawJson)
@@ -674,8 +683,9 @@ int main(int argc, char* argv[])
SetupEnvironment(); SetupEnvironment();
try { try {
if(!AppInitRawTx(argc, argv)) int ret = AppInitRawTx(argc, argv);
return EXIT_FAILURE; if (ret != CONTINUE_EXECUTION)
return ret;
} }
catch (const std::exception& e) { catch (const std::exception& e) {
PrintExceptionContinue(&e, "AppInitRawTx()"); PrintExceptionContinue(&e, "AppInitRawTx()");

View File

@@ -88,7 +88,7 @@ bool AppInit(int argc, char* argv[])
} }
fprintf(stdout, "%s", strUsage.c_str()); fprintf(stdout, "%s", strUsage.c_str());
return false; return true;
} }
try try