Added Argument System
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* Copyright (C) 2026 UnlegitDqrk - All Rights Reserved
|
||||
*
|
||||
* You are unauthorized to remove this copyright.
|
||||
* You have to give Credits to the Author in your project and link this GitHub site: https://github.com/UnlegitDqrk
|
||||
* See LICENSE-File if exists
|
||||
*/
|
||||
|
||||
package dev.unlegitdqrk.unlegitlibrary.argument;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Argument {
|
||||
|
||||
private final String name;
|
||||
private final String description;
|
||||
private final boolean required;
|
||||
private final List<String> values;
|
||||
private final boolean requireValue;
|
||||
private final boolean optionalValue;
|
||||
private ArgumentRun run;
|
||||
|
||||
public ArgumentRun getRun() {
|
||||
return run;
|
||||
}
|
||||
|
||||
public void setRun(ArgumentRun run) {
|
||||
this.run = run;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public boolean isRequired() {
|
||||
return required;
|
||||
}
|
||||
|
||||
public List<String> getValues() {
|
||||
return values;
|
||||
}
|
||||
|
||||
public boolean isRequireValue() {
|
||||
return requireValue;
|
||||
}
|
||||
|
||||
public boolean isOptionalValue() {
|
||||
return optionalValue;
|
||||
}
|
||||
|
||||
public Argument(String name, String description, boolean argRequired) {
|
||||
this(name, description, argRequired, false, false, new ArrayList<>());
|
||||
}
|
||||
|
||||
public Argument(String name, String description, boolean argRequired, boolean requireValue, boolean optionalValue, List<String> values) {
|
||||
if (requireValue && optionalValue) {
|
||||
throw new IllegalArgumentException("requireValue and optionalValue cannot both be true");
|
||||
}
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
this.required = argRequired;
|
||||
this.values = values == null ? new ArrayList<>() : values;
|
||||
this.requireValue = requireValue;
|
||||
this.optionalValue = optionalValue;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* Copyright (C) 2026 UnlegitDqrk - All Rights Reserved
|
||||
*
|
||||
* You are unauthorized to remove this copyright.
|
||||
* You have to give Credits to the Author in your project and link this GitHub site: https://github.com/UnlegitDqrk
|
||||
* See LICENSE-File if exists
|
||||
*/
|
||||
|
||||
package dev.unlegitdqrk.unlegitlibrary.argument;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public class ArgumentParser {
|
||||
|
||||
private final String[] args;
|
||||
private final List<Argument> arguments;
|
||||
private final List<ArgumentRun> runnedArguments;
|
||||
|
||||
public ArgumentParser(String[] args) {
|
||||
this.args = args;
|
||||
this.arguments = new ArrayList<>();
|
||||
this.runnedArguments = new ArrayList<>();
|
||||
}
|
||||
|
||||
public void registerArgument(Argument arg) {
|
||||
arguments.add(arg);
|
||||
}
|
||||
|
||||
public void unregisterArgument(Argument arg) {
|
||||
arguments.remove(arg);
|
||||
}
|
||||
|
||||
public void runArguments() {
|
||||
arguments.stream().filter(Argument::isRequired).forEach(arg -> {
|
||||
if (!Arrays.stream(args).toList().contains(arg.getName())) {
|
||||
throw new IllegalArgumentException("Missing argument: " + arg.getName());
|
||||
}
|
||||
});
|
||||
|
||||
for (int i = 0; i <= args.length - 1; i++) {
|
||||
String arg = args[i];
|
||||
|
||||
for (Argument argument : arguments) {
|
||||
if (argument.getName().equalsIgnoreCase(arg)) {
|
||||
if (argument.isRequireValue()) {
|
||||
if (i + 1 >= args.length) {
|
||||
if (argument.isOptionalValue()) {
|
||||
if (argument.getRun() != null) argument.getRun().onRun(argument, Optional.empty());
|
||||
} else {
|
||||
throw new IllegalArgumentException("Missing value for argument: " + argument.getName());
|
||||
}
|
||||
} else {
|
||||
String value = args[i + 1];
|
||||
if (!argument.getValues().isEmpty() && !argument.getValues().contains(value)) {
|
||||
StringBuilder possibleValues = new StringBuilder();
|
||||
for (int i1 = 0; i1 < argument.getValues().size(); i1++) {
|
||||
possibleValues.append(argument.getValues().get(i1));
|
||||
if (i1 != argument.getValues().size() - 1) possibleValues.append(", ");
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException("Invalid argument value '" + value + "' for argument '" + argument.getName() + "'! Possible: " + possibleValues.toString());
|
||||
} else if (argument.getRun() != null) argument.getRun().onRun(argument, Optional.of(value));
|
||||
i++;
|
||||
}
|
||||
} else if (argument.getRun() != null) argument.getRun().onRun(argument, Optional.empty());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
/*
|
||||
* Copyright (C) 2026 UnlegitDqrk - All Rights Reserved
|
||||
*
|
||||
* You are unauthorized to remove this copyright.
|
||||
* You have to give Credits to the Author in your project and link this GitHub site: https://github.com/UnlegitDqrk
|
||||
* See LICENSE-File if exists
|
||||
*/
|
||||
|
||||
package dev.unlegitdqrk.unlegitlibrary.argument;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public abstract class ArgumentRun {
|
||||
public abstract void onRun(Argument argument, Optional<String> value);
|
||||
}
|
||||
Reference in New Issue
Block a user