diff --git a/README.md b/README.md index 16e7fcc78..06e2dcab7 100644 --- a/README.md +++ b/README.md @@ -62,9 +62,12 @@ See the complete list of commands and usage information. Usage: very_good [arguments] Global options: --h, --help Print this usage information. - --version Print the current version. - --analytics Opt into or out of anonymous usage statistics. +-h, --help Print this usage information. + --version Print the current version. + --analytics Toggle anonymous usage statistics. + + [false] Disable anonymous usage statistics + [true] Enable anonymous usage statistics Available commands: create Creates a new very good flutter application in seconds. diff --git a/lib/src/command_runner.dart b/lib/src/command_runner.dart index 87a44feab..5bf0709aa 100644 --- a/lib/src/command_runner.dart +++ b/lib/src/command_runner.dart @@ -32,7 +32,12 @@ class VeryGoodCommandRunner extends CommandRunner { ) ..addOption( 'analytics', - help: 'Opt into or out of anonymous usage statistics.', + help: 'Toggle anonymous usage statistics.', + allowed: ['true', 'false'], + allowedHelp: { + 'true': 'Enable anonymous usage statistics', + 'false': 'Disable anonymous usage statistics', + }, ); addCommand(CreateCommand(analytics: _analytics, logger: logger)); } @@ -69,10 +74,9 @@ class VeryGoodCommandRunner extends CommandRunner { ..info('') ..info(usage); return ExitCode.usage.code; - } on UsageException catch (e, stackTrace) { + } on UsageException catch (e) { _logger ..err(e.message) - ..err('$stackTrace') ..info('') ..info(usage); return ExitCode.usage.code; diff --git a/test/src/command_runner_test.dart b/test/src/command_runner_test.dart index 31621ce9a..a277008d1 100644 --- a/test/src/command_runner_test.dart +++ b/test/src/command_runner_test.dart @@ -14,6 +14,25 @@ class MockAnalytics extends Mock implements Analytics {} class MockLogger extends Mock implements Logger {} +const expectedUsage = [ + '🦄 A Very Good Command Line Interface\n' + '\n' + 'Usage: very_good [arguments]\n' + '\n' + 'Global options:\n' + '-h, --help Print this usage information.\n' + ' --version Print the current version.\n' + ' --analytics Toggle anonymous usage statistics.\n' + '\n' + ' [false] Disable anonymous usage statistics\n' + ' [true] Enable anonymous usage statistics\n' + '\n' + 'Available commands:\n' + ' create Creates a new very good flutter application in seconds.\n' + '\n' + 'Run "very_good help " for more information about a command.' +]; + void main() { group('VeryGoodCommandRunner', () { List printLogs; @@ -98,51 +117,21 @@ void main() { }); test('handles no command', overridePrint(() async { - const expectedPrintLogs = [ - '🦄 A Very Good Command Line Interface\n' - '\n' - 'Usage: very_good [arguments]\n' - '\n' - 'Global options:\n' - '-h, --help Print this usage information.\n' - ' --version Print the current version.\n' - ''' --analytics Opt into or out of anonymous usage statistics.\n''' - '\n' - 'Available commands:\n' - ''' create Creates a new very good flutter application in seconds.\n''' - '\n' - '''Run "very_good help " for more information about a command.''' - ]; final result = await commandRunner.run([]); - expect(printLogs, equals(expectedPrintLogs)); + expect(printLogs, equals(expectedUsage)); expect(result, equals(ExitCode.success.code)); })); group('--help', () { test('outputs usage', overridePrint(() async { - const expectedPrintLogs = [ - '🦄 A Very Good Command Line Interface\n' - '\n' - 'Usage: very_good [arguments]\n' - '\n' - 'Global options:\n' - '-h, --help Print this usage information.\n' - ' --version Print the current version.\n' - ''' --analytics Opt into or out of anonymous usage statistics.\n''' - '\n' - 'Available commands:\n' - ''' create Creates a new very good flutter application in seconds.\n''' - '\n' - '''Run "very_good help " for more information about a command.''' - ]; final result = await commandRunner.run(['--help']); - expect(printLogs, equals(expectedPrintLogs)); + expect(printLogs, equals(expectedUsage)); expect(result, equals(ExitCode.success.code)); printLogs.clear(); final resultAbbr = await commandRunner.run(['-h']); - expect(printLogs, equals(expectedPrintLogs)); + expect(printLogs, equals(expectedUsage)); expect(resultAbbr, equals(ExitCode.success.code)); })); }); @@ -160,10 +149,13 @@ void main() { verify(analytics.enabled = false); }); - test('sets analytics.enabled to false (garbage value)', () async { + test('does not accept erroneous input', () async { final result = await commandRunner.run(['--analytics', 'garbage']); - expect(result, equals(ExitCode.success.code)); - verify(analytics.enabled = false); + expect(result, equals(ExitCode.usage.code)); + verifyNever(analytics.enabled); + verify(logger.err( + '"garbage" is not an allowed value for option "analytics".', + )).called(1); }); test('exits with bad usage when missing value', () async {