chooseOne method

String chooseOne(
  1. String message, {
  2. required List<String> choices,
  3. String? defaultValue,
})

Implementation

String chooseOne(
  String message, {
  required List<String> choices,
  String? defaultValue,
}) {
  if (choices.isEmpty) return defaultValue ?? '';

  if (level != Level.quiet) {
    stdout.writeln(message);
    for (var index = 0; index < choices.length; index++) {
      stdout.writeln('${index + 1}. ${choices[index]}');
    }
  }

  final fallback = defaultValue ?? choices.first;
  final input = stdin.readLineSync()?.trim();
  final selected = int.tryParse(input ?? '');
  if (selected == null || selected < 1 || selected > choices.length) {
    return fallback;
  }
  return choices[selected - 1];
}