ArgsSimple.parse constructor

ArgsSimple.parse([
  1. List<String> args = const <String>[]
])

Implementation

ArgsSimple.parse([List<String> args = const <String>[]]) {
  final length = args.length;

  for (var i = 0; i < length;) {
    var a = args[i];

    if (a.startsWith('--')) {
      var k = normalizeKey(a.substring(2));
      var v = i < length - 1 ? args[i + 1] : '';
      var prev = _options[k];
      if (prev != null) {
        if (prev is List) {
          prev.add(v);
        } else {
          _options[k] = [prev, v];
        }
      } else {
        _options[k] = v;
      }
      i += 2;
    } else if (a.startsWith('-P') || a.startsWith('-D')) {
      var idx = a.indexOf(_propertyDelimiter);
      if (idx > 0) {
        var k = normalizeKey(a.substring(2, idx));
        var v = a.substring(idx + 1);
        _properties[k] = v;
      } else {
        var k = normalizeKey(a.substring(2));
        _properties[k] = '';
      }
      ++i;
    } else if (a.startsWith('-')) {
      var s = a.substring(1);
      var n = num.tryParse(s);
      if (n != null) {
        _arguments.add(a);
      } else {
        var k = normalizeKey(s);
        _flags.add(k);
      }
      ++i;
    } else {
      _arguments.add(a);
      ++i;
    }
  }
}