timeoutSeconds static method

int? timeoutSeconds(
  1. List<String> arguments
)

Implementation

static int? timeoutSeconds(List<String> arguments) {
  String? value;
  for (final arg in arguments) {
    if (arg.startsWith('--timeout=')) {
      value = arg.substring('--timeout='.length);
    }
  }
  if (value == null) return defaultTimeoutSeconds;

  final match = RegExp(r'^(\d+)(ms|s|m|h)?$').firstMatch(value);
  if (match == null) {
    stderr.writeln(
      'Invalid --timeout value "$value". Use a duration like 30s, 5m, or 1h.',
    );
    exit(2);
  }

  final amount = int.parse(match.group(1)!);
  final unit = match.group(2) ?? 's';
  final seconds = switch (unit) {
    'ms' => (amount / 1000).ceil(),
    's' => amount,
    'm' => amount * 60,
    'h' => amount * 60 * 60,
    _ => amount,
  };
  if (seconds <= 0) {
    stderr.writeln('--timeout must be greater than zero.');
    exit(2);
  }
  return seconds;
}