Skip to content

Commit

Permalink
feat!: ExitException to handle full range of valid exit codes (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
christerswahn authored Jan 22, 2025
1 parent 338dff8 commit 47752dc
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 26 deletions.
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

0 comments on commit 47752dc

Please sign in to comment.