diff --git a/packages/shorebird_cli/lib/src/shorebird_cli_command_runner.dart b/packages/shorebird_cli/lib/src/shorebird_cli_command_runner.dart index 56392e55..a75f40ad 100644 --- a/packages/shorebird_cli/lib/src/shorebird_cli_command_runner.dart +++ b/packages/shorebird_cli/lib/src/shorebird_cli_command_runner.dart @@ -202,6 +202,13 @@ Engine • revision ${shorebirdEnv.shorebirdEngineRevision}'''); exitCode = await super.runCommand(topLevelResults); } on ProcessExit catch (error) { exitCode = error.exitCode; + } on UsageException catch (e) { + logger + ..err(e.message) + ..info(e.usage); + // When on an usage exception we don't need to show the "if you aren't + // sure" message, so we do an early return here. + return ExitCode.usage.code; } catch (error, stackTrace) { logger ..detail('$error') diff --git a/packages/shorebird_cli/test/src/shorebird_cli_command_runner_test.dart b/packages/shorebird_cli/test/src/shorebird_cli_command_runner_test.dart index 603fb477..51a57cd0 100644 --- a/packages/shorebird_cli/test/src/shorebird_cli_command_runner_test.dart +++ b/packages/shorebird_cli/test/src/shorebird_cli_command_runner_test.dart @@ -113,20 +113,23 @@ void main() { }); test('handles UsageException', () async { - final exception = UsageException('oops!', 'exception usage'); - var isFirstInvocation = true; - when(() => logger.info(any())).thenAnswer((_) { - if (isFirstInvocation) { - isFirstInvocation = false; - throw exception; - } - }); final result = await runWithOverrides( - () => commandRunner.run(['--version']), + // fly_to_the_moon is not a valid command. + () => commandRunner.run(['fly_to_the_moon']), ); expect(result, equals(ExitCode.usage.code)); - verify(() => logger.err(exception.message)).called(1); - verify(() => logger.info('exception usage')).called(1); + verify( + () => logger.err('Could not find a command named "fly_to_the_moon".'), + ).called(1); + verify( + () => logger.info( + any( + that: contains( + 'Usage: shorebird [arguments]', + ), + ), + ), + ).called(1); }); test('handles missing option error', () async {