Skip to content
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
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,12 @@ See the complete list of commands and usage information.
Usage: very_good <command> [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.
Expand Down
10 changes: 7 additions & 3 deletions lib/src/command_runner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ class VeryGoodCommandRunner extends CommandRunner<int> {
)
..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));
}
Expand Down Expand Up @@ -69,10 +74,9 @@ class VeryGoodCommandRunner extends CommandRunner<int> {
..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;
Expand Down
64 changes: 28 additions & 36 deletions test/src/command_runner_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 <command> [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 <command>" for more information about a command.'
];

void main() {
group('VeryGoodCommandRunner', () {
List<String> printLogs;
Expand Down Expand Up @@ -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 <command> [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 <command>" 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 <command> [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 <command>" 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));
}));
});
Expand All @@ -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 {
Expand Down