chooseShorthand<T> function
T?
chooseShorthand<T>(})
Similar to choose, but opts for a shorthand syntax that fits into one line, rather than a multi-line prompt.
Acceptable inputs include:
- The full value of
toString()
for any one option - The first character (case-insensitive) of
toString()
for an option
A default option may be provided by means of defaultsTo
.
color
, defaultsTo
, inputColor
, and chevron
are forwarded to get.
Implementation
T? chooseShorthand<T>(String message, Iterable<T> options,
{T? defaultsTo,
bool chevron = true,
@deprecated bool colon = true,
AnsiCode inputColor = cyan,
bool color = true,
bool conceal = false}) {
if (options.isEmpty) {
throw ArgumentError.value('`options` may not be empty.');
}
var b = StringBuffer(message);
if (chevron && colon) b.write(':');
b.write(' (');
var firstChars = <String>[], strings = <String>[];
var i = 0;
for (var option in options) {
var str = option.toString();
if (i++ > 0) b.write('/');
if (defaultsTo != null) {
if (defaultsTo == option) {
str = str[0].toUpperCase() + str.substring(1);
} else {
str = str[0].toLowerCase() + str.substring(1);
}
}
b.write(str);
firstChars.add(str[0].toLowerCase());
strings.add(str);
}
b.write(')');
T? value;
get(
b.toString(),
chevron: chevron && colon,
inputColor: inputColor,
color: color,
conceal: conceal,
validate: (s) {
if (s.isEmpty) return (value = defaultsTo) != null;
if (strings.contains(s)) {
value = options.elementAt(strings.indexOf(s));
return true;
}
if (firstChars.contains(s[0].toLowerCase())) {
value = options.elementAt(firstChars.indexOf(s[0].toLowerCase()));
return true;
}
return false;
},
);
return value;
}