Class Shell

  • All Implemented Interfaces:
    java.lang.AutoCloseable

    public class Shell
    extends java.lang.Object
    implements java.lang.AutoCloseable
    An interactive shell that runs a read-eval-print loop (REPL) using a CommandDispatcher for command execution.

    The shell is a thin REPL loop. The dispatcher is injected or auto-created as a DefaultCommandDispatcher.

    Use builder() to create a shell:

     Shell shell = Shell.builder()
         .terminal(terminal)
         .prompt("myapp> ")
         .dispatcher(dispatcher)
         .groups(group1, group2)
         .build();
     shell.run();
     
    Since:
    4.0
    See Also:
    ShellBuilder, CommandDispatcher
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static ShellBuilder builder()
      Creates a new ShellBuilder.
      void close()
      Closes this shell, releasing the dispatcher and optionally the terminal.
      CommandDispatcher dispatcher()
      Returns the command dispatcher used by this shell.
      boolean isRunning()
      Returns whether the shell is currently running.
      org.jline.reader.LineReader reader()
      Returns the line reader used by this shell.
      void run()
      Runs the interactive REPL loop.
      void stop()
      Stops the REPL loop.
      org.jline.terminal.Terminal terminal()
      Returns the terminal used by this shell.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • run

        public void run()
                 throws java.lang.Exception
        Runs the interactive REPL loop.

        This method blocks until the user exits (e.g. via EOF or an exit command), or until stop() is called.

        Throws:
        java.lang.Exception - if initialization or execution fails
      • stop

        public void stop()
        Stops the REPL loop. The current or next iteration will exit.
      • isRunning

        public boolean isRunning()
        Returns whether the shell is currently running.
        Returns:
        true if running
      • terminal

        public org.jline.terminal.Terminal terminal()
        Returns the terminal used by this shell.
        Returns:
        the terminal
      • reader

        public org.jline.reader.LineReader reader()
        Returns the line reader used by this shell.
        Returns:
        the line reader
      • dispatcher

        public CommandDispatcher dispatcher()
        Returns the command dispatcher used by this shell.
        Returns:
        the command dispatcher
      • close

        public void close()
        Closes this shell, releasing the dispatcher and optionally the terminal.
        Specified by:
        close in interface java.lang.AutoCloseable