Backend API. Server-side code: this runs in a Codename One backend, not in the app on the device.
public final class Prompt
- Object
- 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 Cancelled; a command
run through CommandLine that lets it propagate exits with
CANCELLED_EXIT_CODE, the code a shell gives an interrupted command.
Nested types
class Prompt.Cancelled | Thrown when the person cancels a question. |
interface Prompt.Validator | Checks an answer before a text prompt accepts it. |
interface Prompt.Labeler | Turns a choice into the label a selection list shows. |
class Prompt.Text | A text question with options: a default, a validator, masking. |
class Prompt.Select | A single-choice list with options: labels, a default, page size, filtering. |
class Prompt.MultiSelect | A multiple-choice list with options: labels, preselected choices, limits. |
Fields
public static final int CANCELLED_EXIT_CODE = 130 | The exit code CommandLine answers when a prompt was cancelled: 128 + SIGINT. |
Methods
Inherited methods
Field details
CANCELLED_EXIT_CODE
public static final int CANCELLED_EXIT_CODE = 130CommandLine answers when a prompt was cancelled:
128 + SIGINT.Method details
interactive
public static boolean interactive()rawMode
public static AutoCloseable rawMode()Keys itself; close it to restore the
terminal. Null when standard input is not a terminal.confirm
public static boolean confirm(String question, boolean defaultValue)defaultValue.confirm
public static boolean confirm(String question)text
public static String text(String question)text
public static String text(String question, String defaultValue)defaultValue.password
public static String password(String question)*.number
public static long number(String question, long defaultValue, long min, long max)min and max; Enter takes
defaultValue.select
public static <T> T select(String question, List<T> choices)choices and answers it.multiSelect
public static <T> List<T> multiSelect(String question, List<T> choices)choices and answers them in list order.