prompt method

  1. @override
Future<int> prompt(
  1. String message,
  2. List<String> choices
)
override

Prompt the user for input.

The message and choices are displayed to the user and the index of the chosen option is returned.

If this environmment is non-interactive (such as when running in a test) this method should throw NonInteractiveBuildException.

Implementation

@override
Future<int> prompt(String message, List<String> choices) async {
  if (!_isInteractive) throw NonInteractiveBuildException();
  while (true) {
    stdout.writeln('\n$message');
    for (var i = 0, l = choices.length; i < l; i++) {
      stdout.writeln('${i + 1} - ${choices[i]}');
    }
    final input = stdin.readLineSync()!;
    final choice = int.tryParse(input) ?? -1;
    if (choice > 0 && choice <= choices.length) return choice - 1;
    stdout.writeln('Unrecognized option $input, '
        'a number between 1 and ${choices.length} expected');
  }
}