Class Prompt

java.lang.Object
com.codename1.cli.Prompt

public final class Prompt extends Object

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 Classes
    Modifier and Type
    Class
    Description
    static final class 
    Thrown when the person cancels a question.
    static interface 
    Turns a choice into the label a selection list shows.
    static final class 
    A multiple-choice list with options: labels, preselected choices, limits.
    static final class 
    A single-choice list with options: labels, a default, page size, filtering.
    static final class 
    A text question with options: a default, a validator, masking.
    static interface 
    Checks an answer before a text prompt accepts it.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    The exit code CommandLine answers when a prompt was cancelled: 128 + SIGINT.
  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    confirm(String question)
    Asks a yes-or-no question with no default: Enter alone answers nothing and the question stays.
    static boolean
    confirm(String question, boolean defaultValue)
    Asks a yes-or-no question; Enter takes defaultValue.
    static boolean
    Whether 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 of choices and answers them in list order.
    static long
    number(String question, long defaultValue, long min, long max)
    Asks for a whole number between min and max; Enter takes defaultValue.
    static String
    password(String question)
    Asks for a secret, echoed as *.
    Raw mode for code that reads Keys itself; close it to restore the terminal.
    static <T> T
    select(String question, List<T> choices)
    Asks for one of choices and answers it.
    static String
    text(String question)
    Asks for a line of text.
    static String
    text(String question, String defaultValue)
    Asks for a line of text; Enter on an empty line takes defaultValue.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • CANCELLED_EXIT_CODE

      public static final int CANCELLED_EXIT_CODE
      The exit code CommandLine answers 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

      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.