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.java116
-rw-r--r--base/src/main/java/bjc/utils/cli/objects/Command.java40
-rw-r--r--base/src/main/java/bjc/utils/cli/objects/DefineCLI.java49
3 files changed, 119 insertions, 86 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 94ee726..ac62c80 100644
--- a/base/src/main/java/bjc/utils/cli/objects/BlockReaderCLI.java
+++ b/base/src/main/java/bjc/utils/cli/objects/BlockReaderCLI.java
@@ -35,20 +35,20 @@ public class BlockReaderCLI {
/**
* All of the configured block readers.
*/
- public final Map<String, BlockReader> readers;
+ public final Map<String, BlockReader> readers;
/**
* All of the configured I/O sources.
*/
- public final Map<String, Reader> 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.
+ * The set of configured block readers.
*
* @param sources
- * The set of configured I/O sources.
+ * The set of configured I/O sources.
*/
public BlockReaderState(Map<String, BlockReader> readers, Map<String, Reader> sources) {
this.readers = readers;
@@ -63,7 +63,7 @@ public class BlockReaderCLI {
* Create a new CLI for configuring BlockReaders.
*
* @param srcs
- * The container of initial I/O sources.
+ * The container of initial I/O sources.
*/
public BlockReaderCLI(Map<String, Reader> srcs) {
stat = new BlockReaderState(new HashMap<>(), srcs);
@@ -73,7 +73,7 @@ public class BlockReaderCLI {
* Create a new CLI for configuring BlockReaders.
*
* @param state
- * The state object to use.
+ * The state object to use.
*/
public BlockReaderCLI(BlockReaderState state) {
stat = state;
@@ -84,12 +84,12 @@ public class BlockReaderCLI {
* Run the command line interface
*
* @param args
- * Ignored CLI args.
+ * Ignored CLI args.
*/
public static void main(String[] args) {
/* Create/configure I/O sources. */
Map<String, Reader> sources = new HashMap<>();
- BlockReaderCLI reader = new BlockReaderCLI(sources);
+ BlockReaderCLI reader = new BlockReaderCLI(sources);
sources.put("stdio", new InputStreamReader(System.in));
@@ -102,13 +102,13 @@ public class BlockReaderCLI {
* Run the CLI on an input source.
*
* @param input
- * The place to read input from.
+ * The place to read input from.
*
* @param ioSource
- * The name of the place to read input from.
+ * The name of the place to read input from.
*
* @param interactive
- * Whether or not the source is interactive
+ * Whether or not the source is interactive
*/
public void run(Scanner input, String ioSource, boolean interactive) {
int lno = 0;
@@ -128,9 +128,9 @@ public class BlockReaderCLI {
if(com == null) continue;
/* Handle a command. */
- CommandStatus stat = handleCommand(com, interactive);
+ CommandStatus stt = handleCommand(com, interactive);
/* Exit if we finished or encountered a fatal error. */
- if(stat == FINISH || stat == ERROR) {
+ if(stt == FINISH || stt == ERROR) {
return;
}
@@ -146,36 +146,36 @@ public class BlockReaderCLI {
* Handle a command.
*
* @param com
- * The command to handle
+ * The command to handle
*
* @param interactive
- * Whether the current input source is interactive or not.
+ * Whether the current input source is interactive or not.
+ * @return The status of the executed command.
*/
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;
}
}
@@ -191,7 +191,7 @@ public class BlockReaderCLI {
return FAIL;
}
String blockName = remn.substring(0, idx).trim();
- remn = remn.substring(idx).trim();
+ remn = remn.substring(idx).trim();
/*
* Check there isn't a reader already bound to this name.
@@ -205,11 +205,11 @@ public class BlockReaderCLI {
*/
idx = remn.indexOf(' ');
if(idx == -1) {
- LOGGER.severe(com.error("No reader-name argument for def-filtered.\n"));
+ LOGGER.severe(com.error("No reader-name argument for def-filtered.\n"));
return FAIL;
}
String readerName = remn.substring(0, idx).trim();
- remn = remn.substring(idx).trim();
+ remn = remn.substring(idx).trim();
/*
* Check there is a reader bound to that name.
@@ -238,22 +238,26 @@ public class BlockReaderCLI {
return mat.matches();
};
+ @SuppressWarnings("resource")
BlockReader reader = new FilteredBlockReader(stat.readers.get(readerName), pred);
stat.readers.put(blockName, reader);
- } catch (PatternSyntaxException psex) {
- LOGGER.severe(com.error("Invalid regular expression '%s' for filter. (%s)\n", filter, psex.getMessage()));
+ } catch(PatternSyntaxException psex) {
+ LOGGER.severe(com.error("Invalid regular expression '%s' for filter. (%s)\n", filter,
+ psex.getMessage()));
return FAIL;
}
return SUCCESS;
}
+ @SuppressWarnings("resource")
private CommandStatus defPushback(Command com) {
String[] parts = com.remnCommand.split(" ");
if(parts.length != 2) {
- LOGGER.severe(com.error("Incorrect number of arguments to def-pushback. Requires a block name and a reader name\n"));
+ LOGGER.severe(com.error(
+ "Incorrect number of arguments to def-pushback. Requires a block name and a reader name\n"));
return FAIL;
}
@@ -275,11 +279,13 @@ public class BlockReaderCLI {
return SUCCESS;
}
+ @SuppressWarnings("resource")
private CommandStatus defToggled(Command com) {
String[] parts = com.remnCommand.split(" ");
if(parts.length != 3) {
- LOGGER.severe(com.error("Incorrect number of arguments to def-toggled. Requires a block name and two reader names\n"));
+ LOGGER.severe(com.error(
+ "Incorrect number of arguments to def-toggled. Requires a block name and two reader names\n"));
return FAIL;
}
@@ -310,11 +316,13 @@ public class BlockReaderCLI {
return SUCCESS;
}
+ @SuppressWarnings("resource")
private CommandStatus defLayered(Command com) {
String[] parts = com.remnCommand.split(" ");
if(parts.length != 3) {
- LOGGER.severe(com.error("Incorrect number of arguments to def-layered. Requires a block name and two reader names\n"));
+ LOGGER.severe(com.error(
+ "Incorrect number of arguments to def-layered. Requires a block name and two reader names\n"));
return FAIL;
}
@@ -345,11 +353,13 @@ public class BlockReaderCLI {
return SUCCESS;
}
+ @SuppressWarnings("resource")
private CommandStatus defSerial(Command com) {
String[] parts = com.remnCommand.split(" ");
if(parts.length < 2) {
- LOGGER.severe(com.error("Not enough arguments to def-serial. Requires at least a block name and at least one reader name\n"));
+ LOGGER.severe(com.error(
+ "Not enough arguments to def-serial. Requires at least a block name and at least one reader name\n"));
return FAIL;
}
@@ -401,7 +411,7 @@ public class BlockReaderCLI {
return FAIL;
}
String blockName = remn.substring(0, idx).trim();
- remn = remn.substring(idx).trim();
+ remn = remn.substring(idx).trim();
/*
* Check there isn't a reader already bound to this name.
@@ -415,11 +425,11 @@ public class BlockReaderCLI {
*/
idx = remn.indexOf(' ');
if(idx == -1) {
- LOGGER.severe(com.error("No source-name argument for def-simple.\n"));
+ LOGGER.severe(com.error("No source-name argument for def-simple.\n"));
return FAIL;
}
String sourceName = remn.substring(0, idx).trim();
- remn = remn.substring(idx).trim();
+ remn = remn.substring(idx).trim();
/*
* Check there is a source bound to that name.
@@ -440,11 +450,13 @@ public class BlockReaderCLI {
String delim = remn;
try {
+ @SuppressWarnings("resource")
BlockReader reader = new SimpleBlockReader(delim, stat.sources.get(sourceName));
stat.readers.put(blockName, reader);
- } catch (PatternSyntaxException psex) {
- LOGGER.severe(com.error("Invalid regular expression '%s' for delimiter. (%s)\n", delim, psex.getMessage()));
+ } catch(PatternSyntaxException psex) {
+ LOGGER.severe(com.error("Invalid regular expression '%s' for delimiter. (%s)\n", delim,
+ psex.getMessage()));
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 3a7d452..04cdfd9 100644
--- a/base/src/main/java/bjc/utils/cli/objects/Command.java
+++ b/base/src/main/java/bjc/utils/cli/objects/Command.java
@@ -38,15 +38,15 @@ public class Command {
/**
* The full text of this command.
*/
- public final String fullCommand;
+ public final String fullCommand;
/**
* The text of this command without its name.
*/
- public final String remnCommand;
+ public final String remnCommand;
/**
* The name of this command.
*/
- public final String nameCommand;
+ public final String nameCommand;
/**
* The name of the I/O source this command was read from.
@@ -57,13 +57,13 @@ public class Command {
* Create a new command.
*
* @param ln
- * The string to get the command from.
+ * The string to get the command from.
*
* @param lno
- * The number of the line the command came from.
+ * The number of the line the command came from.
*
* @param ioSrc
- * The name of where the I/O came from.
+ * The name of where the I/O came from.
*/
public Command(String ln, int lno, String ioSrc) {
int idx = ln.indexOf(' ');
@@ -87,17 +87,18 @@ public class Command {
* through the line.
*
* @param ln
- * The string to get the command from.
+ * The string to get the command from.
*
* @param lno
- * The line number of the command.
+ * The line number of the command.
*
* @param ioSource
- * The name of where the I/O came from.
+ * The name of where the I/O came from.
+ * @return The parsed command
*/
public static Command fromString(String ln, int lno, String ioSource) {
/* Ignore blank lines and comments. */
- if(ln.equals("")) return null;
+ if(ln.equals("")) return null;
if(ln.startsWith("#")) return null;
/* Trim off comments part-way through the line. */
@@ -114,10 +115,11 @@ public class Command {
* command.
*
* @param info
- * The informational message.
+ * The informational message.
*
* @param parms
- * The parameters for the informational message.
+ * The parameters for the informational message.
+ * @return The information message.
*/
public String info(String info, Object... parms) {
String msg = String.format(info, parms);
@@ -129,10 +131,12 @@ public class Command {
* Warn about something in relation to this command.
*
* @param warning
- * The warning message.
+ * The warning message.
*
* @param parms
- * The parameters for the warning message.
+ * The parameters for the warning message.
+ *
+ * @return The formatted warning.
*/
public String warn(String warning, Object... parms) {
String msg = String.format(warning, parms);
@@ -143,11 +147,13 @@ public class Command {
/**
* Give an error about something in relation to this command.
*
- * @param error
- * The error message.
+ * @param err
+ * The error message.
*
* @param parms
- * The parameters for the error message.
+ * The parameters for the error message.
+ *
+ * @return The formatted error
*/
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 280afd0..aa9bcf3 100644
--- a/base/src/main/java/bjc/utils/cli/objects/DefineCLI.java
+++ b/base/src/main/java/bjc/utils/cli/objects/DefineCLI.java
@@ -23,11 +23,11 @@ import static bjc.utils.cli.objects.Command.CommandStatus.*;
public class DefineCLI {
private final Logger LOGGER = Logger.getLogger(DefineCLI.class.getName());
- public static class DefineState {
+ static class DefineState {
public final Map<String, UnaryOperator<String>> defines;
- public final Map<String, String> strings;
- public final Map<String, String> formats;
+ public final Map<String, String> strings;
+ public final Map<String, String> formats;
public final Map<String, Pattern> patterns;
@@ -48,14 +48,23 @@ public class DefineCLI {
private DefineState stat;
+ /**
+ * Create a new Define CLI
+ */
public DefineCLI() {
stat = new DefineState();
}
+ /**
+ * Main method
+ *
+ * @param args
+ * CLI args
+ */
public static void main(String[] args) {
DefineCLI defin = new DefineCLI();
- try (Scanner scn = new Scanner(System.in)) {
+ try(Scanner scn = new Scanner(System.in)) {
defin.run(scn, "console", true);
}
}
@@ -64,25 +73,23 @@ public class DefineCLI {
* Run the CLI on an input source.
*
* @param input
- * The place to read input from.
+ * The place to read input from.
* @param ioSource
- * The name of the place to read input from.
+ * The name of the place to read input from.
* @param interactive
- * Whether or not the source is 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("define-conf(%d)>", lno);
+ while(input.hasNextLine()) {
+ if(interactive) System.out.printf("define-conf(%d)>", lno);
String ln = input.nextLine();
lno += 1;
Command com = Command.fromString(ln, lno, ioSource);
- if (com == null)
- continue;
+ if(com == null) continue;
handleCommand(com, interactive);
}
@@ -90,8 +97,16 @@ public class DefineCLI {
input.close();
}
+ /**
+ * Handle a command
+ *
+ * @param com
+ * The command to handle
+ * @param interactive
+ * Whether or not our I/O stream is interactive
+ */
public void handleCommand(Command com, boolean interactive) {
- switch (com.nameCommand) {
+ switch(com.nameCommand) {
case "def-string":
default:
LOGGER.severe(com.error("Unknown command %s\n", com.nameCommand));
@@ -103,14 +118,14 @@ public class DefineCLI {
String remn = com.remnCommand;
int idx = remn.indexOf(' ');
- if (idx == -1) {
+ if(idx == -1) {
LOGGER.warning(com.warn("Binding empty string to name '%s'\n", remn));
idx = remn.length();
}
String name = remn.substring(0, idx);
String strang = remn.substring(idx);
- if (stat.strings.containsKey(name)) {
+ if(stat.strings.containsKey(name)) {
LOGGER.warning(com.warn("Shadowing string '%s'\n", name));
}
@@ -123,14 +138,14 @@ public class DefineCLI {
String remn = com.remnCommand;
int idx = remn.indexOf(' ');
- if (idx == -1) {
+ if(idx == -1) {
LOGGER.warning(com.warn("Binding empty format to name '%s'\n", remn));
idx = remn.length();
}
String name = remn.substring(0, idx);
String fmt = remn.substring(idx);
- if (stat.formats.containsKey(name)) {
+ if(stat.formats.containsKey(name)) {
LOGGER.warning(com.warn("Shadowing format '%s'\n", name));
}