extract static method

({LiveOptions options, List<String> rest}) extract(
  1. List<String> args
)

Removes the shared flags from args, returning them with the remaining arguments in their original order.

Arguments after -- are passed through untouched. Throws a FormatException for a flag missing its value or a bad --timeout.

Implementation

static ({LiveOptions options, List<String> rest}) extract(List<String> args) {
  String? device;
  String? uri;
  bool all = false;
  bool json = false;
  Duration timeout = defaultTimeout;
  final List<String> rest = [];

  String valueFor(String flag, int index) {
    if (index + 1 >= args.length) {
      throw FormatException('Missing value for $flag.');
    }
    return args[index + 1];
  }

  for (int i = 0; i < args.length; i++) {
    final String arg = args[i];
    if (arg == '--') {
      rest.addAll(args.sublist(i));
      break;
    }

    if (arg == '-d' || arg == '--device') {
      device = valueFor(arg, i);
      i++;
    } else if (arg.startsWith('--device=')) {
      device = arg.substring('--device='.length);
    } else if (arg == '--uri') {
      uri = valueFor(arg, i);
      i++;
    } else if (arg.startsWith('--uri=')) {
      uri = arg.substring('--uri='.length);
    } else if (arg == '--timeout') {
      timeout = parseTimeout(valueFor(arg, i));
      i++;
    } else if (arg.startsWith('--timeout=')) {
      timeout = parseTimeout(arg.substring('--timeout='.length));
    } else if (arg == '--all') {
      all = true;
    } else if (arg == '--json') {
      json = true;
    } else {
      rest.add(arg);
    }
  }

  return (
    options: LiveOptions(
      device: device,
      all: all,
      json: json,
      uri: uri,
      timeout: timeout,
    ),
    rest: rest,
  );
}