diff options
| author | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-04-13 15:41:42 -0400 |
|---|---|---|
| committer | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-04-13 15:41:42 -0400 |
| commit | 9fdf37e3032defc8ea11fd59722b487163381422 (patch) | |
| tree | d3014f1bec5f0d476e0a77ff537ee26853ca148f | |
| parent | 70cea4b406f1cd592c59c4103c1b9b301d3b5907 (diff) | |
Implemented stream-based commands
4 files changed, 118 insertions, 5 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 f48f08e..e816b78 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/cli/CLICommander.java +++ b/BJC-Utils2/src/main/java/bjc/utils/cli/CLICommander.java @@ -2,6 +2,9 @@ package bjc.utils.cli; import java.io.InputStream; import java.io.OutputStream; +import java.io.PrintStream; +import java.util.Arrays; +import java.util.Scanner; /** * Runs a CLI interface from the provided set of streams @@ -13,6 +16,7 @@ public class CLICommander { private InputStream input; private OutputStream output; private OutputStream error; + private ICommandMode initialMode; /** @@ -57,4 +61,48 @@ public class CLICommander { this.initialMode = initialMode; } + + /** + * Run a set of commands through this commander + */ + public void runCommands() { + PrintStream normalOutput = new PrintStream(output); + PrintStream errorOutput = new PrintStream(error); + + @SuppressWarnings("resource") + // We might use this stream multiple times. Don't close it + Scanner inputSource = new Scanner(input); + + ICommandMode currentMode = initialMode; + + while (currentMode != null) { + if (currentMode.useCustomPrompt()) { + normalOutput.print(currentMode.getCustomPrompt()); + } else { + normalOutput.print(currentMode.getName() + ">> "); + } + + String ln = inputSource.nextLine(); + + if (currentMode.canHandleCommand(ln)) { + String[] commandTokens = ln.split(" "); + + String[] commandArgs; + + if (commandTokens.length > 1) { + commandArgs = Arrays.copyOfRange(commandTokens, 1, + commandTokens.length); + } else { + commandArgs = null; + } + + currentMode = currentMode.processCommand(commandTokens[0], + commandArgs); + } else { + errorOutput.print("Error: Unrecognized command " + ln); + } + } + + normalOutput.print("Exiting now."); + } } 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 522dfbd..658a299 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommand.java +++ b/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommand.java @@ -7,6 +7,34 @@ 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; @@ -35,4 +63,19 @@ public class GenericCommand implements ICommand { public ICommandHelp getHelp() { return help; } + + @Override + public boolean isAlias() { + return false; + } + + /** + * Create a command that is an alias to this one + * + * @return A command that is an alias to this one + */ + @Override + public ICommand createAlias() { + return new DelegatingCommand(this); + } } 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 5b35a0b..4f8e1c8 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommandMode.java +++ b/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommandMode.java @@ -49,6 +49,10 @@ 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(); @@ -130,7 +134,7 @@ public class GenericCommandMode implements ICommandMode { + aliasName + "' to a command with a bound handler"); } else { commandHandlers.put(aliasName, - commandHandlers.get(commandName)); + commandHandlers.get(commandName).createAlias()); } } @@ -237,8 +241,10 @@ public class GenericCommandMode implements ICommandMode { "Help topics for this command mode are as follows:\n"); if (commandHandlers.getSize() > 0) { commandHandlers.forEachValue((command) -> { - normalOutput.accept( - "\t" + command.getHelp().getSummary() + "\n"); + if (!command.isAlias()) { + normalOutput.accept("\t" + + command.getHelp().getSummary() + "\n"); + } }); } else { normalOutput.accept("\tNone available\n"); @@ -248,8 +254,10 @@ public class GenericCommandMode implements ICommandMode { "\nHelp topics available in all command modes are as follows\n"); if (defaultHandlers.getSize() > 0) { defaultHandlers.forEachValue((command) -> { - normalOutput.accept( - "\t" + command.getHelp().getSummary() + "\n"); + if (!command.isAlias()) { + normalOutput.accept("\t" + + command.getHelp().getSummary() + "\n"); + } }); } else { normalOutput.accept("\tNone available\n"); diff --git a/BJC-Utils2/src/main/java/bjc/utils/cli/ICommand.java b/BJC-Utils2/src/main/java/bjc/utils/cli/ICommand.java index 681986d..4cd2dd1 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/cli/ICommand.java +++ b/BJC-Utils2/src/main/java/bjc/utils/cli/ICommand.java @@ -20,4 +20,18 @@ public interface ICommand { * @return The help entry for this command */ public ICommandHelp getHelp(); + + /** + * Create a command that serves as an alias to this one + * + * @return A command that serves as an alias to this one + */ + public ICommand createAlias(); + + /** + * Check if this command is an alias of another command + * + * @return Whether or not this command is an alias of another + */ + public boolean isAlias(); } |
