Class CommandLineInterface


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

      Fields 
      Modifier and Type Field Description
      static CommandLineInterface cli
      Bounded to System.in, System.out
    • Constructor Summary

      Constructors 
      Constructor Description
      CommandLineInterface()
      Default ctor which binds new CLI object to System.in, System.out
      CommandLineInterface​(InputStream in, OutputStream out, OutputStream err)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean askConfirm​(String message)
      Print user message and ask to confirm it entering either "yes" or "no"
      void askPressEnter()
      Print user message to press Enter and wait
      void fail​(String fmt, Object... args)
      Print error to stderr and terminate application with error code 1
      void print​(Object obj)
      Print object to stdout adding new line at the end
      void print​(String message)
      Print message to stdout adding new line at the end
      void print​(String fmt, Object... args)
      Print formatted message to stdout adding new line at the end
      void printerr​(Object obj)
      Print object to stderr adding new line at the end
      void printerr​(String message)
      Print formatted message to stderr adding new line at the end
      void printerr​(String fmt, Object... args)
      Print formatted message to stderr adding new line at the end
      String read()
      Read a line and returns it
      String read​(String msg)
      Show message to the user and return what he enters
      int readInt()
      Read an integer and returns it
      String readPassword​(String fmt, Object... args)
      Read password safely from the user
      void teeToFile​(Path path)
      Attach T pipe for all output streams (out, err) and forward it to the file
      void waitPressEnter()
      Wait user to press enter
      boolean wasKeyPressed()
      When this method is called for the first time it returns false.
      • Methods inherited from class Object

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

      • 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 Detail

      • waitPressEnter

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

        public void askPressEnter()
        Print user message to press Enter and wait
      • wasKeyPressed

        public boolean wasKeyPressed()

        When this method is called for the first time it returns false. All consecutive calls will return false as well except when user press any 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 any key:

        
         while (!wasKeyPressed()) {
             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 and returns it
      • readInt

        public int readInt()
        Read an integer and returns it
      • readPassword

        public String readPassword​(String fmt,
                                   Object... args)
        Read password 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