Module id.xfunction

Class CommandLineInterface

Object
CommandLineInterface

public class CommandLineInterface extends Object
Set of functions to work with command line interface
  • Field Details

  • Constructor Details

    • CommandLineInterface

      public CommandLineInterface()
      Default ctor which binds new CLI object to System.in, System.out
    • CommandLineInterface

      public CommandLineInterface(InputStream in, OutputStream out, OutputStream err)
  • Method Details

    • waitPressEnter

      public void waitPressEnter()
      Wait user to press enter
    • askPressEnter

      public void askPressEnter()
      Print user message "Press Enter to continue..." and wait
    • wasEnterKeyPressed

      public boolean wasEnterKeyPressed()
      When this method is called for the first time it returns false. All consecutive calls will return false as well except when user press Enter key since the time when this method was called last time.

      It allows you to execute some action repeatedly without blocking it to wait for user to press Enter key:

      
       while (!wasEnterKeyPressed()) {
           action();
       }
       

      Here action() will be executed indefinitely until user press any key.

    • read

      public String read(String msg)
      Show message to the user and return what he enters
    • read

      public String read()
      Read a line until user enters and return it
    • readInt

      public int readInt()
      Read an integer entered by the user and return it
    • readPassword

      public String readPassword(String fmt, Object... args)
      Read password entered safely from the user
    • askConfirm

      public boolean askConfirm(String message)
      Print user message and ask to confirm it entering either "yes" or "no"
      Returns:
      whether user confirmed or not
    • fail

      public void fail(String fmt, Object... args)
      Print error to stderr and terminate application with error code 1
    • print

      public void print(String fmt, Object... args)
      Print formatted message to stdout adding new line at the end
    • print

      public void print(String message)
      Print message to stdout adding new line at the end
    • print

      public void print(Object obj)
      Print object to stdout adding new line at the end
    • printerr

      public void printerr(String fmt, Object... args)
      Print formatted message to stderr adding new line at the end
    • printerr

      public void printerr(String message)
      Print formatted message to stderr adding new line at the end
    • printerr

      public void printerr(Object obj)
      Print object to stderr adding new line at the end
    • teeToFile

      public void teeToFile(Path path)
      Attach T pipe for all output streams (out, err) and forward it to the file
    • echo

      public static void echo(boolean enabled) throws Exception
      Enable or disable echo to stdout for any key user press on keyboard.

      This operation works only in systems with "stty" installed. If "stty" is not found or if it returns error code the Exception will be thrown.

      Throws:
      Exception
    • nonBlockingSystemInput

      public static void nonBlockingSystemInput() throws Exception
      Usually any read operation on System.in blocks until user press Enter (new line). This operation allows to disable such behavior so that any key which user press on keyboard will be immediately available.

      This operation works only in systems with "stty" installed. If "stty" is not found or if it returns error code the Exception will be thrown.

      Throws:
      Exception