resolveCommandPath method

CommandResolution resolveCommandPath(
  1. List<String> args
)

Resolves command tokens while skipping values owned by applicable inputs.

Implementation

CommandResolution resolveCommandPath(List<String> args) {
  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 ownedLength = registry.registeredInputTokenLength(token);
    if (ownedLength != null) {
      index += ownedLength - 1;
      continue;
    }
    if (path.isEmpty && token == name) {
      indices.add(index);
      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);
    }
  }
  return CommandResolution(
    registry: registry,
    path: List.unmodifiable([name, ...path.map((item) => item.name)]),
    tokenIndices: Set.unmodifiable(indices),
  );
}