PrefsOption<T>.parse constructor

PrefsOption<T>.parse(
  1. String prefs
)

Implementation

factory PrefsOption.parse(String prefs) {
  final match = _preferencePattern.firstMatch(prefs);
  if (match == null) {
    return InvalidOption('Not a valid prefs option: "$prefs".')
        as PrefsOption<T>;
  }
  final name = match.group(1);
  final valueString = match.group(2);
  if (valueString.startsWith('"') && valueString.endsWith('"')) {
    final value = valueString
        .substring(1, valueString.length - 1)
        .replaceAll(r'\"', r'"')
        .replaceAll(r'\\', r'\');
    return StringOption(name, value) as PrefsOption<T>;
  }
  if (valueString.toLowerCase() == 'true') {
    return BooleanOption(name, true) as PrefsOption<T>;
  } else if (valueString.toLowerCase() == 'false') {
    return BooleanOption(name, false) as PrefsOption<T>;
  }
  try {
    int value = int.parse(valueString);
    return IntegerOption(name, value) as PrefsOption<T>;
  } catch (_) {}
  return InvalidOption('Not a valid prefs option: "$prefs".')
      as PrefsOption<T>;
}