diff options
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/cli')
3 files changed, 93 insertions, 55 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"); diff --git a/BJC-Utils2/src/main/java/bjc/utils/cli/DelegatingCommand.java b/BJC-Utils2/src/main/java/bjc/utils/cli/DelegatingCommand.java index ddad5e2..b6f7a87 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/cli/DelegatingCommand.java +++ b/BJC-Utils2/src/main/java/bjc/utils/cli/DelegatingCommand.java @@ -1,20 +1,22 @@ package bjc.utils.cli; /** - * A class for a command that delegates to another command + * A class for a command that delegates to another command. * * @author ben * */ class DelegatingCommand implements ICommand { - // The command to delegate to + /* + * The command to delegate to. + */ private ICommand delegate; /** - * Create a new command that delegates to another command + * Create a new command that delegates to another command. * * @param delegate - * The command to delegate to + * The command to delegate to. */ public DelegatingCommand(ICommand delegate) { this.delegate = delegate; diff --git a/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommandMode.java b/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommandMode.java index 982cf48..f2dae7d 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommandMode.java +++ b/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommandMode.java @@ -24,20 +24,30 @@ public class GenericCommandMode implements ICommandMode { private IMap<String, ICommand> commandHandlers; private IMap<String, ICommand> defaultHandlers; - // Contains help topics without an associated command + /* + * Contains help topics without an associated command + */ private IMap<String, ICommandHelp> helpTopics; - // The action to execute upon encountering an unknown command + /* + * The action to execute upon encountering an unknown command + */ private BiConsumer<String, String[]> unknownCommandHandler; - // The functions to use for input/output + /* + * The functions to use for input/output + */ private Consumer<String> errorOutput; private Consumer<String> normalOutput; - // The name of this command mode, or null if it is unnamed + /* + * The name of this command mode, or null if it is unnamed + */ private String modeName; - // The custom prompt to use, or null if none is specified + /* + * The custom prompt to use, or null if none is specified + */ private String customPrompt; /** @@ -49,18 +59,21 @@ public class GenericCommandMode implements ICommandMode { * The function to use for error output */ public GenericCommandMode(Consumer<String> normalOutput, Consumer<String> errorOutput) { - if(normalOutput == null) - throw new NullPointerException("Normal output source must be non-null"); + if(normalOutput == null) throw new NullPointerException("Normal output source must be non-null"); else if(errorOutput == null) throw new NullPointerException("Error output source must be non-null"); this.normalOutput = normalOutput; - this.errorOutput = errorOutput; - - // Initialize handler maps so that they sort in alphabetical - // order + this.errorOutput = errorOutput; + + /* + * Initialize handler maps so that they sort in alphabetical + */ + /* + * order + */ commandHandlers = new FunctionalMap<>(new TreeMap<>()); defaultHandlers = new FunctionalMap<>(new TreeMap<>()); - helpTopics = new FunctionalMap<>(new TreeMap<>()); + helpTopics = new FunctionalMap<>(new TreeMap<>()); setupDefaultCommands(); } @@ -144,8 +157,8 @@ public class GenericCommandMode implements ICommandMode { private GenericCommand buildAliasCommand() { String aliasShortHelp = "alias\tAlias one command to another"; String aliasLongHelp = "Gives a command another name it can be invoked by." - + " Invoke with two arguments: the name of the command to alias" - + "followed by the name of the alias to give that command."; + + " Invoke with two arguments: the name of the command to alias" + + "followed by the name of the alias to give that command."; return new GenericCommand((args) -> { doAliasCommands(args); @@ -168,7 +181,7 @@ public class GenericCommandMode implements ICommandMode { private GenericCommand buildExitCommand() { String exitShortHelp = "exit\tExit the console"; String exitLongHelp = "First prompts the user to make sure they want to" - + " exit, then quits if they say they do"; + + " exit, then quits if they say they do"; return new GenericCommand((args) -> { errorOutput.accept("ERROR: This console doesn't support auto-exiting"); @@ -180,16 +193,20 @@ public class GenericCommandMode implements ICommandMode { private GenericCommand buildHelpCommand() { String helpShortHelp = "help\tConsult the help system"; String helpLongHelp = "Consults the internal help system." - + " Invoked in two different ways. Invoking with no arguments" - + " causes all the topics you can ask for details on to be list," - + " while invoking with the name of a topic will print the entry" + " for that topic"; + + " Invoked in two different ways. Invoking with no arguments" + + " causes all the topics you can ask for details on to be list," + + " while invoking with the name of a topic will print the entry" + " for that topic"; return new GenericCommand((args) -> { if(args == null || args.length == 0) { - // Invoke general help + /* + * Invoke general help + */ doHelpSummary(); } else { - // Invoke help for a command + /* + * Invoke help for a command + */ doHelpCommand(args[0]); } @@ -200,7 +217,7 @@ public class GenericCommandMode implements ICommandMode { private GenericCommand buildListCommand() { String listShortHelp = "list\tList available commands"; String listLongHelp = "Lists all of the commands available in this mode," - + " as well as commands available in any mode"; + + " as well as commands available in any mode"; return new GenericCommand((args) -> { doListCommands(); @@ -387,11 +404,19 @@ public class GenericCommandMode implements ICommandMode { addCommandAlias("help", "man"); - // Add commands handled in a upper layer. - - // @TODO figure out a place to put commands that apply across - // all - // modes, but only apply to a specific application + /* + * Add commands handled in a upper layer. + */ + + /* + * @TODO figure out a place to put commands that apply across + */ + /* + * all + */ + /* + * modes, but only apply to a specific application + */ defaultHandlers.put("clear", buildClearCommands()); defaultHandlers.put("exit", buildExitCommand()); } |
