multiSelect method

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

Multi-select - allows user to select multiple options

Implementation

List<String> multiSelect(String question, List<String> options) {
  final selected = <String>[];

  print('$question');
  print(
      'Enter the numbers of your choices (comma-separated) or "all" for all options:');

  // Display options with numbers
  for (int i = 0; i < options.length; i++) {
    print('  ${i + 1}. ${options[i]}');
  }

  print('\nYour selection (e.g. "1,3,4" or "all"): ');
  final input = stdin.readLineSync()?.trim().toLowerCase() ?? '';

  if (input == 'all') {
    return List<String>.from(options);
  }

  // Parse number inputs
  final selections = input.split(',');
  for (final selection in selections) {
    try {
      final index = int.parse(selection.trim()) - 1;
      if (index >= 0 && index < options.length) {
        selected.add(options[index]);
      }
    } catch (_) {
      // Skip invalid inputs
    }
  }

  return selected;
}