diff options
Diffstat (limited to 'BJC-Utils2/src/main/java')
7 files changed, 107 insertions, 138 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 179eec0..bc3e56e 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/cli/CLICommander.java +++ b/BJC-Utils2/src/main/java/bjc/utils/cli/CLICommander.java @@ -16,14 +16,14 @@ public class CLICommander { /* * The streams used for input and normal/error output */ - private InputStream input; + private InputStream input; private OutputStream output; private OutputStream error; /* * The command mode to start execution in */ - private ICommandMode initialMode; + private ICommandMode initialMode; /** * Create a new CLI interface powered by streams. @@ -35,17 +35,13 @@ public class CLICommander { * @param error * The stream to send error output to. */ - public CLICommander(InputStream input, OutputStream output, - OutputStream error) { + public CLICommander(InputStream input, OutputStream output, OutputStream error) { if (input == null) { - throw new NullPointerException( - "Input stream must not be null"); + throw new NullPointerException("Input stream must not be null"); } else if (output == null) { - throw new NullPointerException( - "Output stream must not be null"); + throw new NullPointerException("Output stream must not be null"); } else if (error == null) { - throw new NullPointerException( - "Error stream must not be null"); + throw new NullPointerException("Error stream must not be null"); } this.input = input; @@ -95,21 +91,17 @@ public class CLICommander { // 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); + commandArgs = Arrays.copyOfRange(commandTokens, 1, commandTokens.length); } // Process command - currentMode = currentMode.process(commandTokens[0], - commandArgs); + currentMode = currentMode. process(commandTokens[0], commandArgs); } else { - errorOutput.print( - "Error: Unrecognized command " + currentLine); + errorOutput.print("Error: Unrecognized command " + currentLine); } } @@ -124,10 +116,9 @@ public class CLICommander { */ public void setInitialCommandMode(ICommandMode initialMode) { if (initialMode == null) { - throw new NullPointerException( - "Initial mode must be non-zero"); + throw new NullPointerException("Initial mode must be non-zero"); } this.initialMode = initialMode; } -}
\ No newline at end of file +} 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 ad115d7..aa0a308 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/cli/DelegatingCommand.java +++ b/BJC-Utils2/src/main/java/bjc/utils/cli/DelegatingCommand.java @@ -39,4 +39,4 @@ class DelegatingCommand implements ICommand { public boolean isAlias() { return true; } -}
\ No newline at end of file +} diff --git a/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommand.java b/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommand.java index 7a38e9b..d6a72c9 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommand.java +++ b/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommand.java @@ -9,8 +9,9 @@ package bjc.utils.cli; public class GenericCommand implements ICommand { // The behavior for invoking the command private ICommandHandler handler; + // The help for the command - private ICommandHelp help; + private ICommandHelp help; /** * Create a new generic command @@ -22,11 +23,9 @@ public class GenericCommand implements ICommand { * @param help * The detailed help message for the command. May be null */ - public GenericCommand(ICommandHandler handler, String description, - String help) { + public GenericCommand(ICommandHandler handler, String description, String help) { if (handler == null) { - throw new NullPointerException( - "Command handler must not be null"); + throw new NullPointerException("Command handler must not be null"); } this.handler = handler; 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 b6c6ea4..b3847c0 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommandMode.java +++ b/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommandMode.java @@ -21,24 +21,24 @@ public class GenericCommandMode implements ICommandMode { /* * Contains the commands this mode handles */ - private IMap<String, ICommand> commandHandlers; - private IMap<String, ICommand> defaultHandlers; + private IMap<String, ICommand> commandHandlers; + private IMap<String, ICommand> defaultHandlers; // Contains help topics without an associated command - private IMap<String, ICommandHelp> helpTopics; + private IMap<String, ICommandHelp> helpTopics; // The action to execute upon encountering an unknown command - private BiConsumer<String, String[]> unknownCommandHandler; + private BiConsumer<String, String[]> unknownCommandHandler; // The functions to use for input/output - private Consumer<String> errorOutput; - private Consumer<String> normalOutput; + private Consumer<String> errorOutput; + private Consumer<String> normalOutput; // The name of this command mode, or null if it is unnamed - private String modeName; + private String modeName; // The custom prompt to use, or null if none is specified - private String customPrompt; + private String customPrompt; /** * Create a new generic command mode @@ -48,14 +48,11 @@ public class GenericCommandMode implements ICommandMode { * @param errorOutput * The function to use for error output */ - public GenericCommandMode(Consumer<String> normalOutput, - Consumer<String> errorOutput) { + public GenericCommandMode(Consumer<String> normalOutput, Consumer<String> errorOutput) { if (normalOutput == null) { - throw new NullPointerException( - "Normal output source must be non-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"); + throw new NullPointerException("Error output source must be non-null"); } this.normalOutput = normalOutput; @@ -64,7 +61,7 @@ public class GenericCommandMode implements ICommandMode { // 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(); } @@ -83,15 +80,13 @@ public class GenericCommandMode implements ICommandMode { */ public void addCommandAlias(String commandName, String aliasName) { if (commandName == null) { - throw new NullPointerException( - "Command name must not be null"); + throw new NullPointerException("Command name must not be null"); } else if (aliasName == null) { throw new NullPointerException("Alias name must not be null"); } else if (!commandHandlers.containsKey(commandName) && !defaultHandlers.containsKey(commandName)) { - throw new IllegalArgumentException( - "Cannot alias non-existant command '" + commandName - + "'"); + throw new IllegalArgumentException("Cannot alias non-existant command '" + + commandName + "'"); } else if (commandHandlers.containsKey(aliasName) || defaultHandlers.containsKey(aliasName)) { throw new IllegalArgumentException("Cannot bind alias '" @@ -100,11 +95,9 @@ public class GenericCommandMode implements ICommandMode { ICommand aliasedCommand; if (defaultHandlers.containsKey(commandName)) { - aliasedCommand = defaultHandlers.get(commandName) - .aliased(); + aliasedCommand = defaultHandlers.get(commandName).aliased(); } else { - aliasedCommand = commandHandlers.get(commandName) - .aliased(); + aliasedCommand = commandHandlers.get(commandName).aliased(); } commandHandlers.put(aliasName, aliasedCommand); @@ -129,8 +122,8 @@ public class GenericCommandMode implements ICommandMode { } else if (handler == null) { throw new NullPointerException("Handler must not be null"); } else if (canHandle(command)) { - throw new IllegalArgumentException("Command " + command - + " already has a handler registered"); + throw new IllegalArgumentException("Command " + + command + " already has a handler registered"); } else { commandHandlers.put(command, handler); } @@ -148,46 +141,55 @@ public class GenericCommandMode implements ICommandMode { helpTopics.put(topicName, topic); } - // ------------------------------------------------------------------------- /* * Default command builders */ - // ------------------------------------------------------------------------- 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."; + return new GenericCommand((args) -> { doAliasCommands(args); return this; - }, "alias\tAlias one command to another", - "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."); + }, aliasShortHelp, aliasLongHelp); } private GenericCommand buildClearCommands() { + String clearShortHelp = "clear\tClear the screen"; + String clearLongHelp = "Clears the screen of all the text on it," + + " and prints a new prompt."; + return new GenericCommand((args) -> { - errorOutput.accept( - "ERROR: This console doesn't support screen clearing"); + errorOutput.accept("ERROR: This console doesn't support screen clearing"); return this; - }, "clear\tClear the screen", - "Clears the screen of all the text on it," - + " and prints a new prompt."); + }, clearShortHelp, clearLongHelp); } 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"; + return new GenericCommand((args) -> { - errorOutput.accept( - "ERROR: This console doesn't support auto-exiting"); + errorOutput.accept("ERROR: This console doesn't support auto-exiting"); return this; - }, "exit\tExit the console", - "First prompts the user to make sure they want to exit," - + " and if they affirm it, it immediately exits"); + }, exitShortHelp, exitLongHelp); } 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"; + return new GenericCommand((args) -> { if (args == null || args.length == 0) { // Invoke general help @@ -198,47 +200,40 @@ public class GenericCommandMode implements ICommandMode { } return this; - }, "help\tConsult the help system", - "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"); + }, helpShortHelp, helpLongHelp); } 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"; + return new GenericCommand((args) -> { doListCommands(); return this; - }, "list\tList available command", - "Lists all of the commands available in this mode," - + " as well as the commands that are valid in any mode."); + }, listShortHelp, listLongHelp); } @Override public boolean canHandle(String command) { - return commandHandlers.containsKey(command) - || defaultHandlers.containsKey(command); + return commandHandlers.containsKey(command) || defaultHandlers.containsKey(command); } - // ------------------------------------------------------------------------- /* * Implement default commands */ - // ------------------------------------------------------------------------- private void doAliasCommands(String[] args) { if (args.length != 2) { - errorOutput.accept("ERROR: Alias requires two arguments. " - + "The command name, and the alias for that command"); + errorOutput.accept("ERROR: Alias requires two arguments." + + " The command name, and the alias for that command"); } else { String commandName = args[0]; String aliasName = args[1]; if (!canHandle(commandName)) { - errorOutput.accept("ERROR: '" + commandName - + "' is not a valid command."); + errorOutput.accept("ERROR: '" + commandName + "' is not a valid command."); } else if (canHandle(aliasName)) { errorOutput.accept("ERROR: Cannot overwrite command '" + aliasName + "'"); @@ -250,52 +245,48 @@ public class GenericCommandMode implements ICommandMode { private void doHelpCommand(String commandName) { if (commandHandlers.containsKey(commandName)) { - normalOutput.accept("\n" + commandHandlers.get(commandName) - .getHelp().getDescription()); + String desc = commandHandlers.get(commandName).getHelp().getDescription(); + + normalOutput.accept("\n" + desc); } else if (defaultHandlers.containsKey(commandName)) { - normalOutput.accept("\n" + defaultHandlers.get(commandName) - .getHelp().getDescription()); + String desc = defaultHandlers.get(commandName).getHelp().getDescription(); + + normalOutput.accept("\n" + desc); } else if (helpTopics.containsKey(commandName)) { - normalOutput.accept( - "\n" + helpTopics.get(commandName).getDescription()); + normalOutput.accept("\n" + helpTopics.get(commandName).getDescription()); } else { - errorOutput - .accept("ERROR: I'm sorry, but there is no help available for '" - + commandName + "'"); + errorOutput.accept("ERROR: I'm sorry, but there is no help available for '" + + commandName + "'"); } } private void doHelpSummary() { - normalOutput.accept( - "Help topics for this command mode are as follows:\n"); + normalOutput.accept("Help topics for this command mode are as follows:\n"); + if (commandHandlers.getSize() > 0) { - commandHandlers.forEachValue((command) -> { + commandHandlers.forEachValue(command -> { if (!command.isAlias()) { - normalOutput.accept( - "\t" + command.getHelp().getSummary() + "\n"); + normalOutput.accept("\t" + command.getHelp().getSummary() + "\n"); } }); } else { normalOutput.accept("\tNone available\n"); } - normalOutput.accept( - "\nHelp topics available in all command modes are as follows\n"); + normalOutput.accept("\nHelp topics available in all command modes are as follows\n"); if (defaultHandlers.getSize() > 0) { - defaultHandlers.forEachValue((command) -> { + defaultHandlers.forEachValue(command -> { if (!command.isAlias()) { - normalOutput.accept( - "\t" + command.getHelp().getSummary() + "\n"); + normalOutput.accept("\t" + command.getHelp().getSummary() + "\n"); } }); } else { normalOutput.accept("\tNone available\n"); } - normalOutput.accept( - "\nHelp topics not associated with a command are as follows\n"); + normalOutput.accept("\nHelp topics not associated with a command are as follows\n"); if (helpTopics.getSize() > 0) { - helpTopics.forEachValue((topic) -> { + helpTopics.forEachValue(topic -> { normalOutput.accept("\t" + topic.getSummary() + "\n"); }); } else { @@ -304,17 +295,14 @@ public class GenericCommandMode implements ICommandMode { } private void doListCommands() { - normalOutput.accept( - "The available commands for this mode are as follows:\n"); + normalOutput.accept("The available commands for this mode are as follows:\n"); - commandHandlers.keyList().forEach((commandName) -> { + commandHandlers.keyList().forEach(commandName -> { normalOutput.accept("\t" + commandName); }); - normalOutput.accept( - "\nThe following commands are available in all modes:\n"); - - defaultHandlers.keyList().forEach((commandName) -> { + normalOutput.accept( "\nThe following commands are available in all modes:\n"); + defaultHandlers.keyList().forEach(commandName -> { normalOutput.accept("\t" + commandName); }); @@ -354,16 +342,14 @@ public class GenericCommandMode implements ICommandMode { return commandHandlers.get(command).getHandler().handle(args); } else { if (args != null) { - errorOutput.accept("ERROR: Unrecognized command " + command - + String.join(" ", args)); + errorOutput.accept("ERROR: Unrecognized command " + + command + String.join(" ", args)); } else { - errorOutput - .accept("ERROR: Unrecognized command " + command); + errorOutput.accept("ERROR: Unrecognized command " + command); } if (unknownCommandHandler == null) { - throw new UnsupportedOperationException( - "Command " + command + " is invalid."); + throw new UnsupportedOperationException("Command " + command + " is invalid."); } unknownCommandHandler.accept(command, args); @@ -379,7 +365,7 @@ public class GenericCommandMode implements ICommandMode { * The custom prompt for this mode, or null to disable the * custom prompt */ - public void setCustomPrompt( String prompt) { + public void setCustomPrompt(String prompt) { customPrompt = prompt; } @@ -390,7 +376,7 @@ public class GenericCommandMode implements ICommandMode { * The desired name of this mode, or null to use the default * name */ - public void setModeName( String name) { + public void setModeName(String name) { modeName = name; } @@ -401,8 +387,7 @@ public class GenericCommandMode implements ICommandMode { * The handler to use for unknown commands, or null to throw * on unknown commands */ - public void setUnknownCommandHandler( - BiConsumer<String, String[]> handler) { + public void setUnknownCommandHandler(BiConsumer<String, String[]> handler) { if (handler == null) { throw new NullPointerException("Handler must not be null"); } @@ -411,20 +396,17 @@ public class GenericCommandMode implements ICommandMode { } private void setupDefaultCommands() { - defaultHandlers.put("list", buildListCommand()); - + defaultHandlers.put("list", buildListCommand()); defaultHandlers.put("alias", buildAliasCommand()); - - defaultHandlers.put("help", buildHelpCommand()); + defaultHandlers.put("help", buildHelpCommand()); addCommandAlias("help", "man"); // Add commands handled in a upper layer. - // TODO figure out a place to put commands that apply across all + // @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()); } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/cli/GenericHelp.java b/BJC-Utils2/src/main/java/bjc/utils/cli/GenericHelp.java index f4095f3..fc7cf20 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/cli/GenericHelp.java +++ b/BJC-Utils2/src/main/java/bjc/utils/cli/GenericHelp.java @@ -8,8 +8,8 @@ package bjc.utils.cli; */ public class GenericHelp implements ICommandHelp { // The strings for this help topic - private String summary; - private String description; + private String summary; + private String description; /** * Create a new help topic @@ -22,8 +22,7 @@ public class GenericHelp implements ICommandHelp { */ public GenericHelp(String summary, String description) { if (summary == null) { - throw new NullPointerException( - "Help summary must be non-null"); + throw new NullPointerException("Help summary must be non-null"); } this.summary = summary; diff --git a/BJC-Utils2/src/main/java/bjc/utils/cli/ICommandMode.java b/BJC-Utils2/src/main/java/bjc/utils/cli/ICommandMode.java index 8583b80..00b83ed 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/cli/ICommandMode.java +++ b/BJC-Utils2/src/main/java/bjc/utils/cli/ICommandMode.java @@ -29,8 +29,7 @@ public interface ICommandMode { * if this mode doesn't support a custom prompt */ public default String getCustomPrompt() { - throw new UnsupportedOperationException( - "This mode doesn't support a custom prompt"); + throw new UnsupportedOperationException("This mode doesn't support a custom prompt"); } /** diff --git a/BJC-Utils2/src/main/java/bjc/utils/cli/NullHelp.java b/BJC-Utils2/src/main/java/bjc/utils/cli/NullHelp.java index e150ba6..3e016e2 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/cli/NullHelp.java +++ b/BJC-Utils2/src/main/java/bjc/utils/cli/NullHelp.java @@ -7,7 +7,6 @@ package bjc.utils.cli; * */ public class NullHelp implements ICommandHelp { - @Override public String getDescription() { return "No description provided"; @@ -17,4 +16,4 @@ public class NullHelp implements ICommandHelp { public String getSummary() { return "No summary provided"; } -}
\ No newline at end of file +} |
