Package id.xfunction.cli
Class CommandLineInterface
- Object
-
- 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.outCommandLineInterface(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 waitvoid
fail(String fmt, Object... args)
Print error to stderr and terminate application with error code 1void
print(Object obj)
Print object to stdout adding new line at the endvoid
print(String message)
Print message to stdout adding new line at the endvoid
print(String fmt, Object... args)
Print formatted message to stdout adding new line at the endvoid
printerr(Object obj)
Print object to stderr adding new line at the endvoid
printerr(String message)
Print formatted message to stderr adding new line at the endvoid
printerr(String fmt, Object... args)
Print formatted message to stderr adding new line at the endString
read()
Read a line and returns itString
read(String msg)
Show message to the user and return what he entersint
readInt()
Read an integer and returns itString
readPassword(String fmt, Object... args)
Read password safely from the uservoid
teeToFile(Path path)
Attach T pipe for all output streams (out, err) and forward it to the filevoid
waitPressEnter()
Wait user to press enterboolean
wasKeyPressed()
When this method is called for the first time it returns false.
-
-
-
Field Detail
-
cli
public static CommandLineInterface cli
Bounded to System.in, System.out
-
-
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
-
-