diff options
| author | Ben Culkin <scorpress@gmail.com> | 2020-04-13 18:40:41 -0400 |
|---|---|---|
| committer | Ben Culkin <scorpress@gmail.com> | 2020-04-13 18:40:41 -0400 |
| commit | d4ca769e542b2489b1e23cfcbdc3a0b7275b87cd (patch) | |
| tree | 1653a7399f97fb0c63ce62e3f60fd830d5c37f70 /base/src/main/java/bjc/utils/cli/objects | |
| parent | 2ac2e31a56ae59ee582e43a90c3495f86dd9ee7a (diff) | |
Cleanup pass
Cleanup pass to uniformize things
Diffstat (limited to 'base/src/main/java/bjc/utils/cli/objects')
4 files changed, 169 insertions, 147 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 93d55d8..1c50df3 100644 --- a/base/src/main/java/bjc/utils/cli/objects/BlockReaderCLI.java +++ b/base/src/main/java/bjc/utils/cli/objects/BlockReaderCLI.java @@ -56,7 +56,8 @@ public class BlockReaderCLI { * @param sources * The set of configured I/O sources. */ - public BlockReaderState(Map<String, BlockReader> readers, Map<String, Reader> sources) { + public BlockReaderState(Map<String, BlockReader> readers, + Map<String, Reader> sources) { this.readers = readers; this.sources = sources; } @@ -83,7 +84,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(srcs); @@ -93,7 +94,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; @@ -104,7 +105,7 @@ 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. */ @@ -122,13 +123,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 srcName - * 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 srcName, boolean interactive) { int lno = 0; @@ -146,12 +147,15 @@ public class BlockReaderCLI { /* Parse the command. */ Command com = Command.fromString(ln, lno, srcName); /* Ignore blank commands. */ - if (com == null) continue; + if (com == null) + continue; /* Handle a command. */ CommandStatus sts = handleCommand(com, interactive); /* Exit if we finished or encountered a fatal error. */ - if (sts == FINISH || sts == ERROR) { return; } + if (sts == FINISH || sts == ERROR) { + return; + } } while (input.hasNextLine()); } @@ -159,11 +163,10 @@ 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) { @@ -186,7 +189,8 @@ public class BlockReaderCLI { case "exit": case "quit": if (interactive) - System.out.printf("Exiting reader-conf, %d readers configured in %d commands\n", + System.out.printf( + "Exiting reader-conf, %d readers configured in %d commands\n", stat.readers.size(), com.lno); return FINISH; default: @@ -244,18 +248,19 @@ public class BlockReaderCLI { try { Pattern pat = Pattern.compile(filter); - Predicate<Block> pred = (block) -> { + Predicate<Block> pred = block -> { Matcher mat = pat.matcher(block.contents); return mat.matches(); }; - BlockReader reader = new FilteredBlockReader(stat.readers.get(readerName), pred); + 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())); + LOGGER.severe(com.error("Invalid regular expression '%s' for filter. (%s)\n", + filter, psex.getMessage())); return FAIL; } @@ -319,7 +324,8 @@ public class BlockReaderCLI { return FAIL; } - BlockReader reader = new ToggledBlockReader(stat.readers.get(parts[1]), stat.readers.get(parts[2])); + BlockReader reader = new ToggledBlockReader(stat.readers.get(parts[1]), + stat.readers.get(parts[2])); stat.readers.put(blockName, reader); return SUCCESS; @@ -355,7 +361,8 @@ public class BlockReaderCLI { return FAIL; } - BlockReader reader = new LayeredBlockReader(stat.readers.get(parts[1]), stat.readers.get(parts[2])); + BlockReader reader = new LayeredBlockReader(stat.readers.get(parts[1]), + stat.readers.get(parts[2])); stat.readers.put(blockName, reader); return SUCCESS; @@ -460,12 +467,14 @@ public class BlockReaderCLI { /* Get the delimiter, and create the reader. */ try { - BlockReader reader = new SimpleBlockReader(delim, stat.sources.get(sourceName)); + 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())); + 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 2be7fcd..ce2a985 100644 --- a/base/src/main/java/bjc/utils/cli/objects/Command.java +++ b/base/src/main/java/bjc/utils/cli/objects/Command.java @@ -60,18 +60,19 @@ 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(' '); - if (idx == -1) idx = ln.length(); + if (idx == -1) + idx = ln.length(); /* Grab command parts. */ full = ln; @@ -86,15 +87,14 @@ public class Command { /** * Removes up until the first occurrence of a particular string for the * remaining command, and returns the removed string. - * + * * By default, both the substring and the remaining text are trimmed * (leading/trailing spaces removed). - * + * * @param delm - * The delimiter to stop substringing at. - * - * @return The substring, or null if there is no occurrence of the - * delimiter. + * The delimiter to stop substringing at. + * + * @return The substring, or null if there is no occurrence of the delimiter. */ public String trimTo(String delm) { return trimTo(delm, true); @@ -103,19 +103,19 @@ public class Command { /** * Removes up until the first occurrence of a particular string for the * remaining command, and returns the removed string. - * + * * @param delm - * The delimiter to stop substringing at. + * The delimiter to stop substringing at. * @param doTrim - * Whether or not to trim the substring and remaining - * command (Remove leading/trailing spaces). - * - * @return The substring, or null if there is no occurrence of the - * delimiter. + * Whether or not to trim the substring and remaining command + * (Remove leading/trailing spaces). + * + * @return The substring, or null if there is no occurrence of the delimiter. */ public String trimTo(String delm, boolean doTrim) { int idx = remn.indexOf(delm); - if (idx == -1) return null; + if (idx == -1) + return null; String tmp = remn.substring(0, idx); remn = remn.substring(idx); @@ -129,15 +129,15 @@ public class Command { } /** - * Removes up until the first occurrence of a particular regex for the - * remaining command, and returns the removed string. - * + * Removes up until the first occurrence of a particular regex for the remaining + * command, and returns the removed string. + * * By default, both the substring and the remaining text are trimmed * (leading/trailing spaces removed). - * + * * @param rDelm - * The regex to stop substringing at. - * + * The regex to stop substringing at. + * * @return The string, up to the matched pattern. */ public String trimToRX(String rDelm) { @@ -145,15 +145,15 @@ public class Command { } /** - * Removes up until the first occurrence of a particular regex for the - * remaining command, and returns the removed string. - * + * Removes up until the first occurrence of a particular regex for the remaining + * command, and returns the removed string. + * * By default, both the substring and the remaining text are trimmed * (leading/trailing spaces removed). - * + * * @param delm - * The regex to stop substringing at. - * + * The regex to stop substringing at. + * * @return The string, up to the matched pattern. */ public String trimToRX(Pattern delm) { @@ -161,15 +161,15 @@ public class Command { } /** - * Removes up until the first occurrence of a particular regex for the - * remaining command, and returns the removed string. - * + * Removes up until the first occurrence of a particular regex for the remaining + * command, and returns the removed string. + * * @param rDelm - * The regex to stop substringing at. + * The regex to stop substringing at. * @param doTrim - * Whether or not to trim the substring and remaining - * command (Remove leading/trailing spaces). - * + * Whether or not to trim the substring and remaining command + * (Remove leading/trailing spaces). + * * @return The string, up to the matched pattern. */ public String trimToRX(String rDelm, boolean doTrim) { @@ -177,20 +177,21 @@ public class Command { } /** - * Removes up until the first occurrence of a particular regex for the - * remaining command, and returns the removed string. + * Removes up until the first occurrence of a particular regex for the remaining + * command, and returns the removed string. * * @param delm - * The regex to stop substringing at. + * The regex to stop substringing at. * @param doTrim - * Whether or not to trim the substring and remaining - * command (Remove leading/trailing spaces). - * + * Whether or not to trim the substring and remaining command + * (Remove leading/trailing spaces). + * * @return The string, up to the matched pattern. */ public String trimToRX(Pattern delm, boolean doTrim) { Matcher mat = delm.matcher(remn); - if (!mat.find()) return null; + if (!mat.find()) + return null; String tmp = remn.substring(0, mat.start()); remn = remn.substring(mat.end()); @@ -206,15 +207,14 @@ public class Command { /** * Removes up until the first occurrence of a particular string for the * remaining command, and returns the removed string. - * + * * By default, both the substring and the remaining text are trimmed * (leading/trailing spaces removed). - * + * * @param delm - * The delimiter to stop substringing at. - * - * @return The substring, or null if there is no occurrence of the - * delimiter. + * The delimiter to stop substringing at. + * + * @return The substring, or null if there is no occurrence of the delimiter. */ public String trimTo(char delm) { return trimTo(delm, true); @@ -223,19 +223,19 @@ public class Command { /** * Removes up until the first occurrence of a particular string for the * remaining command, and returns the removed string. - * + * * @param delm - * The delimiter to stop substringing at. + * The delimiter to stop substringing at. * @param doTrim - * Whether or not to trim the substring and remaining - * command (Remove leading/trailing spaces). - * - * @return The substring, or null if there is no occurrence of the - * delimiter. + * Whether or not to trim the substring and remaining command + * (Remove leading/trailing spaces). + * + * @return The substring, or null if there is no occurrence of the delimiter. */ public String trimTo(char delm, boolean doTrim) { int idx = remn.indexOf(delm); - if (idx == -1) return null; + if (idx == -1) + return null; String tmp = remn.substring(0, idx); remn = remn.substring(idx); @@ -250,7 +250,7 @@ public class Command { /** * Check if this command has text after its name. - * + * * @return Whether or not this command has text after its name. */ public boolean hasRemaining() { @@ -260,9 +260,8 @@ public class Command { /** * 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. + * 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. @@ -276,8 +275,10 @@ public class Command { */ public static Command fromString(String ln, int lno, String srcName) { /* Ignore blank lines and comments. */ - if (ln.equals("")) return null; - if (ln.startsWith("#")) return null; + if (ln.equals("")) + return null; + if (ln.startsWith("#")) + return null; /* Trim off comments part-way through the line. */ int idxHash = ln.indexOf('#'); @@ -289,14 +290,13 @@ public class Command { } /** - * Give an informational message about something in relation to this - * command. + * Give an informational message about something in relation to this 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) { @@ -313,7 +313,7 @@ public class Command { * * @param parms * The parameters for the warning message. - * + * * @return The formatted warning. */ public String warn(String warning, Object... parms) { @@ -326,11 +326,11 @@ public class Command { * Give an error about something in relation to this command. * * @param err - * The error message. + * 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) { 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 fc6e04a..d2595d6 100644 --- a/base/src/main/java/bjc/utils/cli/objects/DefineCLI.java +++ b/base/src/main/java/bjc/utils/cli/objects/DefineCLI.java @@ -41,8 +41,9 @@ public class DefineCLI { this(new HashMap<>(), new HashMap<>(), new HashMap<>(), new HashMap<>()); } - public DefineState(Map<String, UnaryOperator<String>> defines, Map<String, String> strings, - Map<String, String> formats, Map<String, Pattern> patterns) { + public DefineState(Map<String, UnaryOperator<String>> defines, + Map<String, String> strings, Map<String, String> formats, + Map<String, Pattern> patterns) { this.defines = defines; this.strings = strings; @@ -63,14 +64,14 @@ public class DefineCLI { /** * Main method - * + * * @param args - * CLI 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); } } @@ -79,23 +80,25 @@ 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); } @@ -105,15 +108,15 @@ public class DefineCLI { /** * Handle a command - * + * * @param com - * The command to handle + * The command to handle * @param interactive - * Whether or not our I/O stream is interactive + * Whether or not our I/O stream is interactive * @return The status of the executed command. */ public CommandStatus handleCommand(Command com, boolean interactive) { - switch(com.name) { + switch (com.name) { case "def-string": return defString(com); case "def-format": @@ -129,7 +132,7 @@ public class DefineCLI { private CommandStatus defString(Command com) { List<String> arguments = StringUtils.processArguments(com.remn); - if(arguments.size() < 1) { + if (arguments.size() < 1) { LOGGER.severe(com.error( "def-string expects at least one argument: the name of the string to bind")); return FAIL; @@ -138,14 +141,14 @@ public class DefineCLI { String name = arguments.get(0); String strang; - if(arguments.size() < 2) { + if (arguments.size() < 2) { LOGGER.warning(com.warn("Binding empty string to name '%s'\n", name)); strang = ""; } else { strang = arguments.get(1); } - if(stat.strings.containsKey(name)) { + if (stat.strings.containsKey(name)) { LOGGER.warning(com.warn("Shadowing string '%s'\n", name)); } @@ -157,7 +160,7 @@ public class DefineCLI { private CommandStatus defFormat(Command com) { List<String> arguments = StringUtils.processArguments(com.remn); - if(arguments.size() < 1) { + if (arguments.size() < 1) { LOGGER.severe(com.error( "def-format expects at least one argument: the name of the format to bind")); return FAIL; @@ -166,14 +169,14 @@ public class DefineCLI { String name = arguments.get(0); String fmt; - if(arguments.size() < 2) { + if (arguments.size() < 2) { LOGGER.warning(com.warn("Binding empty format to name '%s'\n", name)); fmt = ""; } else { fmt = arguments.get(1); } - if(stat.formats.containsKey(name)) { + if (stat.formats.containsKey(name)) { LOGGER.warning(com.warn("Shadowing format '%s'\n", name)); } @@ -185,20 +188,20 @@ public class DefineCLI { private CommandStatus bindFormat(Command com) { List<String> strings = StringUtils.processArguments(com.remn); - if(strings.size() < 2) { + 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)) { + 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)) { + if (stat.strings.containsKey(bindName)) { LOGGER.warning(com.warn("Shadowing string '%s'", bindName)); } @@ -209,13 +212,13 @@ public class DefineCLI { itr.next(); itr.next(); - while(itr.hasNext()) { + while (itr.hasNext()) { String name = itr.next(); - if(name.startsWith("$")) { + if (name.startsWith("$")) { String varName = name.substring(1); - if(stat.strings.containsKey(varName)) { + if (stat.strings.containsKey(varName)) { fillIns.add(stat.strings.get(varName)); } else { LOGGER.severe(com.error("Unknown string '%s'", varName)); @@ -226,15 +229,16 @@ public class DefineCLI { } } - // CLFormatter fmt = new CLFormatter(); + // CLFormatter fmt = new CLFormatter(); String formatted = ""; - // try { - // formatted = fmt.formatString(stat.formats.get(formatName), fillIns); - // } catch (IOException ioex) { - // LOGGER.severe(com.error("IOException formatting string: %s", ioex.getMessage())); - // return FAIL; - // } + // try { + // formatted = fmt.formatString(stat.formats.get(formatName), fillIns); + // } catch (IOException ioex) { + // LOGGER.severe(com.error("IOException formatting string: %s", + // ioex.getMessage())); + // return FAIL; + // } stat.strings.put(bindName, formatted); diff --git a/base/src/main/java/bjc/utils/cli/objects/DelimSplitterCLI.java b/base/src/main/java/bjc/utils/cli/objects/DelimSplitterCLI.java index 27d74c8..59822e4 100644 --- a/base/src/main/java/bjc/utils/cli/objects/DelimSplitterCLI.java +++ b/base/src/main/java/bjc/utils/cli/objects/DelimSplitterCLI.java @@ -83,8 +83,10 @@ public class DelimSplitterCLI { /* * Handle a input command. */ - private void handleCommand(final String inp, final Scanner scn, final boolean isInteractive) { - if (inp.equals("")) return; + private void handleCommand(final String inp, final Scanner scn, + final boolean isInteractive) { + if (inp.equals("")) + return; int idx = inp.indexOf(' '); @@ -116,14 +118,15 @@ public class DelimSplitterCLI { case "splitter-add": split.addSimpleDelimiters(argArray); if (verbose) { - System.out.println("Added delimiters " + StringUtils.toEnglishList(argArray, true)); + System.out.println( + "Added delimiters " + StringUtils.toEnglishList(argArray, true)); } break; case "splitter-addmulti": split.addMultiDelimiters(argArray); if (verbose) { - System.out.println( - "Added multi-delimiters " + StringUtils.toEnglishList(argArray, true)); + System.out.println("Added multi-delimiters " + + StringUtils.toEnglishList(argArray, true)); } break; case "splitter-addmatch": @@ -150,7 +153,8 @@ public class DelimSplitterCLI { dlm.addGroup(groups.get(arg)); } if (verbose) { - System.out.println("Added groups " + StringUtils.toEnglishList(argArray, true)); + System.out.println( + "Added groups " + StringUtils.toEnglishList(argArray, true)); } break; case "delims-setinitial": @@ -176,7 +180,8 @@ public class DelimSplitterCLI { groups.put(arg, new DelimiterGroup<>(arg)); } if (verbose) { - System.out.println("Created groups " + StringUtils.toEnglishList(argArray, true)); + System.out.println( + "Created groups " + StringUtils.toEnglishList(argArray, true)); } break; case "delimgroups-edit": @@ -238,14 +243,16 @@ public class DelimSplitterCLI { } catch (final FileNotFoundException fnfex) { System.out.println("Couldn't find file '" + args + "'"); } catch (final IOException ioex) { - System.out.println("I/O error with file '" + args + "'\nCause: " + ioex.getMessage()); + System.out.println( + "I/O error with file '" + args + "'\nCause: " + ioex.getMessage()); } } /* * Handle editing a group. */ - private void handleEditGroup(final String arg, final Scanner scn, final boolean isInteractive) { + private void handleEditGroup(final String arg, final Scanner scn, + final boolean isInteractive) { if (!groups.containsKey(arg)) { System.out.println("No group named '" + arg + "'"); return; @@ -306,8 +313,8 @@ public class DelimSplitterCLI { case "add-implied-subgroup": group.implySubgroup(argArray[0], argArray[1]); if (verbose) { - System.out.printf("Made closer '%s' imply a '%s' subgroup\n", argArray[0], - argArray[1]); + System.out.printf("Made closer '%s' imply a '%s' subgroup\n", + argArray[0], argArray[1]); } break; case "add-opener": @@ -320,14 +327,15 @@ public class DelimSplitterCLI { case "add-reopener": group.addPredOpener(new RegexOpener(argArray[0], argArray[1])); if (verbose) { - System.out.printf("Added regex '%s' as opener for '%s'\n", argArray[1], - argArray[0]); + System.out.printf("Added regex '%s' as opener for '%s'\n", + argArray[1], argArray[0]); } break; case "add-recloser": group.addPredCloser(new RegexCloser(argArray[0])); if (verbose) { - System.out.printf("Added parameterized string '%s' as closer\n", argArray[0]); + System.out.printf("Added parameterized string '%s' as closer\n", + argArray[0]); } break; case "debug": @@ -355,8 +363,8 @@ public class DelimSplitterCLI { printDelimSeq(res); } catch (final DelimiterException dex) { - System.out.println("Expression '" + args + "' isn't properly delimited.\n\tCause: " - + dex.getMessage()); + System.out.println("Expression '" + args + + "' isn't properly delimited.\n\tCause: " + dex.getMessage()); } } @@ -392,7 +400,8 @@ public class DelimSplitterCLI { strings = new FunctionalList<>(tks); } try { - final ITree<String> delim = dlm.delimitSequence(strings.toArray(new String[0])); + final ITree<String> delim + = dlm.delimitSequence(strings.toArray(new String[0])); printDelimSeq(delim); } catch (final DelimiterException dex) { @@ -420,7 +429,7 @@ public class DelimSplitterCLI { } private void intPrintDelimTree(final ITree<String> tree, final StringBuilder sb) { - tree.doForChildren((child) -> { + tree.doForChildren(child -> { intPrintDelimNode(child, sb); }); } @@ -462,7 +471,7 @@ public class DelimSplitterCLI { * Main method * * @param args - * Unused CLI args. + * Unused CLI args. */ public static void main(final String[] args) { final DelimSplitterCLI tst = new DelimSplitterCLI(); |
