askChoice method
Prompts to select one choice from options.
Implementation
@override
String askChoice(
String question,
List<String> options, {
String? defaultValue,
}) {
if (options.isEmpty) {
throw ArgumentError('Options list cannot be empty.');
}
_out.writeln('? $question');
for (int i = 0; i < options.length; i++) {
final isDefault = options[i] == defaultValue ? ' (default)' : '';
_out.writeln(' ${i + 1}) ${options[i]}$isDefault');
}
final defaultChoice = defaultValue ?? options.first;
while (true) {
_out.write('Select an option (1-${options.length}) [$defaultChoice]: ');
final input = _in.readLineSync()?.trim() ?? '';
if (input.isEmpty) {
return defaultChoice;
}
final choiceIndex = int.tryParse(input);
if (choiceIndex != null && choiceIndex >= 1 && choiceIndex <= options.length) {
return options[choiceIndex - 1];
}
// Check if user typed option string directly
if (options.contains(input)) {
return input;
}
_out.writeln('Invalid choice. Please enter a number between 1 and ${options.length}.');
}
}