confirm static method

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

Prompts the user to confirm a question.

Returns defaultValue when the user provides no input. Otherwise, returns true when the entered value is y or yes.

Implementation

static bool confirm(
  String question, {
  bool defaultValue = false,
}) {
  final prompt = defaultValue ? '(Y/n)' : '(y/N)';

  stdout.write('$question $prompt: ');

  final input = stdin.readLineSync()?.trim().toLowerCase() ?? '';

  if (input.isEmpty) {
    return defaultValue;
  }

  return input == 'y' || input == 'yes';
}