/** * Reads a value of type T. * It repeatedly prompts the users to enter the value, until they provide a valid input string. * @param prompt the messages to be displayed for prompting the user to enter the value * @return the value of type T parsed from the input string */ public T read(String... prompt) { return read(Arrays.asList(prompt)); }
@Override public void run() { setChoices(choices.stream().map(Object::toString).collect(Collectors.toList())); setHistory(historyStore.getValues(key)); try { R inputReader = inputReaderSupplier.get(); if (showPrevious) inputReader.withDefaultValue(defaultValueSupplier.get()); if(inputReaderConfigurator != null) { inputReaderConfigurator.accept(inputReader); } if(constrainedInput) { inputReader.withValueChecker((val,name) -> choices.contains(val) ? null : Arrays.asList("'" + val + "' is not in the choice list.")); } T value = inputReader.read(prompt); historyStore.addValue(key, value.toString()); valueSetter.accept(value); } finally { setChoices(Collections.emptyList()); setHistory(Collections.emptyList()); } }