summaryrefslogtreecommitdiff
path: root/base/src/main/java/bjc/utils/cli/CLICommander.java
diff options
context:
space:
mode:
Diffstat (limited to 'base/src/main/java/bjc/utils/cli/CLICommander.java')
-rw-r--r--base/src/main/java/bjc/utils/cli/CLICommander.java44
1 files changed, 23 insertions, 21 deletions
diff --git a/base/src/main/java/bjc/utils/cli/CLICommander.java b/base/src/main/java/bjc/utils/cli/CLICommander.java
index ad3bea9..f27454e 100644
--- a/base/src/main/java/bjc/utils/cli/CLICommander.java
+++ b/base/src/main/java/bjc/utils/cli/CLICommander.java
@@ -24,20 +24,22 @@ public class CLICommander {
* Create a new CLI interface powered by streams.
*
* @param input
- * The stream to get user input from.
+ * The stream to get user input from.
*
* @param output
- * The stream to send normal output to.
+ * The stream to send normal output to.
*
* @param error
- * The stream to send error output to.
+ * The stream to send error output to.
*/
- public CLICommander(final InputStream input, final OutputStream output, final OutputStream error) {
- if(input == null)
+ public CLICommander(final InputStream input, final OutputStream output,
+ final OutputStream error) {
+ if (input == null)
throw new NullPointerException("Input stream must not be null");
- else if(output == 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");
+ else if (error == null)
+ throw new NullPointerException("Error stream must not be null");
this.input = input;
this.output = output;
@@ -53,9 +55,8 @@ public class CLICommander {
/*
* Set up input streams.
*
- * We're suppressing the warning about a potentially leaked
- * resource because we might use the input stream multiple
- * times.
+ * We're suppressing the warning about a potentially leaked resource because we
+ * might use the input stream multiple times.
*/
@SuppressWarnings("resource")
final Scanner inputSource = new Scanner(input);
@@ -63,24 +64,23 @@ public class CLICommander {
/*
* The mode currently being used to handle commands.
*
- * Used to preserve the initial mode, so that a mode can be
- * invoked more than once.
+ * Used to preserve the initial mode, so that a mode can be invoked more than
+ * once.
*/
CommandMode currentMode = initialMode;
/* The number of the command we are executing. */
int comno = 1;
/*
- * Process commands until we're told to stop, by the mode being
- * set to null.
+ * Process commands until we're told to stop, by the mode being set to null.
*/
- while(currentMode != null) {
+ while (currentMode != null) {
/*
* Print out the command prompt.
*
* Use a custom prompt if one is specified.
*/
- if(currentMode.isCustomPromptEnabled()) {
+ if (currentMode.isCustomPromptEnabled()) {
normalOutput.print(currentMode.getCustomPrompt());
} else {
normalOutput.printf("%s (%d)>> ", currentMode.getName(), comno);
@@ -92,21 +92,22 @@ public class CLICommander {
final String currentLine = inputSource.nextLine();
/* Handle commands we can handle in this mode. */
- if(currentMode.canHandle(currentLine)) {
+ if (currentMode.canHandle(currentLine)) {
final String[] commandTokens = currentLine.split(" ");
String[] commandArgs = null;
final int argCount = commandTokens.length;
/* Parse args if they are present. */
- if(argCount > 1) {
+ if (argCount > 1) {
commandArgs = Arrays.copyOfRange(commandTokens, 1, argCount);
}
/* Process command. */
currentMode = currentMode.process(commandTokens[0], commandArgs);
} else {
- errorOutput.printf("Error: Unrecognized command '%s' (no. %d)\n", currentLine, comno);
+ errorOutput.printf("Error: Unrecognized command '%s' (no. %d)\n",
+ currentLine, comno);
}
}
@@ -117,10 +118,11 @@ public class CLICommander {
* Set the initial command mode to use.
*
* @param initialMode
- * The initial command mode to use.
+ * The initial command mode to use.
*/
public void setInitialCommandMode(final CommandMode initialMode) {
- if(initialMode == null) throw new NullPointerException("Initial mode must be non-null");
+ if (initialMode == null)
+ throw new NullPointerException("Initial mode must be non-null");
this.initialMode = initialMode;
}