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.java134
-rw-r--r--base/src/main/java/bjc/utils/cli/objects/Command.java84
-rw-r--r--base/src/main/java/bjc/utils/cli/objects/DefineCLI.java10
3 files changed, 185 insertions, 43 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 ec66fe2..2edea08 100644
--- a/base/src/main/java/bjc/utils/cli/objects/BlockReaderCLI.java
+++ b/base/src/main/java/bjc/utils/cli/objects/BlockReaderCLI.java
@@ -18,19 +18,46 @@ import bjc.utils.ioutils.blocks.*;
import static bjc.utils.cli.objects.Command.CommandStatus;
import static bjc.utils.cli.objects.Command.CommandStatus.*;
+/**
+ * Command-line interface for configuring block readers.
+ *
+ * @author Ben Culkin
+ */
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;
/**
@@ -43,16 +70,33 @@ public class BlockReaderCLI {
stat = new BlockReaderState(new HashMap<>(), srcs);
}
+ /**
+ * Create a new CLI for configuring BlockReaders.
+ *
+ * @param state
+ * The state object to use.
+ */
+ public BlockReaderCLI(BlockReaderState state) {
+ stat = state;
+ }
+
+ /* :CLIArgsParsing */
+ /**
+ * Run the command line interface
+ *
+ * @param args
+ * Ignored CLI args.
+ */
public static void main(String[] args) {
- /*
- * Create/configure I/O sources.
- */
+ /* Create/configure I/O sources. */
Map<String, Reader> sources = new HashMap<>();
- sources.put("stdio", new InputStreamReader(System.in));
+ BlockReaderCLI reader = new BlockReaderCLI(sources);
- BlockReaderCLI reader = new BlockReaderCLI(sources);
+ sources.put("stdio", new InputStreamReader(System.in));
- reader.run(new Scanner(System.in), "console", true);
+ Scanner input = new Scanner(System.in);
+ reader.run(input, "console", true);
+ input.close();
}
/**
@@ -60,61 +104,79 @@ public class BlockReaderCLI {
*
* @param input
* The place to read input from.
+ *
* @param ioSource
* The name of the place to read input from.
+ *
* @param interactive
* Whether or not the source is interactive
*/
public void run(Scanner input, String ioSource, boolean interactive) {
int lno = 0;
- while(input.hasNextLine()) {
- if(interactive)
- System.out.printf("reader-conf(%d)>", lno);
+ /* Print a prompt. */
+ if(interactive) {
+ System.out.printf("reader-conf(%d)>", lno);
+ }
+ while(input.hasNextLine()) {
+ /* Read a line. */
String ln = input.nextLine();
-
lno += 1;
+ /* Parse the command. */
Command com = Command.fromString(ln, lno, ioSource);
+ /* Ignore blank commands. */
if(com == null) continue;
+ /* Handle a command. */
CommandStatus stat = handleCommand(com, interactive);
+ /* Exit if we finished or encountered a fatal error. */
if(stat == FINISH || stat == ERROR) {
return;
}
- }
- input.close();
+ /* Print a prompt. */
+ if(interactive) {
+ System.out.printf("reader-conf(%d)>", lno);
+ }
+
+ }
}
- /*
+ /**
* Handle a command.
+ *
+ * @param com
+ * The command to handle
+ *
+ * @param interactive
+ * Whether the current input source is interactive or not.
*/
public CommandStatus handleCommand(Command com, boolean interactive) {
switch(com.nameCommand) {
- case "def-filtered":
- return defFiltered(com);
- case "def-layered":
- return defLayered(com);
- case "def-pushback":
- return defPushback(com);
- case "def-simple":
- return defSimple(com);
- case "def-serial":
- return defSerial(com);
- case "def-toggled":
- return defToggled(com);
- case "}":
- case "end":
- case "exit":
- case "quit":
- if(interactive)
- System.out.printf("Exiting reader-conf, %d readers configured in %d commands\n",
- stat.readers.size(), com.lineNo);
- return FINISH;
- default:
- LOGGER.severe(com.error("Unknown command '%s'\n", com.nameCommand));
- return FAIL;
+ case "def-filtered":
+ return defFiltered(com);
+ case "def-layered":
+ return defLayered(com);
+ case "def-pushback":
+ return defPushback(com);
+ case "def-simple":
+ return defSimple(com);
+ case "def-serial":
+ return defSerial(com);
+ case "def-toggled":
+ return defToggled(com);
+ case "}":
+ case "end":
+ case "exit":
+ case "quit":
+ if(interactive)
+ System.out.printf("Exiting reader-conf, %d readers configured in %d commands\n",
+ stat.readers.size(), com.lineNo);
+ return FINISH;
+ default:
+ LOGGER.severe(com.error("Unknown command '%s'\n", com.nameCommand));
+ return FAIL;
}
}
diff --git a/base/src/main/java/bjc/utils/cli/objects/Command.java b/base/src/main/java/bjc/utils/cli/objects/Command.java
index e605a2b..3a7d452 100644
--- a/base/src/main/java/bjc/utils/cli/objects/Command.java
+++ b/base/src/main/java/bjc/utils/cli/objects/Command.java
@@ -1,8 +1,15 @@
package bjc.utils.cli.objects;
+/**
+ * A single-line command read from the user.
+ *
+ * @author Ben Culkin
+ */
public class Command {
/**
* Command status values.
+ *
+ * @author Ben Culkin
*/
public static enum CommandStatus {
/**
@@ -23,21 +30,38 @@ public class Command {
FINISH,
}
+ /**
+ * The line number of this command.
+ */
public final int lineNo;
+ /**
+ * The full text of this command.
+ */
public final String fullCommand;
+ /**
+ * The text of this command without its name.
+ */
public final String remnCommand;
+ /**
+ * The name of this command.
+ */
public final String nameCommand;
+ /**
+ * The name of the I/O source this command was read from.
+ */
public final String ioSource;
/**
* Create a new command.
*
* @param ln
- * The line to get the command from.
+ * The string to get the command from.
+ *
* @param lno
* The number of the line the command came from.
+ *
* @param ioSrc
* The name of where the I/O came from.
*/
@@ -55,16 +79,28 @@ public class Command {
ioSource = ioSrc;
}
+ /**
+ * Parse a command from a string.
+ *
+ * The main thing this does is ignore blank lines, as well as comments
+ * marked by #'s either at the start of the line or part of the way
+ * through the line.
+ *
+ * @param ln
+ * The string to get the command from.
+ *
+ * @param lno
+ * The line number of the command.
+ *
+ * @param ioSource
+ * The name of where the I/O came from.
+ */
public static Command fromString(String ln, int lno, String ioSource) {
- /*
- * Ignore blank lines and comments.
- */
+ /* Ignore blank lines and comments. */
if(ln.equals("")) return null;
if(ln.startsWith("#")) return null;
- /*
- * Trim off comments part-way through the line.
- */
+ /* Trim off comments part-way through the line. */
int idxHash = ln.indexOf('#');
if(idxHash != -1) {
ln = ln.substring(0, idxHash).trim();
@@ -73,12 +109,46 @@ public class Command {
return new Command(ln, lno, ioSource);
}
+ /**
+ * Give an informational message about something in relation to this
+ * command.
+ *
+ * @param info
+ * The informational message.
+ *
+ * @param parms
+ * The parameters for the informational message.
+ */
+ public String info(String info, Object... parms) {
+ String msg = String.format(info, parms);
+
+ return String.format("INFO (%s:%d): %s", ioSource, lineNo, msg);
+ }
+
+ /**
+ * Warn about something in relation to this command.
+ *
+ * @param warning
+ * The warning message.
+ *
+ * @param parms
+ * The parameters for the warning message.
+ */
public String warn(String warning, Object... parms) {
String msg = String.format(warning, parms);
return String.format("WARNING (%s:%d): %s", ioSource, lineNo, msg);
}
+ /**
+ * Give an error about something in relation to this command.
+ *
+ * @param error
+ * The error message.
+ *
+ * @param parms
+ * The parameters for the error message.
+ */
public String error(String err, Object... parms) {
String msg = String.format(err, parms);
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 bb2733f..5763a83 100644
--- a/base/src/main/java/bjc/utils/cli/objects/DefineCLI.java
+++ b/base/src/main/java/bjc/utils/cli/objects/DefineCLI.java
@@ -10,6 +10,16 @@ import java.util.regex.Pattern;
import static bjc.utils.cli.objects.Command.CommandStatus;
import static bjc.utils.cli.objects.Command.CommandStatus.*;
+/*
+ * @TODO 10/09/17 :DefineCLIFinish
+ * This got left off about halfway through due to getting distracted
+ * implementing CL-style format strings. It needs to be finished.
+ */
+/**
+ * Command-line interface for building defines.
+ *
+ * @author Ben Culkin
+ */
public class DefineCLI {
private final Logger LOGGER = Logger.getLogger(DefineCLI.class.getName());