getValidatedInput static method

String? getValidatedInput({
  1. String? consoleMessage,
  2. String? errorMessage,
  3. bool? functionValidator(
    1. String? message
    )?,
})

Gets validated user input from the command line.

consoleMessage is the message to display to the user before getting input. errorMessage is the message to display to the user if the input is invalid. functionValidator is an optional function that takes a string and returns a bool indicating whether the input is valid. If not provided, all input is considered valid.

Trailing commas are automatically removed from the input.

Returns the validated input from the user.

Implementation

static String? getValidatedInput({
  String? consoleMessage,
  String? errorMessage,
  bool? Function(String? message)? functionValidator
}) {
  Console.writeLine(consoleMessage);

  String? message = FileService.removeTrailingComma(Console.readLineSync()?.trim());

  while ((functionValidator?.call(message) ?? true) == false) {
    if (errorMessage != null) {
      Console.writeLine(red('❌  $errorMessage'));
    }
    Console.writeLine(consoleMessage);
    message = Console.readLineSync()?.trim();
  }
  return message;
}