patternOption function Null safety

RegExp? patternOption(
  1. String longName,
  2. String? shortName,
  3. String argument
)

Tests whether the argument has the longName or the shortName and a pattern. A pattern is a glob expression (wildcards: '' and '?) or a regular expression (if starting with '|'). Returns the RegExp or null. Throws OptionException on error. longName: the long name to inspect, e.g. 'log-file' shortName: the short name to inspect, e.g. 'l' argument: argument to inspect, e.g. '--exclude-file=.log'

Implementation

RegExp? patternOption(String longName, String? shortName, String argument) {
  RegExp? rc;
  String? pattern;
  if (argument == '--$longName' || argument == '--$longName=') {
    throw OptionException('missing =<pattern> in $argument');
  } else if (argument.startsWith('--$longName=')) {
    pattern = argument.substring(longName.length + 3);
  } else if (shortName != null && argument.startsWith('-$shortName')) {
    pattern = argument.substring(2);
  }
  if (pattern != null) {
    var caseSensitive = true;
    if (pattern.endsWith('||i')) {
      caseSensitive = false;
      pattern = pattern.substring(0, pattern.length - 3);
    }
    if (pattern.startsWith('|')) {
      pattern = pattern.substring(1);
    } else {
      pattern = globPatternToRegExpression(pattern);
    }
    try {
      rc = RegExp(pattern, caseSensitive: caseSensitive);
    } on Exception catch (exc) {
      throw OptionException(
          'syntax error (reg. expression) in $argument: $exc');
    }
  }
  return rc;
}