intOption function

int intOption(
  1. String longName,
  2. String? shortName,
  3. String argument,
  4. int defaultValue,
)

Tests whether the argument has the longName or the shortName and an integer. Returns the value of the int argument or the defaultValue 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' defaultValue: if the arguments does not match: this value is returned

Implementation

int intOption(
    String longName, String? shortName, String argument, int defaultValue) {
  var rc = defaultValue;
  if (argument == '--$longName') {
    throw OptionException('missing "<int>" in $argument');
  } else if (argument.startsWith('--$longName=')) {
    var matcher = RegExp(r'=(\d+)$').firstMatch(argument);
    if (matcher == null) {
      throw OptionException('missing <int> in $argument');
    }
    rc = int.parse(matcher.group(1)!);
  } else if (shortName != null && argument.startsWith('-$shortName')) {
    var matcher = RegExp('^-$shortName(\\d+)\$').firstMatch(argument);
    if (matcher == null) {
      throw OptionException('missing <int> in $argument');
    }
    rc = int.parse(matcher.group(1)!);
  }
  return rc;
}