chooseOne<T> method

Future<T> chooseOne<T>(
  1. String question, {
  2. required List<T> choices,
  3. T? defaultValue,
  4. String display(
    1. T choice
    )?,
  5. bool? filter,
})

Asks question for one of choices, which must not be empty, each shown as display makes it (its toString without).

  • CliMode.interactive: a SelectionList of them under the question, seven rows at most: Up and Down move, Enter picks the one the cursor is on, which starts on defaultValue, or the first. With filter what is typed narrows the list to the choices it matches (fuzzy, as a command palette does), Backspace takes it back; null filters a list of more than seven.
  • CliMode.plain: the choices numbered (1) dev), then a number read, or a choice as it reads; empty, defaultValue, or the first. Any other answer is asked again.
  • CliMode.nonInteractive: defaultValue, written as ? question choice; a defaultValue that is not one of choices, or none, throws PromptAnswerRequiredException.

Ctrl+C, Ctrl+D (with nothing typed to filter) and the end of input leave it as they leave prompt.

final env = await Cli.current.chooseOne(
  'Target environment?',
  choices: ['dev', 'stage', 'prod'],
  defaultValue: 'dev',
);

Implementation

Future<T> chooseOne<T>(
  String question, {
  required List<T> choices,
  T? defaultValue,
  String Function(T choice)? display,
  bool? filter,
}) => _prompter.chooseOne(
  question,
  choices: choices,
  defaultValue: defaultValue,
  display: display,
  filter: filter,
);