multiSelect method
Multi-select - allows user to select multiple options
Implementation
List<String> multiSelect(String question, List<String> options) {
final selected = <String>[];
stdout.writeln(question);
stdout.writeln(
'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++) {
stdout.writeln(' ${i + 1}. ${options[i]}');
}
stdout.writeln('\nYour selection (e.g. "1,3,4" or "all"): ');
final String input = stdin.readLineSync()?.trim().toLowerCase() ?? '';
if (input == 'all') {
return List<String>.from(options);
}
// Parse number inputs
final List<String> selections = input.split(',');
for (final selection in selections) {
try {
final int index = int.parse(selection.trim()) - 1;
if (index >= 0 && index < options.length) {
selected.add(options[index]);
}
} catch (_) {
// Skip invalid inputs
}
}
return selected;
}