confirm static method

bool confirm({
  1. required String title,
  2. bool defaultValue = false,
})

Reads a yes/no answer.

Empty input and end-of-input return defaultValue. Invalid input is reported and the prompt is shown again.

Implementation

static bool confirm({
  required String title,
  bool defaultValue = false,
}) {
  while (true) {
    final suffix = defaultValue ? '[Y/n]' : '[y/N]';
    TerminalContext.output.write('$title $suffix: ');

    final line = TerminalContext.input.readLineSync();
    if (line == null) return defaultValue;

    final value = line.trim().toLowerCase();
    if (value.isEmpty) return defaultValue;

    if (value == 'y' || value == 'yes' || value == 'true' || value == '1') {
      return true;
    }
    if (value == 'n' || value == 'no' || value == 'false' || value == '0') {
      return false;
    }

    _writeError('Enter yes or no.');
  }
}