promptSelect static method

Future<int?> promptSelect(
  1. String message,
  2. List<String> options
)

Prompt user to select from multiple options

Implementation

static Future<int?> promptSelect(String message, List<String> options) async {
  print(message);
  for (int i = 0; i < options.length; i++) {
    print('  ${i + 1}. ${options[i]}');
  }

  while (true) {
    stdout.write('Select option (1-${options.length}) or 0 to cancel: ');
    final input = stdin.readLineSync()?.trim() ?? '';

    if (input == '0') {
      return null; // Cancel
    }

    final selection = int.tryParse(input);
    if (selection != null && selection >= 1 && selection <= options.length) {
      return selection - 1; // Convert to 0-based index
    }

    print(
        'Invalid selection. Please enter a number between 0 and ${options.length}');
  }
}