diff options
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 | 92 |
1 files changed, 69 insertions, 23 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 f2dae7d..ee2bdbb 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommandMode.java +++ b/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommandMode.java @@ -25,28 +25,28 @@ public class GenericCommandMode implements ICommandMode { 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; @@ -59,21 +59,22 @@ 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; + this.errorOutput = errorOutput; /* - * Initialize handler maps so that they sort in alphabetical + * Initialize handler maps so that they sort in alphabetical */ /* - * order + * order */ commandHandlers = new FunctionalMap<>(new TreeMap<>()); defaultHandlers = new FunctionalMap<>(new TreeMap<>()); - helpTopics = new FunctionalMap<>(new TreeMap<>()); + helpTopics = new FunctionalMap<>(new TreeMap<>()); setupDefaultCommands(); } @@ -157,8 +158,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); @@ -181,7 +182,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"); @@ -193,19 +194,19 @@ 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]); } @@ -217,7 +218,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(); @@ -405,19 +406,64 @@ public class GenericCommandMode implements ICommandMode { addCommandAlias("help", "man"); /* - * Add commands handled in a upper layer. + * Add commands handled in a upper layer. */ /* - * @TODO figure out a place to put commands that apply across + * @TODO figure out a place to put commands that apply across */ /* - * all + * all */ /* - * modes, but only apply to a specific application + * modes, but only apply to a specific application */ defaultHandlers.put("clear", buildClearCommands()); defaultHandlers.put("exit", buildExitCommand()); } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("GenericCommandMode ["); + + if(commandHandlers != null) { + builder.append("commandHandlers="); + builder.append(commandHandlers); + } + + if(defaultHandlers != null) { + builder.append(", "); + builder.append("defaultHandlers="); + builder.append(defaultHandlers); + } + + if(helpTopics != null) { + builder.append(", "); + builder.append("helpTopics="); + builder.append(helpTopics); + } + + if(modeName != null) { + builder.append(", "); + builder.append("modeName="); + builder.append(modeName); + } + + if(customPrompt != null) { + builder.append(", "); + builder.append("customPrompt="); + builder.append(customPrompt); + } + + builder.append("]"); + + return builder.toString(); + } + } |
