Backend API. Server-side code: this runs in a Codename One backend, not in the app on the device.

public final class Prompt

  1. Object
  2. 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.CancelledThrown when the person cancels a question.
interface Prompt.ValidatorChecks an answer before a text prompt accepts it.
interface Prompt.LabelerTurns a choice into the label a selection list shows.
class Prompt.TextA text question with options: a default, a validator, masking.
class Prompt.SelectA single-choice list with options: labels, a default, page size, filtering.
class Prompt.MultiSelectA multiple-choice list with options: labels, preselected choices, limits.

Fields

public static final int CANCELLED_EXIT_CODE = 130The exit code CommandLine answers when a prompt was cancelled: 128 + SIGINT.

Methods

public static boolean interactive()Whether questions are answered key by key: standard input and output are both terminals.
public static AutoCloseable rawMode()Raw mode for code that reads Keys itself; close it to restore the terminal.
public static boolean confirm(String question, boolean defaultValue)Asks a yes-or-no question; Enter takes defaultValue.
public static boolean confirm(String question)Asks a yes-or-no question with no default: Enter alone answers nothing and the question stays.
public static String text(String question)Asks for a line of text.
public static String text(String question, String defaultValue)Asks for a line of text; Enter on an empty line takes defaultValue.
public static String password(String question)Asks for a secret, echoed as *.
public static long number(String question, long defaultValue, long min, long max)Asks for a whole number between min and max; Enter takes defaultValue.
public static <T> T select(String question, List<T> choices)Asks for one of choices and answers it.
public static <T> List<T> multiSelect(String question, List<T> choices)Asks for any number of choices and answers them in list order.

Inherited methods

Field details

CANCELLED_EXIT_CODE

public static final int CANCELLED_EXIT_CODE = 130
The exit code CommandLine answers when a prompt was cancelled: 128 + SIGINT.

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

public static AutoCloseable rawMode()
Raw mode for code that reads 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)
Asks a yes-or-no question; Enter takes defaultValue.

confirm

public static boolean confirm(String question)
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

public static String text(String question)
Asks for a line of text.

text

public static String text(String question, String defaultValue)
Asks for a line of text; Enter on an empty line takes defaultValue.

password

public static String password(String question)
Asks for a secret, echoed as *.

number

public static long number(String question, long defaultValue, long min, long max)
Asks for a whole number between min and max; Enter takes defaultValue.

select

public static <T> T select(String question, List<T> choices)
Asks for one of choices and answers it.

multiSelect

public static <T> List<T> multiSelect(String question, List<T> choices)
Asks for any number of choices and answers them in list order.