promptYesNo function

Future<bool> promptYesNo(
  1. String question, {
  2. bool defaultToYes = true,
})

Notice

If you use this function, then you must invoke sharedStdIn.terminate in order to close the underlying connection to stdin, allowing your program to close automatically without hanging.

See SharedStdIn.terminate

Also See sharedStdIn

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,
}) async {
  final options = defaultToYes ? "Y/n" : "y/N";
  stdout.writeln("$question [$options] ? ");

  final answer = await sharedStdIn.nextLine();

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