moveNextOption method

String? moveNextOption(
  1. String name, [
  2. String? short
])

Returns the next option that matches the given name.

If matched, the option is removed from the command input.

dart run example.dart build --output=build
dart run example.dart build --output build
dart run example.dart build -o build // short

In the above example, --output=build is the next option, return build as the value.

Implementation

String? moveNextOption(String name, [String? short]) {
  final (flagIndex, valueIndex) = findNextOptionIndex(name, short);
  if (flagIndex == null) return null;
  if (valueIndex == null) return null;

  final offset = switch (_internalArguments[valueIndex]) {
    String(startsWith: final startsWith) when startsWith('--$name=') =>
      name.length + 3,
    String(startsWith: final startsWith)
        when short != null && startsWith('-$short=') =>
      short.length + 2,
    _ => 0,
  };

  final value = _internalArguments[valueIndex].substring(offset);
  if (value.isEmpty || value.startsWith('-')) return null;

  _internalArguments.removeRange(flagIndex, valueIndex);

  return value;
}