Class Prompt
Questions a command asks the person running it:
String name = Prompt.text("Project name", "demo");
String db = Prompt.select("Database", Arrays.asList("PostgreSQL", "MySQL", "SQLite"));
List<String> features = Prompt.multiSelect("Features", Arrays.asList("auth", "mail", "jobs"));
String token = Prompt.password("API token");
if (!Prompt.confirm("Create it?", true)) {
return 1;
}
On a terminal each question is answered key by key: a text prompt edits in place (arrows, Home, End, Ctrl-U, Ctrl-W), a selection list moves with the arrows and filters as you type, a multiple selection toggles with Space. The question then collapses to one line showing the answer.
When standard input or output is not a terminal -- a script piping answers
in, a CI job -- the same calls read one line per question, so a tool stays
scriptable: a selection takes a number or the label, a multiple selection a
comma-separated list of either, a confirmation y or n, and an empty line
takes the default. End of input answers the default where there is one.
Ctrl-C, Escape, or end of input with no default throw Prompt.Cancelled; a command
run through CommandLine that lets it propagate exits with
CANCELLED_EXIT_CODE, the code a shell gives an interrupted command.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classThrown when the person cancels a question.static interfaceTurns a choice into the label a selection list shows.static final classA multiple-choice list with options: labels, preselected choices, limits.static final classA single-choice list with options: labels, a default, page size, filtering.static final classA text question with options: a default, a validator, masking.static interfaceChecks an answer before a text prompt accepts it. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe exit codeCommandLineanswers when a prompt was cancelled: 128 + SIGINT. -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanAsks a yes-or-no question with no default: Enter alone answers nothing and the question stays.static booleanAsks a yes-or-no question; Enter takesdefaultValue.static booleanWhether questions are answered key by key: standard input and output are both terminals.static <T> List<T> multiSelect(String question, List<T> choices) Asks for any number ofchoicesand answers them in list order.static longAsks for a whole number betweenminandmax; Enter takesdefaultValue.static StringAsks for a secret, echoed as*.static AutoCloseablerawMode()Raw mode for code that readsKeys itself; close it to restore the terminal.static <T> TAsks for one ofchoicesand answers it.static StringAsks for a line of text.static StringAsks for a line of text; Enter on an empty line takesdefaultValue.
-
Field Details
-
CANCELLED_EXIT_CODE
public static final int CANCELLED_EXIT_CODEThe exit codeCommandLineanswers when a prompt was cancelled: 128 + SIGINT.- See Also:
-
-
Method Details
-
interactive
public static boolean interactive()Whether questions are answered key by key: standard input and output are both terminals. Otherwise they read lines. -
rawMode
Raw mode for code that readsKeys itself; close it to restore the terminal. Null when standard input is not a terminal. -
confirm
Asks a yes-or-no question; Enter takesdefaultValue. -
confirm
Asks a yes-or-no question with no default: Enter alone answers nothing and the question stays. At the end of piped input the answer is no, the choice that changes nothing. -
text
-
text
-
password
-
number
Asks for a whole number betweenminandmax; Enter takesdefaultValue. -
select
-
multiSelect
-