parse method

void parse(
  1. Iterable<String> input
)

Implementation

void parse(final Iterable<String> input) {
  completions.clear();
  final args = [...input], endsWithSpace = args.lastOrNull == '';
  if (endsWithSpace) args.pop();

  String toComplete = args.lastOrNull ?? '';
  final previousArgs = args.isNotEmpty
      ? args.sublist(0, args.length - 1)
      : args;

  if (endsWithSpace) {
    if (toComplete.isNotEmpty) previousArgs.add(toComplete);
    toComplete = '';
  }

  final (matchedCommand, _) = matchCommand(previousArgs);
  final lastPreArg = previousArgs.lastOrNull;

  if (shouldCompleteFlags(lastPreArg, toComplete)) {
    handleFlagCompletion(matchedCommand, toComplete, lastPreArg);
  } else {
    if (lastPreArg?.startsWith('-') == true &&
        toComplete.isEmpty &&
        endsWithSpace) {
      Option? option = lastPreArg != null
          ? findOption(this, lastPreArg)
          : null;
      if (option != null && lastPreArg != null) {
        for (final command in commands.values) {
          option = findOption(command, lastPreArg);
          if (option != null) break;
        }
      }

      if (option != null && option.isBool == true) {
        complete(toComplete);
        return;
      }
    }

    if (shouldCompleteCommands(toComplete)) {
      handleCommandCompletion(previousArgs, toComplete);
    }

    if (matchedCommand.arguments.isNotEmpty) {
      handlePositionalCompletion(matchedCommand, previousArgs, toComplete);
    }
  }

  complete(toComplete);
}