run method

  1. @override
FutureOr<T>? run()
override

Runs this command.

The return value is wrapped in a Future if necessary and returned by CommandRunner.runCommand.

Implementation

@override
FutureOr<T>? run() {
  try {
    // Get completion request params from the environment
    final completionState = CompletionState.fromEnvironment(
      runner.environmentOverride,
    );

    // If the parameters in the environment are not supported or invalid,
    // do not proceed with completion complete.
    if (completionState == null) {
      return null;
    }

    // Find the completion level
    final completionLevel = CompletionLevel.find(
      completionState.args,
      runner.argParser,
      runner.commands,
    );

    // Do not complete if the command structure is not recognized
    if (completionLevel == null) {
      return null;
    }

    // Parse the completion level into completion suggestions
    final completionResults = CompletionParser(
      completionLevel: completionLevel,
    ).parse();

    // Render the completion suggestions
    for (final completionResult in completionResults) {
      runner.renderCompletionResult(completionResult);
    }
  } on Exception {
    // Do not output any Exception here, since even error messages are
    // interpreted as completion suggestions
  }
  return null;
}