getOption function

String getOption(
  1. ArgResults results,
  2. String name,
  3. String message,
  4. Map<String, String> options, {
  5. required bool isInteractive,
  6. String? desc,
  7. String? defaultsTo,
  8. Map<String, String> fullOptions = const {},
})

Implementation

String getOption(
  ArgResults results,
  String name,
  String message,
  Map<String, String> options, {
  required bool isInteractive,
  String? desc,
  String? defaultsTo,
  Map<String, String> fullOptions = const {},
}) {
  var value = results[name] as String?;
  if (!isInteractive) {
    if (value == null) {
      if (defaultsTo != null) {
        return defaultsTo;
      } else {
        print('Missing parameter $name is required.');
        exit(1);
      }
    }
  }
  final fullValues = {...options, ...fullOptions}.values;
  if (value != null && !fullValues.contains(value)) {
    print('Invalid value $value provided. Must be in: ${options.values}');
    value = null;
  }
  while (value == null) {
    if (desc != null) {
      stdout.write(ansi.darkGray.wrap('\n$desc\u{1B}[1A\r'));
    }
    value = options[prompts.choose(message, options.keys)];
    if (desc != null) {
      stdout.write('\r\u{1B}[K');
    }
  }
  return value;
}