summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/cli/CLICommander.java
diff options
context:
space:
mode:
authorbculkin2442 <bjculkin@mix.wvu.edu>2017-03-14 14:48:56 -0400
committerbculkin2442 <bjculkin@mix.wvu.edu>2017-03-14 14:48:56 -0400
commita1662cac9d27581cffda8e6d1b2ede9fa346724d (patch)
tree35259598045393c4bf11c49c8896001fd0cf8121 /BJC-Utils2/src/main/java/bjc/utils/cli/CLICommander.java
parent4f572b2db312ae4368afd031b50ce3c78a12fc8d (diff)
Formatting
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/cli/CLICommander.java')
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/cli/CLICommander.java61
1 files changed, 36 insertions, 25 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/cli/CLICommander.java b/BJC-Utils2/src/main/java/bjc/utils/cli/CLICommander.java
index b732f01..9fff1ac 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/cli/CLICommander.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/cli/CLICommander.java
@@ -14,14 +14,14 @@ import java.util.Scanner;
*/
public class CLICommander {
/*
- * The streams used for input and normal/error output
+ * The streams used for input and normal/error output.
*/
private InputStream input;
private OutputStream output;
private OutputStream error;
/*
- * The command mode to start execution in
+ * The command mode to start execution in.
*/
private ICommandMode initialMode;
@@ -36,30 +36,30 @@ public class CLICommander {
* The stream to send error output to.
*/
public CLICommander(InputStream input, OutputStream output, OutputStream error) {
- if(input == null)
- throw new NullPointerException("Input stream must not be null");
- else if(output == null)
- throw new NullPointerException("Output stream must not be null");
- else if(error == null) throw new NullPointerException("Error stream must not be null");
+ if(input == null) throw new NullPointerException("Input stream must not be null");
+ else if(output == null) throw new NullPointerException("Output stream must not be null");
+ else if(error == null) throw new NullPointerException("Error stream must not be null");
- this.input = input;
+ this.input = input;
this.output = output;
- this.error = error;
+ this.error = error;
}
/**
* Start handling commands from the given input stream.
*/
public void runCommands() {
- // Setup output streams
+ /*
+ * Setup output streams.
+ */
PrintStream normalOutput = new PrintStream(output);
- PrintStream errorOutput = new PrintStream(error);
+ PrintStream errorOutput = new PrintStream(error);
/*
* Set up input streams.
*
* We're suppressing the warning because we might use the input
- * stream multiple times
+ * stream multiple times.
*/
@SuppressWarnings("resource")
Scanner inputSource = new Scanner(input);
@@ -67,15 +67,17 @@ public class CLICommander {
/*
* The mode currently being used to handle commands.
*
- * Used to preserve the initial mode
+ * Used to preserve the initial mode.
*/
ICommandMode currentMode = initialMode;
- // Process commands until we're told to stop
+ /*
+ * Process commands until we're told to stop.
+ */
while(currentMode != null) {
/*
* Print out the command prompt, using a custom prompt
- * if one is specified
+ * if one is specified.
*/
if(currentMode.isCustomPromptEnabled()) {
normalOutput.print(currentMode.getCustomPrompt());
@@ -83,20 +85,29 @@ public class CLICommander {
normalOutput.print(currentMode.getName() + ">> ");
}
- // Read in a command
+ /*
+ * Read in a command.
+ */
String currentLine = inputSource.nextLine();
- // Handle commands we can handle
+ /*
+ * Handle commands we can handle.
+ */
if(currentMode.canHandle(currentLine)) {
String[] commandTokens = currentLine.split(" ");
- String[] commandArgs = null;
-
- // Parse args if they are present
- if(commandTokens.length > 1) {
- commandArgs = Arrays.copyOfRange(commandTokens, 1, commandTokens.length);
+ String[] commandArgs = null;
+ int argCount = commandTokens.length;
+
+ /*
+ * Parse args if they are present.
+ */
+ if(argCount > 1) {
+ commandArgs = Arrays.copyOfRange(commandTokens, 1, argCount);
}
- // Process command
+ /*
+ * Process command.
+ */
currentMode = currentMode.process(commandTokens[0], commandArgs);
} else {
errorOutput.print("Error: Unrecognized command " + currentLine);
@@ -107,10 +118,10 @@ public class CLICommander {
}
/**
- * Set the initial command mode to use
+ * Set the initial command mode to use.
*
* @param initialMode
- * The initial command mode to use
+ * The initial command mode to use.
*/
public void setInitialCommandMode(ICommandMode initialMode) {
if(initialMode == null) throw new NullPointerException("Initial mode must be non-zero");