dateOption function Null safety

DateTime? dateOption(
  1. String longName,
  2. String? shortName,
  3. String argument
)

Tests whether the argument has the longName or the shortName and a date (and time). Returns the DateTime 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

DateTime? dateOption(String longName, String? shortName, String argument) {
  DateTime? rc;
  String? value;
  if (argument == '--$longName') {
    throw OptionException('missing =<reg-expr> in $argument');
  } else if (argument.startsWith('--$longName=')) {
    value = argument.substring(longName.length + 3);
  } else if (shortName != null && argument.startsWith('-$shortName')) {
    value = argument.substring(2);
  }
  if (value != null) {
    try {
      rc = stringToDateTime(value);
    } on ArgumentError catch (exc) {
      throw OptionException(exc.toString());
    }
  }
  return rc;
}