runCommand method

  1. @override
Future<int?> runCommand(
  1. ArgResults topLevelResults
)
override

Runs the command specified by topLevelResults.

This is notionally a protected method. It may be overridden or called from subclasses, but it shouldn't be called externally.

It's useful to override this to handle global flags and/or wrap the entire command in a block. For example, you might handle the --verbose flag here to enable verbose logging before running the command.

This returns the return value of Command.run.

Implementation

@override
Future<int?> runCommand(ArgResults topLevelResults) async {
  final command = topLevelResults.command;

  try {
    if (command case final cmd? when cmd.arguments.isEmpty) {
      final command = switch (commands[cmd.name]) {
        final CreateCommand command => command,
        _ => null,
      };

      if (command != null) {
        logger.detail('Running command: ${command.name}');
        return await command.run();
      }
    }

    return super.runCommand(topLevelResults);
  } catch (e) {
    logger
      ..err('An error occurred')
      ..detail('$e');
  }
  return null;
}