getString function
Implementation
String getString(
ArgResults results,
String name,
String message, {
required bool isInteractive,
String? desc,
String? Function(String)? validate,
}) {
var value = results[name] as String?;
if (!isInteractive) {
if (value == null || value.isEmpty) {
print('Missing parameter $name is required.');
exit(1);
}
final error = validate?.call(value);
if (error != null) {
print('Invalid value $value provided: $error');
exit(1);
}
}
while (value == null || value.isEmpty) {
if (desc != null) {
stdout.write(ansi.darkGray.wrap('\n$desc\u{1B}[1A\r'));
}
value = prompts.get(
message,
validate: (e) {
final error = validate?.call(e);
if (error != null) {
// clear the line
stdout.write('\n\r\u{1B}[K');
stdout.write(ansi.red.wrap('$error\u{1B}[1A\r'));
return false;
} else {
stdout.write('\n\r\u{1B}[K');
return true;
}
},
);
if (desc != null) {
stdout.write('\r\u{1B}[K');
}
}
return value;
}