promptYesNo function

Future<bool> promptYesNo(
  1. String question, {
  2. bool defaultToYes = true,
  3. StringSink? output,
  4. PromptAnswerReader answerReader = defaultAnswerReader,
})

Notice

This function does not block the calling Isolate, until a full line is available. Instead a Future that completes when a full line is available is awaited.

If you want to block, use the synchronous version instead.

See promptYesNoSync.

Description

Prompt the user with question.

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

Implementation

Future<bool> promptYesNo(
  String question, {
  bool defaultToYes = true,
  StringSink? output,
  PromptAnswerReader answerReader = defaultAnswerReader,
}) async {
  final oSink = output ?? stdout;
  final options = defaultToYes ? "Y/n" : "y/N";

  oSink.writeln("$question [$options] ? ");

  final answer = await answerReader();

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