askYesNo static method

bool askYesNo(
  1. String question, {
  2. bool defaultValue = true,
})

Prompts the user with a yes/no question and reads a confirmation from stdin.

Implementation

static bool askYesNo(String question, {bool defaultValue = true}) {
  final options = defaultValue ? '[Y/n]' : '[y/N]';
  stdout.write('$question $options: ');
  final input = stdin.readLineSync()?.trim().toLowerCase();
  if (input == null || input.isEmpty) {
    return defaultValue;
  }
  if (input == 'y' || input == 'yes') {
    return true;
  }
  if (input == 'n' || input == 'no') {
    return false;
  }
  // Invalid input, prompt again
  return askYesNo(question, defaultValue: defaultValue);
}