summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/cli
diff options
context:
space:
mode:
authorbculkin2442 <bjculkin@mix.wvu.edu>2016-04-25 22:11:28 -0400
committerbculkin2442 <bjculkin@mix.wvu.edu>2016-04-25 22:11:28 -0400
commitfb7d03388e298258563c22abda1bd46cdaf991b7 (patch)
treeff2caeda751acd733ea00c089800ff380211f0b3 /BJC-Utils2/src/main/java/bjc/utils/cli
parent42f7d379a430aaf2fad169f0170de04072b08b10 (diff)
General code cleanup, and some more GUI controls
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/cli')
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/cli/CLICommander.java11
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommand.java2
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommandMode.java105
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/cli/ICommandMode.java5
4 files changed, 69 insertions, 54 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 09d3da7..e1a57c2 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/cli/CLICommander.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/cli/CLICommander.java
@@ -67,10 +67,10 @@ public class CLICommander {
normalOutput.print(currentMode.getName() + ">> ");
}
- String ln = inputSource.nextLine();
+ String currentLine = inputSource.nextLine();
- if (currentMode.canHandleCommand(ln)) {
- String[] commandTokens = ln.split(" ");
+ if (currentMode.canHandleCommand(currentLine)) {
+ String[] commandTokens = currentLine.split(" ");
String[] commandArgs;
@@ -84,7 +84,8 @@ public class CLICommander {
currentMode = currentMode.processCommand(commandTokens[0],
commandArgs);
} else {
- errorOutput.print("Error: Unrecognized command " + ln);
+ errorOutput.print(
+ "Error: Unrecognized command " + currentLine);
}
}
@@ -105,4 +106,4 @@ public class CLICommander {
this.initialMode = initialMode;
}
-}
+} \ 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 fad1199..529635d 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommand.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommand.java
@@ -50,4 +50,4 @@ public class GenericCommand implements ICommand {
public boolean isAlias() {
return false;
}
-}
+} \ No newline at end of file
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 84b4d39..a9c9054 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommandMode.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommandMode.java
@@ -70,16 +70,26 @@ public class GenericCommandMode implements ICommandMode {
"Command name must not be null");
} else if (aliasName == null) {
throw new NullPointerException("Alias name must not be null");
- } else if (!commandHandlers.containsKey(commandName)) {
+ } else if (!commandHandlers.containsKey(commandName)
+ && !defaultHandlers.containsKey(commandName)) {
throw new IllegalArgumentException(
"Cannot alias non-existant command '" + commandName
+ "'");
- } else if (commandHandlers.containsKey(aliasName)) {
+ } else if (commandHandlers.containsKey(aliasName)
+ || defaultHandlers.containsKey(aliasName)) {
throw new IllegalArgumentException("Cannot bind alias '"
+ aliasName + "' to a command with a bound handler");
} else {
- commandHandlers.put(aliasName,
- commandHandlers.get(commandName).createAlias());
+ ICommand aliasedCommand;
+ if (defaultHandlers.containsKey(commandName)) {
+ aliasedCommand =
+ defaultHandlers.get(commandName).createAlias();
+ } else {
+ aliasedCommand =
+ commandHandlers.get(commandName).createAlias();
+ }
+
+ commandHandlers.put(aliasName, aliasedCommand);
}
}
@@ -120,29 +130,9 @@ public class GenericCommandMode implements ICommandMode {
helpTopics.put(topicName, help);
}
- private void aliasCommands(String[] args) {
- if (args.length != 2) {
- 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 (!canHandleCommand(commandName)) {
- errorOutput.accept("ERROR: '" + commandName
- + "' is not a valid command.");
- } else if (canHandleCommand(aliasName)) {
- errorOutput.accept("ERROR: Cannot overwrite command '"
- + aliasName + "'");
- } else {
- addCommandAlias(commandName, aliasName);
- }
- }
- }
-
private GenericCommand buildAliasCommand() {
return new GenericCommand((args) -> {
- aliasCommands(args);
+ doAliasCommands(args);
return this;
}, "alias\tAlias one command to another",
@@ -177,10 +167,10 @@ public class GenericCommandMode implements ICommandMode {
return new GenericCommand((args) -> {
if (args == null || args.length == 0) {
// Invoke general help
- helpSummary();
+ doHelpSummary();
} else {
// Invoke help for a command
- helpCommand(args[0]);
+ doHelpCommand(args[0]);
}
return this;
@@ -194,7 +184,7 @@ public class GenericCommandMode implements ICommandMode {
private GenericCommand buildListCommand() {
return new GenericCommand((args) -> {
- listCommands();
+ doListCommands();
return this;
}, "list\tList available command",
@@ -208,25 +198,27 @@ public class GenericCommandMode implements ICommandMode {
|| defaultHandlers.containsKey(command);
}
- @Override
- public String getCustomPrompt() {
- if (customPrompt != null) {
- return customPrompt;
- }
-
- return ICommandMode.super.getCustomPrompt();
- }
+ 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");
+ } else {
+ String commandName = args[0];
+ String aliasName = args[1];
- @Override
- public String getName() {
- if (modeName != null) {
- return modeName;
+ if (!canHandleCommand(commandName)) {
+ errorOutput.accept("ERROR: '" + commandName
+ + "' is not a valid command.");
+ } else if (canHandleCommand(aliasName)) {
+ errorOutput.accept("ERROR: Cannot overwrite command '"
+ + aliasName + "'");
+ } else {
+ addCommandAlias(commandName, aliasName);
+ }
}
-
- return ICommandMode.super.getName();
}
- private void helpCommand(String commandName) {
+ private void doHelpCommand(String commandName) {
if (commandHandlers.containsKey(commandName)) {
normalOutput.accept("\n" + commandHandlers.get(commandName)
.getHelp().getDescription());
@@ -243,7 +235,7 @@ public class GenericCommandMode implements ICommandMode {
}
}
- private void helpSummary() {
+ private void doHelpSummary() {
normalOutput.accept(
"Help topics for this command mode are as follows:\n");
if (commandHandlers.getSize() > 0) {
@@ -281,7 +273,7 @@ public class GenericCommandMode implements ICommandMode {
}
}
- private void listCommands() {
+ private void doListCommands() {
normalOutput.accept(
"The available commands for this mode are as follows:\n");
@@ -300,6 +292,24 @@ public class GenericCommandMode implements ICommandMode {
}
@Override
+ public String getCustomPrompt() {
+ if (customPrompt != null) {
+ return customPrompt;
+ }
+
+ return ICommandMode.super.getCustomPrompt();
+ }
+
+ @Override
+ public String getName() {
+ if (modeName != null) {
+ return modeName;
+ }
+
+ return ICommandMode.super.getName();
+ }
+
+ @Override
public ICommandMode processCommand(String command, String[] args) {
normalOutput.accept("\n");
@@ -373,7 +383,10 @@ 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 all
+ // modes, but only apply to a specific application
defaultHandlers.put("clear", buildClearCommands());
defaultHandlers.put("exit", buildExitCommand());
diff --git a/BJC-Utils2/src/main/java/bjc/utils/cli/ICommandMode.java b/BJC-Utils2/src/main/java/bjc/utils/cli/ICommandMode.java
index f6313af..48a741c 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/cli/ICommandMode.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/cli/ICommandMode.java
@@ -36,10 +36,11 @@ public interface ICommandMode {
/**
* Get the name of this command mode
*
- * @return The name of this command mode, which is "crawler" by default
+ * @return The name of this command mode, which is the empty string by
+ * default
*/
public default String getName() {
- return "crawler";
+ return "";
}
/**