askMultipleChoice method
Implementation
List<String> askMultipleChoice(
String question,
List<String> options, {
List<String>? defaults,
}) {
if (options.isEmpty) return [];
if (defaults == null || defaults.isEmpty) {
defaults = const [];
}
output.write('\n$question\n');
for (var i = 0; i < options.length; i++) {
final marker = defaults.contains(options[i]) ? 'x' : ' ';
output.write(' [$marker] ${i + 1}. ${options[i]}\n');
}
output.write(
'Enter numbers separated by comma (default: ${defaults.isEmpty ? "none" : defaults.join(", ")}): ',
);
final input = readLine()?.trim() ?? '';
if (input.isEmpty) {
return defaults;
}
final selected = <String>[];
final parts = input.split(RegExp(r'[,\s]+'));
for (final part in parts) {
final index = int.tryParse(part);
if (index != null && index >= 1 && index <= options.length) {
final option = options[index - 1];
if (!selected.contains(option)) {
selected.add(option);
}
}
}
if (selected.isEmpty) {
return defaults;
}
return selected;
}