registerProviderCommands function

void registerProviderCommands(
  1. CommandRunner<void> runner,
  2. Iterable<ProviderCommandRegistration> registrations,
  3. String usage
)

Registers provider commands with the given runner.

Implementation

void registerProviderCommands(
  CommandRunner<void> runner,
  Iterable<ProviderCommandRegistration> registrations,
  String usage,
) {
  if (registrations.isEmpty) {
    return;
  }
  final existingNames = <String>{};
  for (final command in runner.commands.values) {
    existingNames.add(command.name);
    existingNames.addAll(command.aliases);
  }
  for (final registration in registrations) {
    Command<void> command;
    try {
      command = registration.factory();
    } catch (error) {
      throw UsageException(
        'Failed to load provider command "${registration.id}": $error',
        usage,
      );
    }
    final hasConflict =
        existingNames.contains(command.name) ||
        command.aliases.any(existingNames.contains);
    if (hasConflict) {
      throw UsageException(
        'Provider command "${command.name}" conflicts with an existing command.',
        usage,
      );
    }
    runner.addCommand(command);
    existingNames.add(command.name);
    existingNames.addAll(command.aliases);
  }
}