run method

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

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<T?> run(Iterable<String> args) async {
  final argsList = args.toList();

  if (argsList.contains('--completion-script')) {
    writeOut(shellCompletionScript);
    _setExitCode(0);
    return null;
  }

  if (enableShellCompletion) {
    completion.tryCompletion(
      argsList,
      (compArgs, compLine, compPoint) =>
          shellCompleter.complete(compArgs, compLine, compPoint),
    );
  }

  if (_shouldRunUnknownCommandFallback(argsList)) {
    return await unknownCommandFallback!(List.unmodifiable(argsList));
  }

  final ansi = _resolveAnsiForArgs(argsList);
  if (_rendererInjected) {
    // Deterministic behavior for tests: respect explicit --ansi/--no-ansi but
    // do not auto-detect terminal capabilities.
    if (ansi == false) {
      _renderer.colorProfile = ColorProfile.ascii;
    } else if (ansi == true) {
      // Use a safe default for "ANSI enabled" output.
      _renderer.colorProfile = ColorProfile.trueColor;
    }
  } else {
    if (ansi == false) {
      _renderer = TerminalRenderer(forceProfile: ColorProfile.ascii);
    } else if (ansi == true) {
      _renderer = TerminalRenderer(forceProfile: ColorProfile.trueColor);
    } else {
      _renderer = TerminalRenderer();
    }
  }

  _verbosity = _resolveVerbosityForArgs(argsList);
  _interactive = _resolveInteractiveForArgs(argsList);
  _io = null;

  try {
    return await super.run(argsList);
  } on args_pkg.UsageException catch (e) {
    if (!_handleMissingCommandAsNamespace(e)) {
      _printUsageError(e);
      _setExitCode(usageExitCode);
    }
    return null;
  } finally {
    _io?.dispose();
    if (isSharedStdinStreamStarted) {
      await shutdownSharedStdinStream();
    }
  }
}