Skip to content

Commit c33dadd

Browse files
authored
feat: add cli completion (#600)
1 parent 46e7eef commit c33dadd

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

lib/src/command_runner.dart

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'package:args/args.dart';
22
import 'package:args/command_runner.dart';
3+
import 'package:cli_completion/cli_completion.dart';
34
import 'package:mason/mason.dart' hide packageVersion;
45
import 'package:pub_updater/pub_updater.dart';
56
import 'package:usage/usage_io.dart';
@@ -18,7 +19,7 @@ const packageName = 'very_good_cli';
1819
/// {@template very_good_command_runner}
1920
/// A [CommandRunner] for the Very Good CLI.
2021
/// {@endtemplate}
21-
class VeryGoodCommandRunner extends CommandRunner<int> {
22+
class VeryGoodCommandRunner extends CompletionCommandRunner<int> {
2223
/// {@macro very_good_command_runner}
2324
VeryGoodCommandRunner({
2425
Analytics? analytics,
@@ -85,6 +86,7 @@ class VeryGoodCommandRunner extends CommandRunner<int> {
8586
normalizedResponse == 'y' || normalizedResponse == 'yes';
8687
}
8788
final _argResults = parse(args);
89+
8890
if (_argResults['verbose'] == true) {
8991
_logger.level = Level.verbose;
9092
}
@@ -107,6 +109,11 @@ class VeryGoodCommandRunner extends CommandRunner<int> {
107109

108110
@override
109111
Future<int?> runCommand(ArgResults topLevelResults) async {
112+
if (topLevelResults.command?.name == 'completion') {
113+
await super.runCommand(topLevelResults);
114+
return ExitCode.success.code;
115+
}
116+
110117
_logger
111118
..detail('Argument information:')
112119
..detail(' Top level options:');

pubspec.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ environment:
88

99
dependencies:
1010
args: ^2.1.0
11+
cli_completion: 0.1.0
1112
glob: ^2.0.2
1213
lcov_parser: ^0.1.2
1314
mason: ">=0.1.0-dev.39 <0.1.0-dev.40"

test/src/command_runner_test.dart

+12
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,18 @@ void main() {
191191
expect(result, equals(ExitCode.success.code));
192192
});
193193

194+
test('handles completion command', () async {
195+
final result = await commandRunner.run(['completion']);
196+
verifyNever(() => logger.info(any()));
197+
verifyNever(() => logger.err(any()));
198+
verifyNever(() => logger.warn(any()));
199+
verifyNever(() => logger.write(any()));
200+
verifyNever(() => logger.success(any()));
201+
verifyNever(() => logger.detail(any()));
202+
203+
expect(result, equals(ExitCode.success.code));
204+
});
205+
194206
group('--help', () {
195207
test('outputs usage', () async {
196208
final result = await commandRunner.run(['--help']);

0 commit comments

Comments
 (0)