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 the CLI.
    static CLI create()
    Creates a new CLI object with a null application title and a null command prompt.
    static CLI create​(java.lang.String title)
    Creates a new CLI object with a null application title and a null command prompt.
    static CLI create​(java.lang.String title, java.lang.String commandPrompt)
    Creates a new CLI object with the given application title and command prompt.
    void execute()
    Starts executing the CLI, as the execute(PrintStream, InputStream) method does, but on the standard I/O.
    void execute​(java.io.PrintStream output, java.io.InputStream input)
    Starts executing the CLI until the current thread is interrupted.
    java.lang.String getCommandPrompt()
    Gets the current command prompt that is shown just before the CLI 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 this CLI on the given out stream.
    CLI removeCommand​(java.lang.String name)
    Removes a command to the CLI.
    CLI setCommandPrompt​(java.lang.String commandPrompt)
    Sets a new command prompt to show just before the CLI starts to listen for the next command.
    CLI setTitle​(java.lang.String title)
    Sets the CLI title (e.g. the application name and the version).

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • create

      public static CLI create​(java.lang.String title, java.lang.String commandPrompt)
      Creates a new CLI object with the given application title and command prompt.
      Parameters:
      title - the title to show when the CLI(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

      public static CLI create()
      Creates a new CLI object with a null application title and a null command prompt.
      Returns:
      the new CLI object
    • create

      public static CLI create​(java.lang.String title)
      Creates a new CLI object with a null application title and a null command prompt.
      Parameters:
      title - the title to show when the CLI(String, String) starts (e.g. the application name and the version)
      Returns:
      the new CLI object
    • addCommand

      public CLI addCommand​(java.lang.String name, Command command)
      Adds a command to the CLI.
      Parameters:
      name - the name used to invoke the command
      command - the command to add
      Returns:
      this same CLI object
    • removeCommand

      public CLI removeCommand​(java.lang.String name)
      Removes a command to the CLI.
      Parameters:
      name - the name of the command to remove
      Returns:
      this same CLI object
    • setCommandPrompt

      public CLI setCommandPrompt​(java.lang.String commandPrompt)
      Sets a new command prompt to show just before the CLI 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 the CLI starts to listen for the next command.
      Returns:
      the current command prompt as string
    • setTitle

      public CLI setTitle​(java.lang.String title)
      Sets the CLI title (e.g. the application name and the version).
      Parameters:
      title - the title to show when the CLI starts.
      Returns:
      this same CLI object
    • 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 this CLI 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 the CLI, as the execute(PrintStream, InputStream) method does, but on the standard I/O.

      See execute(PrintStream, InputStream) for more details.
      See Also:
      execute(PrintStream, InputStream)
    • execute

      public void execute​(java.io.PrintStream output, java.io.InputStream input)
      Starts executing the CLI until the current thread is interrupted. When this method is invoked, the CLI starts writing the title on the specified out stream. Then the CLI writes a short message to show to the user how to list the available commands. Finally the CLI 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 out System.out)
      input - the in stream on which the user writes the commands (can be the standard in System.in)