Skip to content

Fixing the incorrect error code even when CLI exits correctly #1151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
22 changes: 21 additions & 1 deletion src/Cli/src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public static int Main(string[] args)
ConfigGenerator.SetLoggerForCliConfigGenerator(configGeneratorLogger);
Utils.SetCliUtilsLogger(cliUtilsLogger);

// To know if `--help` or `--version` was requested.
bool isHelpOrVersionRequested = false;

// Parsing user arguments and executing required methods.
ParserResult<object>? result = parser.ParseArguments<InitOptions, AddOptions, UpdateOptions, StartOptions>(args)
.WithParsed<InitOptions>(options =>
Expand Down Expand Up @@ -100,9 +103,26 @@ public static int Main(string[] args)
{
cliLogger.LogError("Failed to start the engine.");
}
})
.WithNotParsed(err =>
{
/// System.CommandLine considers --help and --version as NonParsed Errors
/// ref: https://github.com/commandlineparser/commandline/issues/630
/// This is a workaround to make sure our app exits with exit code 0,
/// when user does --help or --versions.
/// dab --help -> ErrorType.HelpVerbRequestedError
/// dab [command-name] --help -> ErrorType.HelpRequestedError
/// dab --version -> ErrorType.VersionRequestedError
List<Error> errors = err.ToList();
if (errors.Any(e => e.Tag == ErrorType.VersionRequestedError
|| e.Tag == ErrorType.HelpRequestedError
|| e.Tag == ErrorType.HelpVerbRequestedError))
{
isHelpOrVersionRequested = true;
}
});

return result is Parsed<object> ? 0 : -1;
return ((result is Parsed<object>) || (isHelpOrVersionRequested)) ? 0 : -1;
}

/// <summary>
Expand Down
17 changes: 17 additions & 0 deletions src/Cli/test/EndToEndTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,23 @@ public void TestEngineStartUpWithVerboseAndLogLevelOptions(string logLevelOption
Assert.IsTrue(output.Contains($"User provided config file: {_testRuntimeConfig}"));
}

/// <summary>
/// Test to verify that `--help` and `--version` along with know command/option produce the exit code 0,
/// while unknown commands/options have exit code -1.
/// </summary>
[DataTestMethod]
[DataRow(new string[] { "--version" }, 0, DisplayName = "Checking version should have exit code 0.")]
[DataRow(new string[] { "--help" }, 0, DisplayName = "Checking commands with help should have exit code 0.")]
[DataRow(new string[] { "add", "--help" }, 0, DisplayName = "Checking options with help should have exit code 0.")]
[DataRow(new string[] { "initialize" }, -1, DisplayName = "Invalid Command should have exit code -1.")]
[DataRow(new string[] { "init", "--database-name", "mssql" }, -1, DisplayName = "Invalid Options should have exit code -1.")]
[DataRow(new string[] { "init", "--database-type", "mssql", "-c", "dab-config-test.json" }, 0,
DisplayName = "Correct command with correct options should have exit code 0.")]
public void VerifyExitCodeForCli(string[] cliArguments, int expectedErrorCode)
{
Assert.AreEqual(Cli.Program.Main(cliArguments), expectedErrorCode);
}

/// <summary>
/// Test to verify that help writer window generates output on the console.
/// </summary>
Expand Down