Skip to content
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

feat!: ExitException to handle full range of valid exit codes #20

Merged
merged 1 commit into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/src/better_command_runner/better_command_runner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ typedef OnAnalyticsEvent = void Function(String event);
/// The [BetterCommandRunner] class provides a more enhanced command line interface
/// for running commands and handling command line arguments.
class BetterCommandRunner extends CommandRunner {
/// Process exit code value for command not found -
/// The specified command was not found or couldn't be located.
static const int exitCodeCommandNotFound = 127;

final PassMessage? _logError;
final PassMessage? _logInfo;
final SetLogLevel? _setLogLevel;
Expand Down Expand Up @@ -115,7 +119,7 @@ class BetterCommandRunner extends CommandRunner {
} on UsageException catch (e) {
_onAnalyticsEvent?.call(BetterCommandRunnerAnalyticsEvents.invalid);
_logError?.call(e.toString());
throw ExitException(ExitCodeType.commandNotFound);
throw ExitException(exitCodeCommandNotFound);
}
}

Expand Down Expand Up @@ -169,7 +173,7 @@ class BetterCommandRunner extends CommandRunner {
} on UsageException catch (e) {
_logError?.call(e.toString());
_onAnalyticsEvent?.call(BetterCommandRunnerAnalyticsEvents.invalid);
throw ExitException(ExitCodeType.commandNotFound);
throw ExitException(exitCodeCommandNotFound);
}
}

Expand Down
33 changes: 13 additions & 20 deletions lib/src/better_command_runner/exit_exception.dart
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
enum ExitCodeType {
/// An exception that can be thrown to exit the command with a specific exit code.
class ExitException implements Exception {
/// Successful termination - The command was successful.
static const int codeOk = 0;

/// General errors - This code is often used to indicate generic or
/// unspecified errors.
general(1),

/// Command invoked cannot execute - The specified command was found but
/// couldn't be executed.
commandInvokedCannotExecute(126),

/// Command not found - The specified command was not found or couldn't be
/// located.
commandNotFound(127);
static const int codeError = 1;

const ExitCodeType(this.exitCode);
/// The exit code to use.
final int exitCode;
}

/// An exception that can be thrown to exit the command with a specific exit
class ExitException implements Exception {
/// Creates an instance of [ExitException].
ExitException([this.exitCodeType = ExitCodeType.general]);
/// Creates an instance of [ExitException] with a given exit code.
ExitException(this.exitCode);

/// The type of exit code to use.
final ExitCodeType exitCodeType;
/// Creates an instance of [ExitException] with an OK exit code (0).
ExitException.ok() : exitCode = codeOk;

/// The exit code to use.
int get exitCode => exitCodeType.exitCode;
/// Creates an instance of [ExitException] with a general error exit code (1).
ExitException.error() : exitCode = codeError;
}
2 changes: 1 addition & 1 deletion lib/src/prompts/select.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ Future<List<Option>> _interactiveSelect(

var quit = keyCode == KeyCodes.q;
if (quit) {
throw ExitException();
throw ExitException.error();
}

if (keyCode == KeyCodes.escapeSequenceStart) {
Expand Down
6 changes: 3 additions & 3 deletions test/better_command_runner/exit_exceptions_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void main() {
await expectLater(
() => runner.run(args),
throwsA(predicate<ExitException>(
(e) => e.exitCodeType == ExitCodeType.commandNotFound)),
(e) => e.exitCode == BetterCommandRunner.exitCodeCommandNotFound)),
);
});

Expand All @@ -53,7 +53,7 @@ void main() {
await expectLater(
() => runner.run(args),
throwsA(predicate<ExitException>(
(e) => e.exitCodeType == ExitCodeType.commandNotFound)),
(e) => e.exitCode == BetterCommandRunner.exitCodeCommandNotFound)),
);
});

Expand All @@ -65,7 +65,7 @@ void main() {
await expectLater(
() => runner.run(args),
throwsA(predicate<ExitException>(
(e) => e.exitCodeType == ExitCodeType.commandNotFound)),
(e) => e.exitCode == BetterCommandRunner.exitCodeCommandNotFound)),
);
});
});
Expand Down
Loading