confirm function

bool confirm(
  1. String prompt, {
  2. bool? defaultValue,
})

confirm is a specialized version of ask that returns true or false based on the value entered.

The user must enter a valid value or, if a defaultValue is passed, the enter key.

Accepted values are y|t|true|yes and n|f|false|no (case insenstiive).

If the user enters an unknown value an error is printed and they are reprompted.

The prompt is displayed to the user with ' (y/n)' appended.

If a defaultValue is passed then either the y or n will be capitalised and if the user hits the enter key then the defaultValue will be returned.

If the script is not attached to a terminal Terminal().hasTerminal then confirm returns immediately with the defaultValue. If there is no defaultValue then true is returned.

Implementation

bool confirm(String prompt, {bool? defaultValue}) {
  var result = false;
  var matched = false;

  if (!Terminal().hasTerminal) {
    return defaultValue ?? true;
  }
  var finalPrompt = prompt;

  if (defaultValue == null) {
    finalPrompt += ' (y/n):';
  } else {
    if (defaultValue == true) {
      finalPrompt += ' (Y/n):';
    } else {
      finalPrompt += ' (y/N):';
    }
  }

  while (!matched) {
    final entered = Ask()._ask(
      finalPrompt,
      toLower: true,
      hidden: false,
      required: false,
      validator: Ask.dontCare,
    );
    var lower = entered.trim().toLowerCase();

    if (lower.isEmpty && defaultValue != null) {
      lower = defaultValue ? 'true' : 'false';
    }

    if (['y', 't', 'true', 'yes'].contains(lower)) {
      result = true;
      matched = true;
      break;
    }
    if (['n', 'f', 'false', 'no'].contains(lower)) {
      result = false;
      matched = true;
      break;
    }
    print('Invalid value: $entered');
  }
  return result;
}