throwIfConfigurationErrors method

void throwIfConfigurationErrors(
  1. CommandContext<OptionDefinition, dynamic> contexts,
  2. CommandRunMode runMode
)

Throws a CommandUsageException if any command on the command line has configuration errors, including command line parse errors.

Implementation

void throwIfConfigurationErrors(
  final CommandContext contexts,
  final CommandRunMode runMode,
) {
  // Completion mode: Resolves a partial command line. Errors stay on the
  // contexts for middleware and completion to inspect.
  if (runMode == CommandRunMode.completion) return;

  final acceptsPartialCommandLine = _acceptsPartialCommandLine(contexts);

  final commandErrors = <(CommandDefinition, Iterable<String>)>[];
  CommandContext? context = contexts;
  while (context != null) {
    final allErrors = context.config.errors.followedBy(
      context.builtinConfig.errors,
    );
    final errors = acceptsPartialCommandLine
        ? allErrors.where((final error) => !_isMissingValueError(error))
        : allErrors;
    commandErrors.add((context.command, errors));
    context = context.next;
  }
  _throwIfErrors(commandErrors);
}