Don't jarjar anymore.
This commit is contained in:
parent
44eb9d1d9c
commit
307b3427e8
77 changed files with 6359 additions and 15 deletions
106
src/main/java/dev/zontreck/ariaslib/args/Argument.java
Normal file
106
src/main/java/dev/zontreck/ariaslib/args/Argument.java
Normal file
|
@ -0,0 +1,106 @@
|
|||
package dev.zontreck.ariaslib.args;
|
||||
|
||||
import dev.zontreck.ariaslib.exceptions.WrongArgumentTypeException;
|
||||
|
||||
public abstract class Argument<T> implements Cloneable
|
||||
{
|
||||
public boolean hasValue = false;
|
||||
public String name;
|
||||
|
||||
|
||||
/**
|
||||
* Initializes a boolean only command line toggle
|
||||
*
|
||||
* @param name The option name
|
||||
*/
|
||||
public Argument(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the current argument's type
|
||||
*
|
||||
* @return The argument type!
|
||||
*/
|
||||
public abstract ArgumentType getType();
|
||||
|
||||
|
||||
/**
|
||||
* Retrieves the value.
|
||||
*
|
||||
* @return The value
|
||||
* @throws IllegalArgumentException When there is no value
|
||||
*/
|
||||
public T getValue() throws IllegalArgumentException {
|
||||
throw new IllegalArgumentException("No value");
|
||||
}
|
||||
|
||||
/**
|
||||
* Directly cast to the Argument type Long
|
||||
* @return
|
||||
* @throws WrongArgumentTypeException Throws when type does not match
|
||||
*/
|
||||
public LongArgument getAsLong() throws WrongArgumentTypeException {
|
||||
if(this instanceof LongArgument)
|
||||
{
|
||||
return (LongArgument) this;
|
||||
}
|
||||
|
||||
throw new WrongArgumentTypeException();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Directly cast to the Argument type String
|
||||
* @return
|
||||
* @throws WrongArgumentTypeException Throws when type does not match
|
||||
*/
|
||||
public StringArgument getAsString() throws WrongArgumentTypeException {
|
||||
if(this instanceof StringArgument)
|
||||
{
|
||||
return (StringArgument) this;
|
||||
}
|
||||
|
||||
throw new WrongArgumentTypeException();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Directly cast to the Argument type Integer
|
||||
* @return
|
||||
* @throws WrongArgumentTypeException Throws when type does not match
|
||||
*/
|
||||
public IntegerArgument getAsInteger() throws WrongArgumentTypeException
|
||||
{
|
||||
if(this instanceof IntegerArgument)
|
||||
{
|
||||
return (IntegerArgument) this;
|
||||
}
|
||||
|
||||
throw new WrongArgumentTypeException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Argument<T> clone() {
|
||||
Argument<T> arg = null;
|
||||
try{
|
||||
|
||||
if(getType() == ArgumentType.LONG)
|
||||
{
|
||||
arg = (Argument<T>) new LongArgument(name, getAsLong().getValue());
|
||||
} else if(getType() == ArgumentType.STRING)
|
||||
{
|
||||
arg = (Argument<T>) new StringArgument(name, getAsString().getValue());
|
||||
} else if(getType() == ArgumentType.BOOLEAN) {
|
||||
arg = (Argument<T>) new BooleanArgument(name);
|
||||
} else if(getType() == ArgumentType.INTEGER){
|
||||
arg = (Argument<T>) new IntegerArgument(name, getAsInteger().getValue());
|
||||
}
|
||||
}catch (WrongArgumentTypeException ex)
|
||||
{
|
||||
ex.printStackTrace();
|
||||
}
|
||||
|
||||
return arg;
|
||||
}
|
||||
}
|
25
src/main/java/dev/zontreck/ariaslib/args/ArgumentType.java
Normal file
25
src/main/java/dev/zontreck/ariaslib/args/ArgumentType.java
Normal file
|
@ -0,0 +1,25 @@
|
|||
package dev.zontreck.ariaslib.args;
|
||||
|
||||
public enum ArgumentType {
|
||||
/**
|
||||
* This indicates a string. It may possibly have a default value
|
||||
*/
|
||||
STRING,
|
||||
/**
|
||||
* This indicates a boolean
|
||||
* <p>
|
||||
* This may have a default value, which initiates a BooleanArgument
|
||||
*/
|
||||
BOOLEAN,
|
||||
|
||||
/**
|
||||
* This indicates a data type of integer
|
||||
* The type of integer arg is determined by the length of the integer.
|
||||
*/
|
||||
INTEGER,
|
||||
|
||||
/**
|
||||
* This is a long value, which can hold larger values than a integer. The type of integer arg is determined by the length of the integer.
|
||||
*/
|
||||
LONG
|
||||
}
|
63
src/main/java/dev/zontreck/ariaslib/args/Arguments.java
Normal file
63
src/main/java/dev/zontreck/ariaslib/args/Arguments.java
Normal file
|
@ -0,0 +1,63 @@
|
|||
package dev.zontreck.ariaslib.args;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks for and returns the argument
|
||||
*
|
||||
* @param argName The argument's name
|
||||
* @return The argument instance, or null if not found
|
||||
*/
|
||||
public Argument getArg(String argName) {
|
||||
if (hasArg(argName))
|
||||
return args.get(argName);
|
||||
else return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the argument is set.
|
||||
*
|
||||
* @param argName The argument's name to check for
|
||||
* @return True if the argument is present. This does not indicate if the argument has a value
|
||||
*/
|
||||
public boolean hasArg(String argName) {
|
||||
return args.containsKey(argName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks the argument (if it exists), for whether a value is set
|
||||
*
|
||||
* @param argName This is the argument name
|
||||
* @return True if a value is set
|
||||
* @throws IllegalArgumentException If there is no such argument
|
||||
*/
|
||||
public boolean argHasValue(String argName) throws IllegalArgumentException {
|
||||
if (hasArg(argName)) {
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package dev.zontreck.ariaslib.args;
|
||||
|
||||
public class ArgumentsBuilder {
|
||||
private Arguments args = new Arguments();
|
||||
|
||||
public static ArgumentsBuilder builder() {
|
||||
return new ArgumentsBuilder();
|
||||
}
|
||||
|
||||
public ArgumentsBuilder withArgument(Argument arg) {
|
||||
args.setArg(arg);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Arguments build()
|
||||
{
|
||||
return args;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,99 @@
|
|||
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 = defaults.clone();
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
Argument arg = parseArgument(args[i]);
|
||||
if (arg != null) {
|
||||
Argument defaultArg = null;
|
||||
if (defaults.hasArg(arg.name)) {
|
||||
defaultArg = defaults.getArg(arg.name);
|
||||
}
|
||||
|
||||
if (!arg.hasValue) {
|
||||
if (defaultArg != null) {
|
||||
arg = defaultArg;
|
||||
}
|
||||
}
|
||||
|
||||
arguments.setArg(arg);
|
||||
}
|
||||
}
|
||||
return arguments;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses and returns an argument with a type set
|
||||
* @param arg The argument to parse with double tack
|
||||
* @return Typed Argument
|
||||
* @throws IllegalArgumentException when no type matches and the input is malformed in some way
|
||||
*/
|
||||
public static Argument parseArgument(String arg) {
|
||||
if (arg.startsWith("--")) {
|
||||
String[] parts = arg.split("=");
|
||||
String name = getNamePart(parts[0]);
|
||||
if (parts.length == 1) {
|
||||
return new BooleanArgument(name);
|
||||
|
||||
} else if (parts.length == 2) {
|
||||
String value = getValuePart(parts[1]);
|
||||
ArgumentType typeOfArg = getArgumentType(value);
|
||||
switch(typeOfArg)
|
||||
{
|
||||
case INTEGER:
|
||||
{
|
||||
return new IntegerArgument(name, Integer.parseInt(value));
|
||||
}
|
||||
case LONG:
|
||||
{
|
||||
return new LongArgument(name, Long.parseLong(value));
|
||||
}
|
||||
case BOOLEAN:
|
||||
{
|
||||
return new BooleanArgument(name);
|
||||
}
|
||||
default:
|
||||
{
|
||||
return new StringArgument(name, value);
|
||||
}
|
||||
}
|
||||
} else throw new IllegalArgumentException("The argument is malformed. Remember to use --arg=val, or --toggle");
|
||||
} else {
|
||||
throw new IllegalArgumentException("Not a valid argument format");
|
||||
}
|
||||
}
|
||||
|
||||
protected static String getNamePart(String entry) {
|
||||
return entry.substring(2);
|
||||
}
|
||||
|
||||
protected static String getValuePart(String entry) {
|
||||
return entry;
|
||||
}
|
||||
|
||||
protected static ArgumentType getArgumentType(String input) {
|
||||
try {
|
||||
Integer.parseInt(input);
|
||||
return ArgumentType.INTEGER;
|
||||
}catch(Exception e){}
|
||||
try{
|
||||
Long.parseLong(input);
|
||||
return ArgumentType.LONG;
|
||||
}catch(Exception E){
|
||||
|
||||
}
|
||||
|
||||
if(input.isEmpty())
|
||||
return ArgumentType.BOOLEAN;
|
||||
else return ArgumentType.STRING;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
package dev.zontreck.ariaslib.args;
|
||||
|
||||
public class BooleanArgument extends Argument<Boolean> {
|
||||
private boolean value;
|
||||
|
||||
/**
|
||||
* Initializes a boolean only command line toggle
|
||||
*
|
||||
* @param name The option name
|
||||
*/
|
||||
public BooleanArgument(String name) {
|
||||
super(name);
|
||||
|
||||
this.hasValue = true;
|
||||
this.value = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArgumentType getType() {
|
||||
return ArgumentType.BOOLEAN;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "BooleanArgument{" +
|
||||
name + "=true" +
|
||||
'}';
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package dev.zontreck.ariaslib.args;
|
||||
|
||||
public class IntegerArgument extends Argument<Integer> {
|
||||
private int value;
|
||||
|
||||
public IntegerArgument(String name, int value) {
|
||||
super(name);
|
||||
this.hasValue = true;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArgumentType getType() {
|
||||
return ArgumentType.INTEGER;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "IntegerArgument{" +
|
||||
name + "=" +
|
||||
value +
|
||||
'}';
|
||||
}
|
||||
}
|
30
src/main/java/dev/zontreck/ariaslib/args/LongArgument.java
Normal file
30
src/main/java/dev/zontreck/ariaslib/args/LongArgument.java
Normal file
|
@ -0,0 +1,30 @@
|
|||
package dev.zontreck.ariaslib.args;
|
||||
|
||||
public class LongArgument extends Argument<Long> {
|
||||
private long value;
|
||||
|
||||
public LongArgument(String name, long value) {
|
||||
super(name);
|
||||
this.hasValue = true;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArgumentType getType() {
|
||||
return ArgumentType.LONG;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "LongArgument{" +
|
||||
name + "=" +
|
||||
value +
|
||||
'}';
|
||||
}
|
||||
}
|
30
src/main/java/dev/zontreck/ariaslib/args/StringArgument.java
Normal file
30
src/main/java/dev/zontreck/ariaslib/args/StringArgument.java
Normal file
|
@ -0,0 +1,30 @@
|
|||
package dev.zontreck.ariaslib.args;
|
||||
|
||||
public class StringArgument extends Argument<String> {
|
||||
|
||||
private String value;
|
||||
|
||||
public StringArgument(String name, String value) {
|
||||
super(name);
|
||||
this.value = value;
|
||||
this.hasValue = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArgumentType getType() {
|
||||
return ArgumentType.STRING;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "StringArgument{" +
|
||||
name + "=" +
|
||||
value +
|
||||
'}';
|
||||
}
|
||||
}
|
Reference in a new issue