Object
CommandLineInterface
Set of functions to work with command line interface
-
Field Summary
-
Constructor Summary
ConstructorDescriptionDefault ctor which binds new CLI object to System.in, System.outCommandLineInterface
(InputStream in, OutputStream out, OutputStream err) -
Method Summary
Modifier and TypeMethodDescriptionboolean
askConfirm
(String message) Print user message and ask to confirm it entering either "yes" or "no"void
Print user message "Press Enter to continue..." and waitstatic void
echo
(boolean enabled) Enable or disable echo to stdout for any key user press on keyboard.void
Print error to stderr and terminate application with error code 1static void
Usually any read operation onSystem.in
blocks until user press Enter (new line).void
Print object to stdout adding new line at the endvoid
Print message to stdout adding new line at the endvoid
Print formatted message to stdout adding new line at the endvoid
Print object to stderr adding new line at the endvoid
Print formatted message to stderr adding new line at the endvoid
Print formatted message to stderr adding new line at the endread()
Read a line until user enters and return itShow message to the user and return what he entersint
readInt()
Read an integer entered by the user and return itreadPassword
(String fmt, Object... args) Read password entered safely from the uservoid
Attach T pipe for all output streams (out, err) and forward it to the filevoid
Wait user to press enterboolean
When this method is called for the first time it returns false.
-
Field Details
-
cli
Bounded to System.in, System.out
-
-
Constructor Details
-
CommandLineInterface
public CommandLineInterface()Default ctor which binds new CLI object to System.in, System.out -
CommandLineInterface
-
-
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
Show message to the user and return what he enters -
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
Read password entered safely from the user -
askConfirm
Print user message and ask to confirm it entering either "yes" or "no"- Returns:
- whether user confirmed or not
-
fail
Print error to stderr and terminate application with error code 1 -
print
Print formatted message to stdout adding new line at the end -
print
Print message to stdout adding new line at the end -
print
Print object to stdout adding new line at the end -
printerr
Print formatted message to stderr adding new line at the end -
printerr
Print formatted message to stderr adding new line at the end -
printerr
Print object to stderr adding new line at the end -
teeToFile
Attach T pipe for all output streams (out, err) and forward it to the file -
echo
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
Usually any read operation onSystem.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
-