java.lang.Object
fibrous.fi.FiCLI
Parses text input, and executes commands accordingly.
Add
FiCommand
objects via the addCommand() method.
Will list the available commands and their configured descriptions when the listCommandsString is input.
The listCommandsString is configurable via an appropriate constructor.
Output from this class can be directed to anything by creating an object that implements FiOutputStream
and passing it through the appropriate constructor.- Author:
- noahm
-
Constructor Summary
ConstructorDescriptionFiCLI()
Creates a new InputParser using the defaultFiConsoleIO
, and a default listCommndsString of '?'.FiCLI
(FiInputStream is, FiOutputStream os, String listCommandsString) Creates a new InputParser with a configurableFiOutputStream
andFiInputStream
.Creates a new InputParser using the defaultFiConsoleIO
. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addCommand
(FiCommand command) Adds a command that will be executed when input matches it's defined syntax.void
allowAdditionalStates
(boolean exit, boolean back) Determines if the processCommand method can return with FiState.EXIT or BACK.void
Removes all commands.getCaret()
Returns the currently configured caret.Returns an ArrayList containing all of the currently configured commands.int
Returns the number of currently configured commands.Outputs the caret, indicating that input is requested, and then waits for input and reacts accordingly.void
setBackDescription
(String desc) Sets the description that the back command will show (when FiState.BACK is allowed to be returned.)void
Determines what String will returnFiState
.BACK when doCommand() is called.void
Sets the caret.void
setCommands
(ArrayList<FiCommand> commands) void
setExitDescription
(String desc) Sets the description that the exit command will show (when FiState.EXIT is allowed to be returned.)void
Determines what String will returnFiState
.EXIT when doCommand() is called.void
setInput
(FiInputStream fis) Sets the input system.void
setOutput
(FiOutputStream fos) Sets the output system.
-
Constructor Details
-
FiCLI
Creates a new InputParser with a configurableFiOutputStream
andFiInputStream
.- Parameters:
os
- TheFiOutputStream
object that this parser will use to directly output data.listCommandsString
- What input string should show all of the commands that have been configured.
-
FiCLI
Creates a new InputParser using the defaultFiConsoleIO
.- Parameters:
listCommandsString
- What input string should show all of the commands that have been configured.
-
FiCLI
public FiCLI()Creates a new InputParser using the defaultFiConsoleIO
, and a default listCommndsString of '?'.
-
-
Method Details
-
addCommand
Adds a command that will be executed when input matches it's defined syntax.- Parameters:
command
-
-
setCommands
- Parameters:
commands
-
-
getCommands
Returns an ArrayList containing all of the currently configured commands.- Returns:
-
clearCommands
public void clearCommands()Removes all commands. -
setExitString
Determines what String will returnFiState
.EXIT when doCommand() is called. This may be synonymous with the FiState.BACK state in certain contexts. In general, this state is used to determine if the application should exit. Using this method also automatically allows FiState.EXIT to be returned.- Parameters:
s
-
-
setBackString
Determines what String will returnFiState
.BACK when doCommand() is called. This may be synonymous with the FiState.EXIT state in certain contexts and configurations. In general, this state is used to determine if this parser should close, and not the application itself. It is useful in menu systems where nested parsers are implemented, and you still want to allow exiting from the application, regardless of where in the CLI the user is. Using this method also automatically allows FiState.BACK to be returned.- Parameters:
s
-
-
allowAdditionalStates
public void allowAdditionalStates(boolean exit, boolean back) Determines if the processCommand method can return with FiState.EXIT or BACK.- Parameters:
exit
-back
-
-
setExitDescription
Sets the description that the exit command will show (when FiState.EXIT is allowed to be returned.)- Parameters:
desc
-
-
setBackDescription
Sets the description that the back command will show (when FiState.BACK is allowed to be returned.)- Parameters:
desc
-
-
setCaret
Sets the caret.- Parameters:
c
-
-
getCaret
Returns the currently configured caret.- Returns:
-
setInput
Sets the input system.- Parameters:
fis
-
-
setOutput
Sets the output system.- Parameters:
fos
-
-
processCommand
Outputs the caret, indicating that input is requested, and then waits for input and reacts accordingly. Checks a string that is passed and attempts to match it against a stored command or other special command. If the string is a valid command, it executes the command, and returns true. If not, nothing happens and it returns false, which allows you to create your own error handling.- Returns:
-
numOfCommands
public int numOfCommands()Returns the number of currently configured commands.- Returns:
-