diff options
Diffstat (limited to 'base/src/main/java/bjc/utils/cli/CLICommander.java')
| -rw-r--r-- | base/src/main/java/bjc/utils/cli/CLICommander.java | 76 |
1 files changed, 34 insertions, 42 deletions
diff --git a/base/src/main/java/bjc/utils/cli/CLICommander.java b/base/src/main/java/bjc/utils/cli/CLICommander.java index cccb255..1504002 100644 --- a/base/src/main/java/bjc/utils/cli/CLICommander.java +++ b/base/src/main/java/bjc/utils/cli/CLICommander.java @@ -10,19 +10,14 @@ import java.util.Scanner; * Runs a CLI interface from the provided set of streams. * * @author ben - * */ public class CLICommander { - /* - * The streams used for input and normal/error output. - */ + /* The streams used for input and normal/error output. */ private final InputStream input; private final OutputStream output; private final OutputStream error; - /* - * The command mode to start execution in. - */ + /* The command mode to start execution in. */ private CommandMode initialMode; /** @@ -30,38 +25,35 @@ public class CLICommander { * * @param input * The stream to get user input from. + * * @param output * The stream to send normal output to. + * * @param error * The stream to send error output to. */ 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) - 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.output = output; this.error = error; } - /** - * Start handling commands from the given input stream. - */ + /** Start handling commands from the given input stream. */ public void runCommands() { - /* - * Setup output streams. - */ + /* Setup output streams. */ final PrintStream normalOutput = new PrintStream(output); - final PrintStream errorOutput = new PrintStream(error); + final PrintStream errorOutput = new PrintStream(error); /* * Set up input streams. * - * We're suppressing the warning 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); @@ -69,55 +61,55 @@ public class CLICommander { /* * The mode currently being used to handle commands. * - * Used to preserve the initial mode. + * 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. + * Process commands until we're told to stop, by the mode being + * set to null. */ while (currentMode != null) { /* - * Print out the command prompt, using a custom prompt - * if one is specified. + * Print out the command prompt. + * + * Use a custom prompt if one is specified. */ if (currentMode.isCustomPromptEnabled()) { normalOutput.print(currentMode.getCustomPrompt()); } else { - normalOutput.print(currentMode.getName() + ">> "); + normalOutput.printf("%s (%d)>> ", currentMode.getName(), comno); + + comno += 1; } - /* - * Read in a command. - */ + /* Read in a command. */ final String currentLine = inputSource.nextLine(); - /* - * Handle commands we can handle. - */ + /* Handle commands we can handle in this mode. */ if (currentMode.canHandle(currentLine)) { final String[] commandTokens = currentLine.split(" "); - String[] commandArgs = null; + String[] commandArgs = null; final int argCount = commandTokens.length; - /* - * Parse args if they are present. - */ + /* 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); + errorOutput.printf("Error: Unrecognized command '%s' (no. %d)\n", + currentLine, comno); } } - normalOutput.print("Exiting now."); + normalOutput.printf("Exiting now (ran %d commands).\n", comno); } /** @@ -127,7 +119,7 @@ public class CLICommander { * The initial command mode to use. */ public void setInitialCommandMode(final CommandMode initialMode) { - if (initialMode == null) throw new NullPointerException("Initial mode must be non-zero"); + if (initialMode == null) throw new NullPointerException("Initial mode must be non-null"); this.initialMode = initialMode; } |
