select static method
select method is used to select an option from the list of options.
The message is the message that will be shown in the console.
The options is the list of options.
The isRequired is a boolean that indicates if the option is required or not.
The method returns the selected option.
Implementation
static String select(
String message,
List<String> options, {
bool isRequired = false,
}) {
console.writeLine('\n\n$message\n');
for (var i = 0; i < options.length; i++) {
console.writeLine(" [${i + 1}]. ${options[i]}");
}
var res = read("Enter the number of the option:");
var num = int.tryParse(res);
if ((num == null || num < 1 || num > options.length) && isRequired) {
write("Invalid option!", CappColors.error);
return select(message, options, isRequired: isRequired);
}
return num != null && options.length > num - 1 ? options[num - 1] : '';
}