promptYesNoSync function

bool promptYesNoSync(
  1. String question, {
  2. bool defaultToYes = true,
  3. StringSink? output,
})

Notice

This function does block the calling Isolate, until a full line is available. If you do not want to block, use the asynchronous version instead.

See promptYesNo

Description

Prompt the user with question.

Returns true, if the user answered with yes and false if the user answered with no.

Implementation

bool promptYesNoSync(
  String question, {
  bool defaultToYes = true,
  StringSink? output,
}) {
  final oSink = output ?? stdout;

  final options = defaultToYes ? "Y/n" : "y/N";
  oSink.writeln("$question [$options] ? ");

  final answer = stdin.readLineSync();

  if (null == answer) return defaultToYes;

  return switch (answer.toLowerCase().trim()) {
    "y" => true,
    "n" => false,
    _ => defaultToYes,
  };
}