Skip to content

Commit a375e66

Browse files
Fixing the incorrect error code even when CLI exits correctly (#1151)
## Why make this change? - Closes #1150 - ErrorCode -1 is being returned when a command other than specified in dab is used, like --version, --help ## What is this change? - Options `--help` generates `ErrorType.HelpRequestedError` and `ErrorType.HelpVerbRequestedError` while `--version` generates `ErrorType.VersionRequestedError`. - While parsing we specifically looked if these error types are produced and marked them with exit code 0. ## How was this tested? - [x] manually - [x] unit tests ## Sample Request(s) ![image](https://user-images.githubusercontent.com/102276754/216330579-efe57e11-29e1-4302-8098-a68f35b7a499.png)
1 parent 1d1e812 commit a375e66

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/Cli/src/Program.cs

+21-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ public static int Main(string[] args)
3535
ConfigGenerator.SetLoggerForCliConfigGenerator(configGeneratorLogger);
3636
Utils.SetCliUtilsLogger(cliUtilsLogger);
3737

38+
// To know if `--help` or `--version` was requested.
39+
bool isHelpOrVersionRequested = false;
40+
3841
// Parsing user arguments and executing required methods.
3942
ParserResult<object>? result = parser.ParseArguments<InitOptions, AddOptions, UpdateOptions, StartOptions>(args)
4043
.WithParsed<InitOptions>(options =>
@@ -100,9 +103,26 @@ public static int Main(string[] args)
100103
{
101104
cliLogger.LogError("Failed to start the engine.");
102105
}
106+
})
107+
.WithNotParsed(err =>
108+
{
109+
/// System.CommandLine considers --help and --version as NonParsed Errors
110+
/// ref: https://github.com/commandlineparser/commandline/issues/630
111+
/// This is a workaround to make sure our app exits with exit code 0,
112+
/// when user does --help or --versions.
113+
/// dab --help -> ErrorType.HelpVerbRequestedError
114+
/// dab [command-name] --help -> ErrorType.HelpRequestedError
115+
/// dab --version -> ErrorType.VersionRequestedError
116+
List<Error> errors = err.ToList();
117+
if (errors.Any(e => e.Tag == ErrorType.VersionRequestedError
118+
|| e.Tag == ErrorType.HelpRequestedError
119+
|| e.Tag == ErrorType.HelpVerbRequestedError))
120+
{
121+
isHelpOrVersionRequested = true;
122+
}
103123
});
104124

105-
return result is Parsed<object> ? 0 : -1;
125+
return ((result is Parsed<object>) || (isHelpOrVersionRequested)) ? 0 : -1;
106126
}
107127

108128
/// <summary>

src/Cli/test/EndToEndTests.cs

+17
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,23 @@ public void TestEngineStartUpWithVerboseAndLogLevelOptions(string logLevelOption
472472
Assert.IsTrue(output.Contains($"User provided config file: {_testRuntimeConfig}"));
473473
}
474474

475+
/// <summary>
476+
/// Test to verify that `--help` and `--version` along with know command/option produce the exit code 0,
477+
/// while unknown commands/options have exit code -1.
478+
/// </summary>
479+
[DataTestMethod]
480+
[DataRow(new string[] { "--version" }, 0, DisplayName = "Checking version should have exit code 0.")]
481+
[DataRow(new string[] { "--help" }, 0, DisplayName = "Checking commands with help should have exit code 0.")]
482+
[DataRow(new string[] { "add", "--help" }, 0, DisplayName = "Checking options with help should have exit code 0.")]
483+
[DataRow(new string[] { "initialize" }, -1, DisplayName = "Invalid Command should have exit code -1.")]
484+
[DataRow(new string[] { "init", "--database-name", "mssql" }, -1, DisplayName = "Invalid Options should have exit code -1.")]
485+
[DataRow(new string[] { "init", "--database-type", "mssql", "-c", "dab-config-test.json" }, 0,
486+
DisplayName = "Correct command with correct options should have exit code 0.")]
487+
public void VerifyExitCodeForCli(string[] cliArguments, int expectedErrorCode)
488+
{
489+
Assert.AreEqual(Cli.Program.Main(cliArguments), expectedErrorCode);
490+
}
491+
475492
/// <summary>
476493
/// Test to verify that help writer window generates output on the console.
477494
/// </summary>

0 commit comments

Comments
 (0)