select method
Asks the user to select an option from a list
Implementation
String select(String question, List<String> options,
{String? defaultOption}) {
info(question);
for (int i = 0; i < options.length; i++) {
final option = options[i];
final isDefault = option == defaultOption;
final marker = isDefault ? '*' : ' ';
print(' $marker ${i + 1}. $option');
}
stdout.write('Enter your choice (1-${options.length}): ');
final input = stdin.readLineSync()?.trim() ?? '';
// Try to parse as integer first
try {
final index = int.parse(input) - 1;
if (index >= 0 && index < options.length) {
return options[index];
}
} catch (_) {
// Not a number, check if it matches an option directly
if (options.contains(input)) {
return input;
}
}
// Return default or first option if input is invalid
return defaultOption ?? options.first;
}