resolveCommandPath method

CommandResolution resolveCommandPath(
  1. List<String> args, {
  2. CommandRegistry defaultTarget(
    1. CommandRegistry
    )?,
})

Resolves command tokens while skipping values owned by applicable inputs.

Implementation

CommandResolution resolveCommandPath(
  List<String> args, {
  CommandRegistry Function(CommandRegistry)? defaultTarget,
}) {
  var registry = this;
  final path = <CommandRegistry>[];
  final indices = <int>{};
  for (var index = 0; index < args.length; index++) {
    final token = args[index];
    if (token == '--') break;
    final next = index + 1 < args.length ? args[index + 1] : null;
    final ownedLength =
        registry.registeredInputTokenLength(token, next: next) ??
        defaultTarget
            ?.call(registry)
            .registeredInputTokenLength(token, next: next);
    if (MambaBuiltInFlags.isControl(token)) break;
    if (ownedLength != null) {
      index += ownedLength - 1;
      continue;
    }
    final child = registry.commandRegistries
        .where(
          (candidate) =>
              candidate.name == token ||
              candidate.commandAliases?.contains(token) == true,
        )
        .firstOrNull;
    if (child != null) {
      registry = child;
      path.add(child);
      indices.add(index);
      continue;
    }
    if (path.isEmpty && token == name) {
      throw MambaApplicationNameException(name);
    }
  }
  return CommandResolution(
    registry: registry,
    path: List.unmodifiable(path.map((item) => item.name)),
    tokenIndices: Set.unmodifiable(indices),
  );
}