1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| import org.jline.reader.Completer; import org.jline.reader.LineReader; import org.jline.reader.LineReaderBuilder; import org.jline.reader.impl.completer.StringsCompleter; import org.jline.terminal.Terminal; import org.jline.terminal.TerminalBuilder; import org.jline.utils.OSUtils;
import java.io.IOException; import java.util.Arrays; public class CommandLineInterface { public static void main(String[] args) throws IOException { Terminal terminal; if (OSUtils.IS_WINDOWS) { terminal = TerminalBuilder.builder() .name("dumb") .dumb(true) .build(); } else { terminal = TerminalBuilder.terminal(); } Completer completer = new StringsCompleter(Arrays.asList("help", "quit", "hello"));
LineReader reader = LineReaderBuilder.builder() .terminal(terminal) .completer(completer) .build();
String prompt = "cli> ";
String line; while ((line = reader.readLine(prompt)) != null) { switch (line) { case "help": System.out.println("This is a help message"); break; case "quit": return; case "hello": System.out.println("Hello world!"); break; default: System.out.println("Unknown command"); } } } }
|