-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update DAB CLI to return appropriate exit codes based upon exit reason (
#2084) ## Why make this change? - Closes #1777 - Certain failures that occurred within CLI execution resulted in the CLI returning exit code 0 for success, even though there were failures. - For example, `dab start {options}` may contain fully valid values, but when used as arguments to start the DAB engine process within CLI, engine startup fails. The failure didn't get reflected as error code -1. - For example, `dab init {options}` may reference a file that already exists in the path that the CLI uses to read/write config files, this results in an error that is not properly reflected in the exit code. ## What is this change? - Any failures that occur in CLI usage will result in the proper exit codes: - `0` success - `-1` failure - The change updates the usage of CommandLineParser. Instead of swallowing exceptions and the status of `isSuccess` within the `options.Handler()` methods, the handler methods all return the int error code now. - e.g. the following now captures the return codes after command line parsing. ```csharp parser.ParseArguments<InitOptions, AddOptions, UpdateOptions, StartOptions, ValidateOptions, ExportOptions, AddTelemetryOptions>(args) .MapResult( ``` ## How was this tested? - [x] Integration Tests
- Loading branch information
1 parent
dbcb651
commit 0fbf7cf
Showing
11 changed files
with
159 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using System.Runtime.CompilerServices; | ||
|
||
[assembly: InternalsVisibleTo("Cli.Tests")] | ||
namespace Cli.Constants | ||
{ | ||
internal class CliReturnCode | ||
{ | ||
public const int SUCCESS = 0; | ||
public const int GENERAL_ERROR = -1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using CommandLine; | ||
|
||
namespace Cli | ||
{ | ||
/// <summary> | ||
/// Processes errors that occur during parsing of CLI verbs (start, init, export, add, update, etc) and their arguments. | ||
/// </summary> | ||
public class DabCliParserErrorHandler | ||
{ | ||
/// <summary> | ||
/// Processes errors accumulated by each parser in parser.ParseArguments<parsers>(). | ||
/// For DAB CLI, this only includes scenarios where the user provides invalid DAB CLI input. | ||
/// e.g. incorrectly formed or missing options and parameters. | ||
/// Additionally, an error is tracked if the user uses: | ||
/// -> an unsupported CLI verb | ||
/// -> --help. | ||
/// -> --version | ||
/// </summary> | ||
/// <param name="err">Collection of Error objects collected by the CLI parser.</param> | ||
/// <returns>Return code: 0 when --help is used, otherwise -1.</returns> | ||
public static int ProcessErrorsAndReturnExitCode(IEnumerable<Error> err) | ||
{ | ||
// To know if `--help` or `--version` was requested. | ||
bool isHelpOrVersionRequested = false; | ||
|
||
/// 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 isHelpOrVersionRequested ? 0 : -1; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.