Attempt to fix Default Arguments not being parsed.

This commit is contained in:
Zontreck 2024-02-10 16:34:31 -07:00
parent c03062e4da
commit 614a7f56f9
3 changed files with 53 additions and 4 deletions

View file

@ -1,6 +1,7 @@
package dev.zontreck.ariaslib.args;
public abstract class Argument<T> {
public abstract class Argument<T> implements Cloneable
{
public boolean hasValue = false;
public String name;
@ -31,4 +32,30 @@ public abstract class Argument<T> {
public T getValue() throws IllegalArgumentException {
throw new IllegalArgumentException("No value");
}
@Override
public Argument<T> clone() {
Argument<T> arg = null;
switch (getType())
{
case LONG -> {
arg = (Argument<T>) new LongArgument(name, (long)getValue());
break;
}
case STRING -> {
arg = (Argument<T>) new StringArgument(name, (String)getValue());
break;
}
case BOOLEAN -> {
arg = (Argument<T>) new BooleanArgument(name);
break;
}
case INTEGER -> {
arg = (Argument<T>) new IntegerArgument(name, (int)getValue());
break;
}
}
return arg;
}
}

View file

@ -3,9 +3,14 @@ package dev.zontreck.ariaslib.args;
import java.util.HashMap;
import java.util.Map;
public class Arguments {
private Map<String, Argument> args = new HashMap<>();
public class Arguments implements Cloneable
{
private Map<String, Argument<?>> args = new HashMap<>();
/**
* Set the argument in the args list
* @param arg
*/
public void setArg(Argument arg) {
args.put(arg.name, arg);
}
@ -44,4 +49,15 @@ public class Arguments {
return getArg(argName).hasValue;
} else throw new IllegalArgumentException(("No such argument"));
}
@Override
public Arguments clone() {
Arguments arg = new Arguments();
for(Map.Entry<String, Argument<?>> entry : args.entrySet())
{
arg.setArg(entry.getValue().clone());
}
return arg;
}
}

View file

@ -2,8 +2,14 @@ package dev.zontreck.ariaslib.args;
public class ArgumentsParser {
/**
* Parses and returns the arguments list with keeping defaults in mind
* @param args
* @param defaults
* @return Arguments with defaults set
*/
public static Arguments parseArguments(String[] args, Arguments defaults) {
Arguments arguments = new Arguments();
Arguments arguments = defaults.clone();
for (int i = 0; i < args.length; i++) {
Argument arg = parseArgument(args[i]);
if (arg != null) {