Package readycli
Class CLI
java.lang.Object
readycli.CLI
- All Implemented Interfaces:
java.io.Serializable
public final class CLI
extends java.lang.Object
implements java.io.Serializable
Represents a CLI (Command-Line Interface). A CLI object can be created
through the
create(String, String)
method. A CLI object is
responsible of interacting with the user on the given out and in streams. A
CLI object allows to add new commands and to remove existing ones. Moreover,
a CLI object allows to show an application title when it starts and a command
prompt, that can be modified at runtime. A CLI object is not active and it
must be executed through the execute(PrintStream, InputStream)
method.- Author:
- Salvatore Giampa'
- See Also:
- Serialized Form
-
Method Summary
Modifier and Type Method Description CLI
addCommand(java.lang.String name, Command command)
Adds a command to theCLI
.static CLI
create()
Creates a newCLI
object with a null application title and a null command prompt.static CLI
create(java.lang.String title)
Creates a newCLI
object with a null application title and a null command prompt.static CLI
create(java.lang.String title, java.lang.String commandPrompt)
Creates a newCLI
object with the given application title and command prompt.void
execute()
Starts executing theCLI
, as theexecute(PrintStream, InputStream)
method does, but on the standardI
/O
.void
execute(java.io.PrintStream output, java.io.InputStream input)
Starts executing theCLI
until the current thread is interrupted.java.lang.String
getCommandPrompt()
Gets the current command prompt that is shown just before theCLI
starts to listen for the next command.java.lang.String
getTitle()
Gets the current title.void
printHelp(java.io.PrintStream output)
Lists the available commands of thisCLI
on the given out stream.CLI
removeCommand(java.lang.String name)
Removes a command to theCLI
.CLI
setCommandPrompt(java.lang.String commandPrompt)
Sets a new command prompt to show just before theCLI
starts to listen for the next command.CLI
setTitle(java.lang.String title)
Sets theCLI
title (e.g. the application name and the version).
-
Method Details
-
create
Creates a newCLI
object with the given application title and command prompt.- Parameters:
title
- the title to show when theCLI(String, String)
starts (e.g. the application name and the version)commandPrompt
- the prompt to show before starting to listen for commands (e.g. the current directory path)- Returns:
- the new
CLI
object
-
create
Creates a newCLI
object with a null application title and a null command prompt.- Returns:
- the new
CLI
object
-
create
Creates a newCLI
object with a null application title and a null command prompt.- Parameters:
title
- the title to show when theCLI(String, String)
starts (e.g. the application name and the version)- Returns:
- the new
CLI
object
-
addCommand
Adds a command to theCLI
. -
removeCommand
Removes a command to theCLI
.- Parameters:
name
- the name of the command to remove- Returns:
- this same
CLI
object
-
setCommandPrompt
Sets a new command prompt to show just before theCLI
starts to listen for the next command.- Parameters:
commandPrompt
- the new command prompt- Returns:
- this same
CLI
object
-
getCommandPrompt
public java.lang.String getCommandPrompt()Gets the current command prompt that is shown just before theCLI
starts to listen for the next command.- Returns:
- the current command prompt as string
-
setTitle
Sets theCLI
title (e.g. the application name and the version). -
getTitle
public java.lang.String getTitle()Gets the current title.- Returns:
- the current title as string
-
printHelp
public void printHelp(java.io.PrintStream output)Lists the available commands of thisCLI
on the given out stream.- Parameters:
output
- the out stream on which the list of command must be wrote.
-
execute
public void execute()Starts executing theCLI
, as theexecute(PrintStream, InputStream)
method does, but on the standardI
/O
.
Seeexecute(PrintStream, InputStream)
for more details.- See Also:
execute(PrintStream, InputStream)
-
execute
public void execute(java.io.PrintStream output, java.io.InputStream input)Starts executing theCLI
until the current thread is interrupted. When this method is invoked, theCLI
starts writing the title on the specified out stream. Then theCLI
writes a short message to show to the user how to list the available commands. Finally theCLI
writes the command prompt on the out stream and starts listening for new commands on the given in stream.- Parameters:
output
- the out stream on which the user can see CLI messages (can be the standard outSystem.out
)input
- the in stream on which the user writes the commands (can be the standard inSystem.in
)
-