summaryrefslogtreecommitdiff
path: root/base/src/main/java/bjc/utils/cli/objects
diff options
context:
space:
mode:
Diffstat (limited to 'base/src/main/java/bjc/utils/cli/objects')
-rw-r--r--base/src/main/java/bjc/utils/cli/objects/BlockReaderCLI.java30
-rw-r--r--base/src/main/java/bjc/utils/cli/objects/BlockReaderState.java36
-rw-r--r--base/src/main/java/bjc/utils/cli/objects/DefineCLI.java109
3 files changed, 128 insertions, 47 deletions
diff --git a/base/src/main/java/bjc/utils/cli/objects/BlockReaderCLI.java b/base/src/main/java/bjc/utils/cli/objects/BlockReaderCLI.java
index 5b14a58..6ab4cec 100644
--- a/base/src/main/java/bjc/utils/cli/objects/BlockReaderCLI.java
+++ b/base/src/main/java/bjc/utils/cli/objects/BlockReaderCLI.java
@@ -26,36 +26,6 @@ public class BlockReaderCLI {
/* Logger. */
private final Logger LOGGER = Logger.getLogger(BlockReaderCLI.class.getName());
- /**
- * The state of the block reader.
- *
- * @author Ben Culkin
- */
- public static class BlockReaderState {
- /**
- * All of the configured block readers.
- */
- public final Map<String, BlockReader> readers;
- /**
- * All of the configured I/O sources.
- */
- public final Map<String, Reader> sources;
-
- /**
- * Create a new set of state for the block reader.
- *
- * @param readers
- * The set of configured block readers.
- *
- * @param sources
- * The set of configured I/O sources.
- */
- public BlockReaderState(Map<String, BlockReader> readers, Map<String, Reader> sources) {
- this.readers = readers;
- this.sources = sources;
- }
- }
-
/* Our state. */
private BlockReaderState stat;
diff --git a/base/src/main/java/bjc/utils/cli/objects/BlockReaderState.java b/base/src/main/java/bjc/utils/cli/objects/BlockReaderState.java
new file mode 100644
index 0000000..c528e6a
--- /dev/null
+++ b/base/src/main/java/bjc/utils/cli/objects/BlockReaderState.java
@@ -0,0 +1,36 @@
+package bjc.utils.cli.objects;
+
+import bjc.utils.ioutils.blocks.BlockReader;
+
+import java.io.Reader;
+import java.util.Map;
+
+/**
+ * The state of the block reader.
+ *
+ * @author Ben Culkin
+ */
+public class BlockReaderState {
+ /**
+ * All of the configured block readers.
+ */
+ public final Map<String, BlockReader> readers;
+ /**
+ * All of the configured I/O sources.
+ */
+ public final Map<String, Reader> sources;
+
+ /**
+ * Create a new set of state for the block reader.
+ *
+ * @param readers
+ * The set of configured block readers.
+ *
+ * @param sources
+ * The set of configured I/O sources.
+ */
+ public BlockReaderState(Map<String, BlockReader> readers, Map<String, Reader> sources) {
+ this.readers = readers;
+ this.sources = sources;
+ }
+} \ No newline at end of file
diff --git a/base/src/main/java/bjc/utils/cli/objects/DefineCLI.java b/base/src/main/java/bjc/utils/cli/objects/DefineCLI.java
index 45d27ce..787b91a 100644
--- a/base/src/main/java/bjc/utils/cli/objects/DefineCLI.java
+++ b/base/src/main/java/bjc/utils/cli/objects/DefineCLI.java
@@ -1,6 +1,12 @@
package bjc.utils.cli.objects;
+import bjc.utils.funcutils.StringUtils;
+import bjc.utils.ioutils.format.CLFormatter;
+
+import java.util.ArrayList;
import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.function.UnaryOperator;
@@ -104,26 +110,40 @@ public class DefineCLI {
* The command to handle
* @param interactive
* Whether or not our I/O stream is interactive
+ * @return The status of the executed command.
*/
- public void handleCommand(Command com, boolean interactive) {
+ public CommandStatus handleCommand(Command com, boolean interactive) {
switch(com.nameCommand) {
case "def-string":
+ return defString(com);
+ case "def-format":
+ return defFormat(com);
+ case "bind-format":
+ return bindFormat(com);
default:
LOGGER.severe(com.error("Unknown command %s\n", com.nameCommand));
- break;
+ return FAIL;
}
}
private CommandStatus defString(Command com) {
- String remn = com.remnCommand;
+ List<String> arguments = StringUtils.processArguments(com.remnCommand);
+
+ if(arguments.size() < 1) {
+ LOGGER.severe(com.error(
+ "def-string expects at least one argument: the name of the string to bind"));
+ return FAIL;
+ }
- int idx = remn.indexOf(' ');
- if(idx == -1) {
- LOGGER.warning(com.warn("Binding empty string to name '%s'\n", remn));
- idx = remn.length();
+ String name = arguments.get(0);
+ String strang;
+
+ if(arguments.size() < 2) {
+ LOGGER.warning(com.warn("Binding empty string to name '%s'\n", name));
+ strang = "";
+ } else {
+ strang = arguments.get(1);
}
- String name = remn.substring(0, idx);
- String strang = remn.substring(idx);
if(stat.strings.containsKey(name)) {
LOGGER.warning(com.warn("Shadowing string '%s'\n", name));
@@ -135,15 +155,23 @@ public class DefineCLI {
}
private CommandStatus defFormat(Command com) {
- String remn = com.remnCommand;
+ List<String> arguments = StringUtils.processArguments(com.remnCommand);
- int idx = remn.indexOf(' ');
- if(idx == -1) {
- LOGGER.warning(com.warn("Binding empty format to name '%s'\n", remn));
- idx = remn.length();
+ if(arguments.size() < 1) {
+ LOGGER.severe(com.error(
+ "def-format expects at least one argument: the name of the format to bind"));
+ return FAIL;
+ }
+
+ String name = arguments.get(0);
+ String fmt;
+
+ if(arguments.size() < 2) {
+ LOGGER.warning(com.warn("Binding empty format to name '%s'\n", name));
+ fmt = "";
+ } else {
+ fmt = arguments.get(1);
}
- String name = remn.substring(0, idx);
- String fmt = remn.substring(idx);
if(stat.formats.containsKey(name)) {
LOGGER.warning(com.warn("Shadowing format '%s'\n", name));
@@ -155,7 +183,54 @@ public class DefineCLI {
}
private CommandStatus bindFormat(Command com) {
- String[] parts = com.remnCommand.split(" ");
+ List<String> strings = StringUtils.processArguments(com.remnCommand);
+
+ if(strings.size() < 2) {
+ LOGGER.severe(com.error(
+ "Binding a format requires at least two arguments: the format to bind, and the name to bind it to."));
+ return FAIL;
+ }
+
+ String formatName = strings.get(0);
+ if(!stat.formats.containsKey(formatName)) {
+ LOGGER.severe(com.error("Unknown format %s", formatName));
+ return FAIL;
+ }
+
+ String bindName = strings.get(1);
+ if(stat.strings.containsKey(bindName)) {
+ LOGGER.warning(com.warn("Shadowing string '%s'", bindName));
+ }
+
+ List<String> fillIns = new ArrayList<>(strings.size() - 1);
+
+ Iterator<String> itr = fillIns.iterator();
+ /* Skip the format name and bind var. */
+ itr.next();
+ itr.next();
+
+ while(itr.hasNext()) {
+ String name = itr.next();
+
+ if(name.startsWith("$")) {
+ String varName = name.substring(1);
+
+ if(stat.strings.containsKey(varName)) {
+ fillIns.add(stat.strings.get(varName));
+ } else {
+ LOGGER.severe(com.error("Unknown string '%s'", varName));
+ return FAIL;
+ }
+ } else {
+ fillIns.add(name);
+ }
+ }
+
+ CLFormatter fmt = new CLFormatter();
+
+ String formatted = fmt.formatString(stat.formats.get(formatName), fillIns);
+
+ stat.strings.put(bindName, formatted);
return SUCCESS;
}