diff --git a/src/main/java/net/knarcraft/ffmpegconverter/utility/Parser.java b/src/main/java/net/knarcraft/ffmpegconverter/utility/Parser.java index e13c6f2..93e9321 100644 --- a/src/main/java/net/knarcraft/ffmpegconverter/utility/Parser.java +++ b/src/main/java/net/knarcraft/ffmpegconverter/utility/Parser.java @@ -1,10 +1,8 @@ package net.knarcraft.ffmpegconverter.utility; import net.knarcraft.ffmpegconverter.parser.ConverterArgument; -import net.knarcraft.ffmpegconverter.parser.ConverterArgumentValue; import java.util.ArrayList; -import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -18,86 +16,96 @@ public final class Parser { } /** - * This function parses command inputs into understandable converter instructions - * @param tokens

A list of tokens containing all arguments

+ * This function parses the input to understandable converter instructions + * @param input

The input string to parse.

+ * @param validArguments

All arguments which are considered valid.

* @return

A map with all parsed arguments.

*/ - private static Map parse(List tokens) { - String[] types = {"animeconverter", "audioconverter", "videoconverter"}; - ConverterArgument[] commonArgs = { - new ConverterArgument("recursions", 'r', true, ConverterArgumentValue.INT), - new ConverterArgument("infile", 'i', true, ConverterArgumentValue.STRING) - }; - ConverterArgument[] animeArgs = { - new ConverterArgument("audiolang", 'a', true, ConverterArgumentValue.COMMA_SEPARATED_LIST), - new ConverterArgument("subtitlelang", 's', true, ConverterArgumentValue.COMMA_SEPARATED_LIST), - new ConverterArgument("tostereo", 't', false, ConverterArgumentValue.BOOLEAN), - new ConverterArgument("preventpartialsubtitles", 'p', false, ConverterArgumentValue.BOOLEAN) - }; - ConverterArgument[] audioVideoArgs = { - new ConverterArgument("-outext", 'o', true, - ConverterArgumentValue.STRING) - }; - String type = tokens.remove(0).toLowerCase(); - if (!ListUtil.listContains(types, s -> s.equals(type))) { - throw new IllegalArgumentException("Unknown converter type chosen."); - } - if (tokens.size() < 2) { - throw new IllegalArgumentException("No file/folder path in argument."); - } - List converterArguments; - switch (type) { - case "animeconverter": - converterArguments = new ArrayList<>(Arrays.asList(ListUtil.concatenate(commonArgs, animeArgs))); - break; - case "audioconverter": - case "videoconverter": - converterArguments = new ArrayList<>(Arrays.asList(ListUtil.concatenate(commonArgs, audioVideoArgs))); - break; - default: - throw new IllegalArgumentException("Unknown converter type chosen."); - } + static Map parse(String input, List validArguments) { + return parse(tokenize(input), validArguments); + } + /** + * This function parses command inputs into understandable converter instructions + * + * @param tokens

A list of tokens containing all arguments

+ * @param validArguments

A list of arguments which are considered valid.

+ * @return

A map with all parsed arguments.

+ */ + private static Map parse(List tokens, List validArguments) { Map parsedArguments = new HashMap<>(); - //TODO: Refactor and test this function - while (!tokens.isEmpty()) { - String currentToken = tokens.remove(0); - List foundArguments; - if (currentToken.startsWith("--")) { - String argumentName = currentToken.substring(2); - foundArguments = ListUtil.getMatching(converterArguments, (item) -> item.getName().equals(argumentName)); - } else if (currentToken.startsWith("-")) { - char argumentShorthand = currentToken.substring(1).charAt(0); - foundArguments = ListUtil.getMatching(converterArguments, (item) -> item.getShorthand() == argumentShorthand); - } else { - throw new IllegalArgumentException("Unexpected value when not given an argument."); - } - if (foundArguments.isEmpty()) { - throw new IllegalArgumentException(String.format("Invalid argument %s encountered.", currentToken)); - } - ConverterArgument foundArgument = foundArguments.get(0); - String argumentValue = tokens.get(0); - boolean valueIsArgument = argumentValue.startsWith("-"); - - if (valueIsArgument) { - if (foundArgument.isValueRequired()) { - throw new IllegalArgumentException(String.format("Argument %s requires a value, but no value was given.", foundArgument.getName())); - } else { - parsedArguments.put(foundArgument.getName(), "true"); - } - } else { - String value = tokens.remove(0); - if (!foundArgument.testArgumentValue(value)) { - throw new IllegalArgumentException(String.format("Invalid value %s for argument %s.", value, foundArgument.getName())); - } - parsedArguments.put(foundArgument.getName(), value); - } + parseArgument(tokens, validArguments, parsedArguments); } return parsedArguments; } + /** + * Parses the first found token as an argument + * + * @param tokens

The tokens to parse.

+ * @param converterArguments

A list of all the valid arguments in existence.

+ * @param parsedArguments

The map to store the parsed argument to.

+ */ + private static void parseArgument(List tokens, List converterArguments, Map parsedArguments) { + String currentToken = tokens.remove(0); + List foundArguments; + + if (currentToken.startsWith("--")) { + String argumentName = currentToken.substring(2); + foundArguments = ListUtil.getMatching(converterArguments, (item) -> item.getName().equals(argumentName)); + } else if (currentToken.startsWith("-")) { + char argumentShorthand = currentToken.substring(1).charAt(0); + foundArguments = ListUtil.getMatching(converterArguments, (item) -> item.getShorthand() == argumentShorthand); + } else { + throw new IllegalArgumentException("Unexpected value when not given an argument."); + } + + if (foundArguments.isEmpty()) { + throw new IllegalArgumentException(String.format("Invalid argument %s encountered.", currentToken)); + } + + ConverterArgument foundArgument = foundArguments.get(0); + storeArgumentValue(tokens, foundArgument, parsedArguments); + } + + /** + * Stores the value of a found argument to parsed arguments + * + * @param tokens

The token list to parse.

+ * @param foundArgument

The found argument to store.

+ * @param parsedArguments

The map to store parsed arguments to.

+ */ + private static void storeArgumentValue(List tokens, ConverterArgument foundArgument, Map parsedArguments) { + String argumentValue; + if (tokens.isEmpty()) { + argumentValue = ""; + } else { + argumentValue = tokens.get(0); + } + boolean valueIsArgument = argumentValue.startsWith("-"); + + if (valueIsArgument) { + if (foundArgument.isValueRequired()) { + throw new IllegalArgumentException(String.format("Argument %s requires a value, but no value was given.", foundArgument.getName())); + } else { + parsedArguments.put(foundArgument.getName(), "true"); + } + } else { + String value; + if (tokens.isEmpty()) { + value = ""; + } else { + value = tokens.remove(0); + } + if (!foundArgument.testArgumentValue(value)) { + throw new IllegalArgumentException(String.format("Invalid value %s for argument %s.", value, foundArgument.getName())); + } + parsedArguments.put(foundArgument.getName(), value); + } + } + /** * Tokenizes a string *