runCommand method

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

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 {
  // Fast track completion command
  if (topLevelResults.command?.name == 'completion') {
    await super.runCommand(topLevelResults);
    return ExitCode.success.code;
  }

  // Verbose logs
  _logger
    ..detail('Argument information:')
    ..detail('  Top level options:');
  for (final option in topLevelResults.options) {
    if (topLevelResults.wasParsed(option)) {
      _logger.detail('  - $option: ${topLevelResults[option]}');
    }
  }
  if (topLevelResults.command != null) {
    final commandResult = topLevelResults.command!;
    _logger
      ..detail('  Command: ${commandResult.name}')
      ..detail('    Command options:');
    for (final option in commandResult.options) {
      if (commandResult.wasParsed(option)) {
        _logger.detail('    - $option: ${commandResult[option]}');
      }
    }
  }

  // Run the command or show version
  final int? exitCode;
  if (topLevelResults['version'] == true) {
    _logger.info(packageVersion);
    exitCode = ExitCode.success.code;
  } else {
    exitCode = await super.runCommand(topLevelResults);
  }

  // Check for updates
  if (topLevelResults.command?.name != UpdateCommand.commandName) {
    await _checkForUpdates();
  }

  return exitCode;
}