Skip to content

Commit c3ca589

Browse files
authored
feat: improve analytics option usage (#83)
1 parent 3cb92cf commit c3ca589

File tree

3 files changed

+41
-42
lines changed

3 files changed

+41
-42
lines changed

README.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,12 @@ See the complete list of commands and usage information.
6262
Usage: very_good <command> [arguments]
6363

6464
Global options:
65-
-h, --help Print this usage information.
66-
--version Print the current version.
67-
--analytics Opt into or out of anonymous usage statistics.
65+
-h, --help Print this usage information.
66+
--version Print the current version.
67+
--analytics Toggle anonymous usage statistics.
68+
69+
[false] Disable anonymous usage statistics
70+
[true] Enable anonymous usage statistics
6871

6972
Available commands:
7073
create Creates a new very good flutter application in seconds.

lib/src/command_runner.dart

+7-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@ class VeryGoodCommandRunner extends CommandRunner<int> {
3232
)
3333
..addOption(
3434
'analytics',
35-
help: 'Opt into or out of anonymous usage statistics.',
35+
help: 'Toggle anonymous usage statistics.',
36+
allowed: ['true', 'false'],
37+
allowedHelp: {
38+
'true': 'Enable anonymous usage statistics',
39+
'false': 'Disable anonymous usage statistics',
40+
},
3641
);
3742
addCommand(CreateCommand(analytics: _analytics, logger: logger));
3843
}
@@ -69,10 +74,9 @@ class VeryGoodCommandRunner extends CommandRunner<int> {
6974
..info('')
7075
..info(usage);
7176
return ExitCode.usage.code;
72-
} on UsageException catch (e, stackTrace) {
77+
} on UsageException catch (e) {
7378
_logger
7479
..err(e.message)
75-
..err('$stackTrace')
7680
..info('')
7781
..info(usage);
7882
return ExitCode.usage.code;

test/src/command_runner_test.dart

+28-36
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,25 @@ class MockAnalytics extends Mock implements Analytics {}
1414

1515
class MockLogger extends Mock implements Logger {}
1616

17+
const expectedUsage = [
18+
'🦄 A Very Good Command Line Interface\n'
19+
'\n'
20+
'Usage: very_good <command> [arguments]\n'
21+
'\n'
22+
'Global options:\n'
23+
'-h, --help Print this usage information.\n'
24+
' --version Print the current version.\n'
25+
' --analytics Toggle anonymous usage statistics.\n'
26+
'\n'
27+
' [false] Disable anonymous usage statistics\n'
28+
' [true] Enable anonymous usage statistics\n'
29+
'\n'
30+
'Available commands:\n'
31+
' create Creates a new very good flutter application in seconds.\n'
32+
'\n'
33+
'Run "very_good help <command>" for more information about a command.'
34+
];
35+
1736
void main() {
1837
group('VeryGoodCommandRunner', () {
1938
List<String> printLogs;
@@ -98,51 +117,21 @@ void main() {
98117
});
99118

100119
test('handles no command', overridePrint(() async {
101-
const expectedPrintLogs = [
102-
'🦄 A Very Good Command Line Interface\n'
103-
'\n'
104-
'Usage: very_good <command> [arguments]\n'
105-
'\n'
106-
'Global options:\n'
107-
'-h, --help Print this usage information.\n'
108-
' --version Print the current version.\n'
109-
''' --analytics Opt into or out of anonymous usage statistics.\n'''
110-
'\n'
111-
'Available commands:\n'
112-
''' create Creates a new very good flutter application in seconds.\n'''
113-
'\n'
114-
'''Run "very_good help <command>" for more information about a command.'''
115-
];
116120
final result = await commandRunner.run([]);
117-
expect(printLogs, equals(expectedPrintLogs));
121+
expect(printLogs, equals(expectedUsage));
118122
expect(result, equals(ExitCode.success.code));
119123
}));
120124

121125
group('--help', () {
122126
test('outputs usage', overridePrint(() async {
123-
const expectedPrintLogs = [
124-
'🦄 A Very Good Command Line Interface\n'
125-
'\n'
126-
'Usage: very_good <command> [arguments]\n'
127-
'\n'
128-
'Global options:\n'
129-
'-h, --help Print this usage information.\n'
130-
' --version Print the current version.\n'
131-
''' --analytics Opt into or out of anonymous usage statistics.\n'''
132-
'\n'
133-
'Available commands:\n'
134-
''' create Creates a new very good flutter application in seconds.\n'''
135-
'\n'
136-
'''Run "very_good help <command>" for more information about a command.'''
137-
];
138127
final result = await commandRunner.run(['--help']);
139-
expect(printLogs, equals(expectedPrintLogs));
128+
expect(printLogs, equals(expectedUsage));
140129
expect(result, equals(ExitCode.success.code));
141130

142131
printLogs.clear();
143132

144133
final resultAbbr = await commandRunner.run(['-h']);
145-
expect(printLogs, equals(expectedPrintLogs));
134+
expect(printLogs, equals(expectedUsage));
146135
expect(resultAbbr, equals(ExitCode.success.code));
147136
}));
148137
});
@@ -160,10 +149,13 @@ void main() {
160149
verify(analytics.enabled = false);
161150
});
162151

163-
test('sets analytics.enabled to false (garbage value)', () async {
152+
test('does not accept erroneous input', () async {
164153
final result = await commandRunner.run(['--analytics', 'garbage']);
165-
expect(result, equals(ExitCode.success.code));
166-
verify(analytics.enabled = false);
154+
expect(result, equals(ExitCode.usage.code));
155+
verifyNever(analytics.enabled);
156+
verify(logger.err(
157+
'"garbage" is not an allowed value for option "analytics".',
158+
)).called(1);
167159
});
168160

169161
test('exits with bad usage when missing value', () async {

0 commit comments

Comments
 (0)