parseOptionValue<R> method

R? parseOptionValue<R>(
  1. String name,
  2. R? tryParse(
    1. String
    )
)

Tries to parse an option value.

Returns null if the option was not supplied. Throws an ArgParserException if the option was supplied but is invalid.

Implementation

R? parseOptionValue<R>(String name, R? Function(String) tryParse) {
  var rawValue = this[name];

  String? stringValue;
  if (rawValue is bool) {
    // This function is meant to be used with options added with
    // [ArgParser.addOption], but try to handle boolean flags added with
    // [ArgParser.addFlag] as a precaution.
    stringValue = rawValue ? 'true' : 'false';
  } else {
    stringValue = this[name] as String?;
    if (stringValue == null) {
      return null;
    }
  }

  var value = tryParse(stringValue);
  if (value == null) {
    throw ArgParserException('Invalid value for "$name": "$stringValue"');
  }
  return value;
}