Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Introduce BetterCommand to help print usage and set wrap text column for arg parser. #3

Merged
merged 1 commit into from
May 10, 2024
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
1 change: 1 addition & 0 deletions lib/better_command_runner.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
library better_command_runner;

export 'src/better_command_runner/better_command.dart';
export 'src/better_command_runner/better_command_runner.dart';
export 'src/better_command_runner/exit_exception.dart';
22 changes: 22 additions & 0 deletions lib/src/better_command_runner/better_command.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import 'package:args/args.dart';
import 'package:args/command_runner.dart';
import 'package:cli_tools/better_command_runner.dart';

abstract class BetterCommand extends Command {
final PassMessage? _logInfo;
final ArgParser _argParser;

BetterCommand({
PassMessage? logInfo,
int? wrapTextColumn,
}) : _logInfo = logInfo,
_argParser = ArgParser(usageLineLength: wrapTextColumn);

@override
ArgParser get argParser => _argParser;

@override
void printUsage() {
_logInfo?.call(usage);
}
}
42 changes: 42 additions & 0 deletions test/better_command_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import 'package:cli_tools/better_command_runner.dart';
import 'package:test/test.dart';

class MockCommand extends BetterCommand {
static String commandName = 'mock-command';

MockCommand({super.logInfo}) {
argParser.addOption(
'name',
defaultsTo: 'serverpod',
allowed: <String>['serverpod', 'stockholm'],
);
}

@override
String get description => 'Mock command used for testing';

@override
String get name => commandName;

@override
void run() {}
}

void main() {
group('Given a better command registered in the better command runner', () {
var infos = <String>[];
var betterCommand =
MockCommand(logInfo: (String message) => infos.add(message));
var runner = BetterCommandRunner('test', 'test project')
..addCommand(betterCommand);

test(
'when running command option --help flag then usage is printed to commands logInfo',
() async {
await runner.run([MockCommand.commandName, '--help']);

expect(infos, hasLength(1));
expect(infos.first, betterCommand.usage);
});
});
}