From 8b0e394ac8de7e9805a9e106a5e18e927e2df7d9 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Thu, 21 Jan 2021 03:57:03 +0100 Subject: [PATCH] Finishes the parser. It should theoretically work now --- .../ffmpegconverter/utility/Parser.java | 34 +++++++++++++++---- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/src/main/java/net/knarcraft/ffmpegconverter/utility/Parser.java b/src/main/java/net/knarcraft/ffmpegconverter/utility/Parser.java index d00179a..e13c6f2 100644 --- a/src/main/java/net/knarcraft/ffmpegconverter/utility/Parser.java +++ b/src/main/java/net/knarcraft/ffmpegconverter/utility/Parser.java @@ -5,7 +5,9 @@ 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; /** * A class to help with command parsing @@ -17,10 +19,10 @@ public final class Parser { /** * This function parses command inputs into understandable converter instructions - * * @param tokens

A list of tokens containing all arguments

+ * @return

A map with all parsed arguments.

*/ - private static void parse(List tokens) { + private static Map parse(List tokens) { String[] types = {"animeconverter", "audioconverter", "videoconverter"}; ConverterArgument[] commonArgs = { new ConverterArgument("recursions", 'r', true, ConverterArgumentValue.INT), @@ -55,6 +57,11 @@ public final class Parser { default: throw new IllegalArgumentException("Unknown converter type chosen."); } + + Map parsedArguments = new HashMap<>(); + + //TODO: Refactor and test this function + while (!tokens.isEmpty()) { String currentToken = tokens.remove(0); List foundArguments; @@ -62,8 +69,8 @@ public final class Parser { 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)); + 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."); } @@ -72,10 +79,23 @@ public final class Parser { } 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 + 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); + } } + return parsedArguments; } /**