Update libAC to include an option parser

This commit is contained in:
zontreck 2023-12-04 22:57:42 -07:00
parent ef08583850
commit 4ce9c62e41
3 changed files with 95 additions and 0 deletions

View file

@ -0,0 +1,35 @@
package dev.zontreck.ariaslib.args;
public class Argument {
public boolean isSet = false;
public boolean hasValue = false;
public String name;
private String value;
/**
* Initializes a boolean only command line toggle
*
* @param name The option name
*/
public Argument(String name) {
this(name, "");
}
/**
* Initializes the argument with a value
*
* @param name The name of the command line option
* @param value The value
*/
public Argument(String name, String value) {
if (value != "") hasValue = true;
isSet = true;
this.name = name;
this.value = value;
}
public String getValue() {
if (hasValue) return value;
else throw new IllegalArgumentException("No value");
}
}

View file

@ -0,0 +1,26 @@
package dev.zontreck.ariaslib.args;
import java.util.HashMap;
import java.util.Map;
public class Arguments {
private Map<String, Argument> args = new HashMap<>();
public void setArg(Argument arg) {
args.put(arg.name, arg);
}
public String getArg(String argumentName, String defaultValue) {
if (!args.containsKey(argumentName)) {
return defaultValue;
}
Argument arg = args.get(argumentName);
if (arg.hasValue) {
return arg.getValue();
} else {
return defaultValue;
}
}
}

View file

@ -0,0 +1,34 @@
package dev.zontreck.ariaslib.args;
public class ArgumentsParser {
public static Arguments parseArguments(String[] args) {
Arguments arguments = new Arguments();
for (int i = 0; i < args.length; i++) {
Argument arg = parseArgument(args[i]);
if (arg != null) {
if (arg.hasValue && i + 1 < args.length) {
arguments.setArg(arg);
i++; // Skip next value as it's the argument's value
} else {
arguments.setArg(arg); // Argument without value, default to empty string
}
}
}
return arguments;
}
public static Argument parseArgument(String arg) {
if (arg.startsWith("--")) {
String[] parts = arg.split("=", 2);
if (parts.length == 1) {
return new Argument(parts[0].substring(2));
} else {
return new Argument(parts[0].substring(2), parts[1]);
}
} else {
return null; // Not a valid argument format
}
}
}