diff options
| author | bculkin2442 <bjculkin@mix.wvu.edu> | 2017-02-26 09:38:26 -0500 |
|---|---|---|
| committer | bculkin2442 <bjculkin@mix.wvu.edu> | 2017-02-26 09:40:25 -0500 |
| commit | 0b4fd83d36eb82c92a39af8b8e1c521d822bd4c3 (patch) | |
| tree | 79bdddd59f526451411362559b213bdb8485406a /BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommandMode.java | |
| parent | e8567df90f38e5f2e15fbb245aa4ac45d37a3003 (diff) | |
Formatting
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommandMode.java')
| -rw-r--r-- | BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommandMode.java | 190 |
1 files changed, 86 insertions, 104 deletions
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()); } } |
