diff options
| author | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-04-17 15:01:44 -0400 |
|---|---|---|
| committer | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-04-17 15:01:44 -0400 |
| commit | 77fcc58d1facffbc3af50be8c05985350e9f1355 (patch) | |
| tree | b7b81d24c107e644924dc526f8bb034efc62d2dc /BJC-Utils2/src/main/java/bjc/utils/cli | |
| parent | a5850915df72f5968fd1b281eb9e455d50c580ee (diff) | |
Code maintenace and changes
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/cli')
3 files changed, 107 insertions, 86 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/cli/DelegatingCommand.java b/BJC-Utils2/src/main/java/bjc/utils/cli/DelegatingCommand.java new file mode 100644 index 0000000..76794cf --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/cli/DelegatingCommand.java @@ -0,0 +1,29 @@ +package bjc.utils.cli; + +class DelegatingCommand implements ICommand { + private ICommand delegate; + + public DelegatingCommand(ICommand delegate) { + this.delegate = delegate; + } + + @Override + public ICommandHandler getHandler() { + return delegate.getHandler(); + } + + @Override + public ICommandHelp getHelp() { + return delegate.getHelp(); + } + + @Override + public ICommand createAlias() { + return new DelegatingCommand(delegate); + } + + @Override + 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 658a299..3c0aef6 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommand.java +++ b/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommand.java @@ -7,34 +7,6 @@ package bjc.utils.cli; * */ public class GenericCommand implements ICommand { - private static class DelegatingCommand implements ICommand { - private ICommand delegate; - - public DelegatingCommand(ICommand delegate) { - this.delegate = delegate; - } - - @Override - public ICommandHandler getHandler() { - return delegate.getHandler(); - } - - @Override - public ICommandHelp getHelp() { - return delegate.getHelp(); - } - - @Override - public ICommand createAlias() { - return new DelegatingCommand(delegate); - } - - @Override - public boolean isAlias() { - return true; - } - } - private ICommandHandler handler; private ICommandHelp help; 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 c52bb08..84b4d39 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommandMode.java +++ b/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommandMode.java @@ -49,64 +49,7 @@ public class GenericCommandMode implements ICommandMode { defaultHandlers = new FunctionalMap<>(new TreeMap<>()); helpTopics = new FunctionalMap<>(new TreeMap<>()); - setupDefaultCommands(errorOutput); - } - - private void setupDefaultCommands(Consumer<String> errorOutput) { - defaultHandlers.put("list", new GenericCommand((args) -> { - listCommands(); - - 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.")); - - defaultHandlers.put("alias", new GenericCommand((args) -> { - aliasCommands(args); - - return this; - }, "alias\tAlias one command to another", - "alias gives a command another name it can be invoked by. It is invoked" - + " with two arguments, the name of the command to alias" - + ", and the alias to give that command.")); - - defaultHandlers.put("help", new GenericCommand((args) -> { - if (args == null || args.length == 0) { - // Invoke general help - helpSummary(); - } else { - // Invoke help for a command - helpCommand(args[0]); - } - - return this; - }, "help\tConsult the help system", - "help consults the internal help system." - + " It can be invoked in two ways. Invoking it with no arguments" - + " causes it to print out all the topics you can ask for details on," - + " while invoking it with the name of a topic will print the entry" - + " for that topic")); - - addCommandAlias("help", "man"); - - // Add commands handled in a upper layer - defaultHandlers.put("clear", new GenericCommand((args) -> { - errorOutput.accept( - "ERROR: This console doesn't support screen clearing"); - - return this; - }, "clear\tClear the screen", - "clear clears the screen of all the text on it," - + " and prepares a fresh prompt.")); - - defaultHandlers.put("exit", new GenericCommand((args) -> { - errorOutput.accept( - "ERROR: This console doesn't support auto-exiting"); - - return this; - }, "exit\tExit the game", - "exit first prompts the user to make sure they want to exit," - + " and if they affirm it, it quits")); + setupDefaultCommands(); } /** @@ -197,6 +140,68 @@ public class GenericCommandMode implements ICommandMode { } } + private GenericCommand buildAliasCommand() { + return new GenericCommand((args) -> { + aliasCommands(args); + + return this; + }, "alias\tAlias one command to another", + "alias gives a command another name it can be invoked by. It is invoked" + + " with two arguments, the name of the command to alias" + + ", and the alias to give that command."); + } + + private GenericCommand buildClearCommands() { + return new GenericCommand((args) -> { + errorOutput.accept( + "ERROR: This console doesn't support screen clearing"); + + return this; + }, "clear\tClear the screen", + "clear clears the screen of all the text on it," + + " and prepares a fresh prompt."); + } + + private GenericCommand buildExitCommand() { + return new GenericCommand((args) -> { + errorOutput.accept( + "ERROR: This console doesn't support auto-exiting"); + + return this; + }, "exit\tExit the game", + "exit first prompts the user to make sure they want to exit," + + " and if they affirm it, it quits"); + } + + private GenericCommand buildHelpCommand() { + return new GenericCommand((args) -> { + if (args == null || args.length == 0) { + // Invoke general help + helpSummary(); + } else { + // Invoke help for a command + helpCommand(args[0]); + } + + return this; + }, "help\tConsult the help system", + "help consults the internal help system." + + " It can be invoked in two ways. Invoking it with no arguments" + + " causes it to print out all the topics you can ask for details on," + + " while invoking it with the name of a topic will print the entry" + + " for that topic"); + } + + private GenericCommand buildListCommand() { + return new GenericCommand((args) -> { + listCommands(); + + 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."); + } + @Override public boolean canHandleCommand(String command) { return commandHandlers.containsKey(command) @@ -359,6 +364,21 @@ public class GenericCommandMode implements ICommandMode { unknownCommandHandler = handler; } + private void setupDefaultCommands() { + defaultHandlers.put("list", buildListCommand()); + + defaultHandlers.put("alias", buildAliasCommand()); + + defaultHandlers.put("help", buildHelpCommand()); + + addCommandAlias("help", "man"); + + // Add commands handled in a upper layer + defaultHandlers.put("clear", buildClearCommands()); + + defaultHandlers.put("exit", buildExitCommand()); + } + @Override public boolean useCustomPrompt() { return customPrompt != null; |
