Skip to content

Commit 1523dcb

Browse files
authored
feat: show command usage on exception (#414)
1 parent 6f11940 commit 1523dcb

File tree

4 files changed

+8
-13
lines changed

4 files changed

+8
-13
lines changed

lib/src/command_runner.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class VeryGoodCommandRunner extends CommandRunner<int> {
8989
_logger
9090
..err(e.message)
9191
..info('')
92-
..info(usage);
92+
..info(e.usage);
9393
return ExitCode.usage.code;
9494
}
9595
}

lib/src/commands/create/create.dart

+4-9
Original file line numberDiff line numberDiff line change
@@ -207,25 +207,23 @@ class CreateCommand extends Command<int> {
207207
void _validateOrgName(String name) {
208208
final isValidOrgName = _isValidOrgName(name);
209209
if (!isValidOrgName) {
210-
throw UsageException(
210+
usageException(
211211
'"$name" is not a valid org name.\n\n'
212212
'A valid org name has at least 2 parts separated by "."\n'
213213
'Each part must start with a letter and only include '
214214
'alphanumeric characters (A-Z, a-z, 0-9), underscores (_), '
215215
'and hyphens (-)\n'
216216
'(ex. very.good.org)',
217-
usage,
218217
);
219218
}
220219
}
221220

222221
void _validateProjectName(String name) {
223222
final isValidProjectName = _isValidPackageName(name);
224223
if (!isValidProjectName) {
225-
throw UsageException(
224+
usageException(
226225
'"$name" is not a valid package name.\n\n'
227226
'See https://dart.dev/tools/pub/pubspec#name for more information.',
228-
usage,
229227
);
230228
}
231229
}
@@ -247,14 +245,11 @@ class CreateCommand extends Command<int> {
247245

248246
void _validateOutputDirectoryArg(List<String> args) {
249247
if (args.isEmpty) {
250-
throw UsageException(
251-
'No option specified for the output directory.',
252-
usage,
253-
);
248+
usageException('No option specified for the output directory.');
254249
}
255250

256251
if (args.length > 1) {
257-
throw UsageException('Multiple output directories specified.', usage);
252+
usageException('Multiple output directories specified.');
258253
}
259254
}
260255
}

lib/src/commands/packages.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class PackagesGetCommand extends Command<int> {
5353
@override
5454
Future<int> run() async {
5555
if (_argResults.rest.length > 1) {
56-
throw UsageException('Too many arguments', usage);
56+
usageException('Too many arguments');
5757
}
5858

5959
final recursive = _argResults['recursive'] as bool;

test/src/command_runner_test.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ void main() {
147147
});
148148

149149
test('handles UsageException', () async {
150-
final exception = UsageException('oops!', commandRunner.usage);
150+
final exception = UsageException('oops!', 'exception usage');
151151
var isFirstInvocation = true;
152152
when(() => logger.info(any())).thenAnswer((_) {
153153
if (isFirstInvocation) {
@@ -158,7 +158,7 @@ void main() {
158158
final result = await commandRunner.run(['--version']);
159159
expect(result, equals(ExitCode.usage.code));
160160
verify(() => logger.err(exception.message)).called(1);
161-
verify(() => logger.info(commandRunner.usage)).called(1);
161+
verify(() => logger.info('exception usage')).called(1);
162162
});
163163

164164
test(

0 commit comments

Comments
 (0)