stringOption function Null safety

String? stringOption(
  1. String longName,
  2. String? shortName,
  3. String argument,
  4. {bool notEmpty = false}
)

Tests whether the argument has the longName or the shortName and a string. Returns the value of the string argument 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. '--log-file=app.log' notEmpty: true: the string may not be empty

Implementation

String? stringOption(String longName, String? shortName, String argument,
    {bool notEmpty = false}) {
  String? rc;
  if (argument == '--$longName') {
    throw OptionException('missing =<string> in $argument');
  } else if (argument.startsWith('--$longName=')) {
    rc = argument.substring(longName.length + 3);
  } else if (shortName != null && argument.startsWith('-$shortName')) {
    rc = argument.substring(2);
  }
  if (rc == '' && notEmpty) {
    throw OptionException('<string> may not be empty: $argument');
  }
  return rc;
}