Finishes the parser. It should theoretically work now
All checks were successful
KnarCraft/FFmpegConvert/pipeline/head This commit looks good
All checks were successful
KnarCraft/FFmpegConvert/pipeline/head This commit looks good
This commit is contained in:
parent
8fb32402c7
commit
8b0e394ac8
@ -5,7 +5,9 @@ import net.knarcraft.ffmpegconverter.parser.ConverterArgumentValue;
|
|||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A class to help with command parsing
|
* A class to help with command parsing
|
||||||
@ -17,10 +19,10 @@ public final class Parser {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* This function parses command inputs into understandable converter instructions
|
* This function parses command inputs into understandable converter instructions
|
||||||
*
|
|
||||||
* @param tokens <p>A list of tokens containing all arguments</p>
|
* @param tokens <p>A list of tokens containing all arguments</p>
|
||||||
|
* @return <p>A map with all parsed arguments.</p>
|
||||||
*/
|
*/
|
||||||
private static void parse(List<String> tokens) {
|
private static Map<String, String> parse(List<String> tokens) {
|
||||||
String[] types = {"animeconverter", "audioconverter", "videoconverter"};
|
String[] types = {"animeconverter", "audioconverter", "videoconverter"};
|
||||||
ConverterArgument[] commonArgs = {
|
ConverterArgument[] commonArgs = {
|
||||||
new ConverterArgument("recursions", 'r', true, ConverterArgumentValue.INT),
|
new ConverterArgument("recursions", 'r', true, ConverterArgumentValue.INT),
|
||||||
@ -55,6 +57,11 @@ public final class Parser {
|
|||||||
default:
|
default:
|
||||||
throw new IllegalArgumentException("Unknown converter type chosen.");
|
throw new IllegalArgumentException("Unknown converter type chosen.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Map<String, String> parsedArguments = new HashMap<>();
|
||||||
|
|
||||||
|
//TODO: Refactor and test this function
|
||||||
|
|
||||||
while (!tokens.isEmpty()) {
|
while (!tokens.isEmpty()) {
|
||||||
String currentToken = tokens.remove(0);
|
String currentToken = tokens.remove(0);
|
||||||
List<ConverterArgument> foundArguments;
|
List<ConverterArgument> foundArguments;
|
||||||
@ -62,8 +69,8 @@ public final class Parser {
|
|||||||
String argumentName = currentToken.substring(2);
|
String argumentName = currentToken.substring(2);
|
||||||
foundArguments = ListUtil.getMatching(converterArguments, (item) -> item.getName().equals(argumentName));
|
foundArguments = ListUtil.getMatching(converterArguments, (item) -> item.getName().equals(argumentName));
|
||||||
} else if (currentToken.startsWith("-")) {
|
} else if (currentToken.startsWith("-")) {
|
||||||
String argumentShorthand = currentToken.substring(1);
|
char argumentShorthand = currentToken.substring(1).charAt(0);
|
||||||
foundArguments = ListUtil.getMatching(converterArguments, (item) -> item.getName().equals(argumentShorthand));
|
foundArguments = ListUtil.getMatching(converterArguments, (item) -> item.getShorthand() == argumentShorthand);
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("Unexpected value when not given an argument.");
|
throw new IllegalArgumentException("Unexpected value when not given an argument.");
|
||||||
}
|
}
|
||||||
@ -72,10 +79,23 @@ public final class Parser {
|
|||||||
}
|
}
|
||||||
ConverterArgument foundArgument = foundArguments.get(0);
|
ConverterArgument foundArgument = foundArguments.get(0);
|
||||||
String argumentValue = tokens.get(0);
|
String argumentValue = tokens.get(0);
|
||||||
//TODO: Check if the value is an argument, and if not consume the value
|
boolean valueIsArgument = argumentValue.startsWith("-");
|
||||||
//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
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user