boolOption function Null safety

Bool boolOption(
  1. String longName,
  2. String shortName,
  3. String argument
)

Tests whether the argument has the longName or the shortName and an boolean. Returns the value of the boolean argument. Throws OptionException on error. longName: the long name to inspect, e.g. 'max-length' shortName: the short name to inspect, e.g. 'm' argument: argument to inspect, e.g. '--max-length=3'

Implementation

Bool boolOption(String longName, String shortName, String argument) {
  var rc = Bool.UNDEF;
  if (argument == '--$longName') {
    rc = Bool.TRUE;
  } else if (argument.startsWith('--$longName=')) {
    var matcher = RegExp('.*=(t(rue)?|f(alse)?)\$').firstMatch(argument);
    if (matcher == null) {
      throw OptionException(
          'missing <bool> in $argument: e.g. "true" or "false"');
    }
    rc = matcher.group(1)?.startsWith('t') ?? false ? Bool.TRUE : Bool.FALSE;
  } else if (argument == '-$shortName') {
    rc = Bool.TRUE;
  }
  return rc;
}