summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/cli
diff options
context:
space:
mode:
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/cli')
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/cli/DelegatingCommand.java29
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommand.java28
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommandMode.java136
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;