promptChooseMultiple static method

Future<List<String>> promptChooseMultiple(
  1. String question,
  2. List<String> options
)

Implementation

static Future<List<String>> promptChooseMultiple(String question, List<String> options) async {
  while (true) {
    stdout.writeln(question);
    for (int i = 0; i < options.length; i++) {
      stdout.writeln('${i + 1}. ${options[i]}');
    }
    stdout.write('Enter the numbers of your choices, separated by commas: ');
    String? input = stdin.readLineSync()?.trim();
    List<int>? choices = input
        ?.split(',')
        .map((e) => int.tryParse(e.trim()))
        .where((e) => e != null && e > 0 && e <= options.length)
        .cast<int>()
        .toList();
    if (choices != null && choices.isNotEmpty) {
      return choices.map((choice) => options[choice - 1]).toList();
    } else {
      stdout.writeln('Invalid choices. Please enter numbers between 1 and ${options.length}, separated by commas.');
    }
  }
}