diff --git a/src/main/java/net/knarcraft/ffmpegconverter/utility/Parser.java b/src/main/java/net/knarcraft/ffmpegconverter/utility/Parser.java index 5c5fe37..d00179a 100644 --- a/src/main/java/net/knarcraft/ffmpegconverter/utility/Parser.java +++ b/src/main/java/net/knarcraft/ffmpegconverter/utility/Parser.java @@ -4,6 +4,7 @@ import net.knarcraft.ffmpegconverter.parser.ConverterArgument; import net.knarcraft.ffmpegconverter.parser.ConverterArgumentValue; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; /** @@ -22,29 +23,58 @@ public final class Parser { private static void parse(List tokens) { String[] types = {"animeconverter", "audioconverter", "videoconverter"}; ConverterArgument[] commonArgs = { - new ConverterArgument("-recursions", true, ConverterArgumentValue.INT) + 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[] audioArgs = { - new ConverterArgument("-outext", true, - ConverterArgumentValue.SINGLE_VALUE) + ConverterArgument[] audioVideoArgs = { + new ConverterArgument("-outext", 'o', true, + ConverterArgumentValue.STRING) }; - ConverterArgument[] videoArgs = { - new ConverterArgument("-outext", true, - ConverterArgumentValue.SINGLE_VALUE) - }; - String type = tokens.get(0).toLowerCase(); + 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."); } - for (int i = 1; i < tokens.size() - 1; i++) { - //TODO: Find the type of argument and check the value - //TODO: Find an executable way to represent the chain of commands parsed + 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."); + } + 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("-")) { + String argumentShorthand = currentToken.substring(1); + foundArguments = ListUtil.getMatching(converterArguments, (item) -> item.getName().equals(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); + //TODO: Check if the value is an argument, and if not consume the value + //TODO: If the value is an argument, but a value is necessary, throw an error + //TODO: Store the found parameter values into a hashmap and send it to the converter as input } }