run method

  1. @override
Future<ExitCode> run(
  1. Iterable<String> args
)
override

Parses args and invokes Command.run on the chosen command.

This always returns a Future in case the command is asynchronous. The Future will throw a UsageException if args was invalid.

Implementation

@override
Future<ExitCode> run(Iterable<String> args) async {
  ExitCode exitCode;

  try {
    logger.detail('Received args: $args');

    final argsToUse = [...args];

    if (args.isNotEmpty) {
      logger.detail('Checking for test command');
      final first = argsToUse.first;
      final second = argsToUse.length > 1 ? argsToUse[1] : null;

      if (first == 'test' &&
          (second == null ||
              second.startsWith('-') ||
              second.startsWith('.${path.separator}') ||
              second.startsWith('test${path.separator}'))) {
        logger.detail('Inserting `run` to args list for `test` command');
        // insert `run` to 2nd position
        argsToUse.insert(1, 'run');
      }
    }

    final argResults = parse(argsToUse);

    logger.detail('VERSION CHECK: ${argResults['version-check']}');

    exitCode = await runCommand(argResults);
  } catch (error, stack) {
    logger
      ..err('$error')
      ..detail('$stack');
    exitCode = ExitCode.software;
  } finally {
    if (args.isNotEmpty && args.first != 'update') {
      final anyResult = (AnyArgParser()
            ..addFlag(
              'version-check',
              defaultsTo: true,
            ))
          .parse(args);

      if (anyResult['version-check'] as bool) {
        logger.detail('Checking for updates');
        await checkForUpdate();
      } else {
        logger.detail('Skipping version check');
      }
    } else {
      logger.detail('Skipping version check');
    }
  }

  return exitCode;
}