preprocessRootFlag function

(List<String>, bool) preprocessRootFlag(
  1. List<String> args
)

Preprocess command-line arguments to handle special -R behavior.

The -R/--root flag can be used in two ways:

  1. Bare -R - Use detected workspace root
  2. -R <path> - Use specified path as workspace root

Returns a record with:

  • processedArgs: The args with bare -R converted to marker
  • bareRoot: Whether bare -R was detected

Implementation

(List<String> processedArgs, bool bareRoot) preprocessRootFlag(
  List<String> args,
) {
  final processedArgs = <String>[];
  var bareRoot = false;

  for (var i = 0; i < args.length; i++) {
    final arg = args[i];

    if (arg == '-R' || arg == '--root') {
      final hasNextArg = i + 1 < args.length;
      final nextArg = hasNextArg ? args[i + 1] : null;

      if (!hasNextArg ||
          (nextArg != null &&
              (nextArg.startsWith('-') ||
                  nextArg.startsWith(':') ||
                  _isPipelineOrCommandName(nextArg)))) {
        bareRoot = true;
        processedArgs.add('--root=__BARE_ROOT__');
      } else {
        processedArgs.add(arg);
      }
    } else if (arg.startsWith('-R=') || arg.startsWith('--root=')) {
      processedArgs.add(arg);
    } else {
      processedArgs.add(arg);
    }
  }

  return (processedArgs, bareRoot);
}