Added Argument System
This commit is contained in:
25
README.MD
25
README.MD
@@ -11,6 +11,31 @@ and a wide set of math/number/string/file/reflection helpers.
|
||||
- Command system: command manager, permissions, execution events
|
||||
- Network system: TCP/UDP transport, packet handling, optional TLS, UDP encryption
|
||||
- Utilities: math/number helpers, strings, colors, files, reflection, logging
|
||||
- Argument parsing: register arguments, validate values, run callbacks
|
||||
|
||||
## ArgumentParser
|
||||
Basic usage
|
||||
```java
|
||||
ArgumentParser parser = new ArgumentParser(args);
|
||||
|
||||
Argument verbose = new Argument("verbose", "Enable verbose output", false);
|
||||
verbose.setRun((arg, value) -> System.out.println("Verbose on"));
|
||||
parser.registerArgument(verbose);
|
||||
|
||||
Argument mode = new Argument("mode", "Run mode", true, true, false, List.of("dev", "prod"));
|
||||
mode.setRun((arg, value) -> System.out.println("Mode: " + value.orElse("")));
|
||||
parser.registerArgument(mode);
|
||||
|
||||
parser.runArguments();
|
||||
```
|
||||
|
||||
Value rules
|
||||
- Constructor: `Argument(name, description, required, requireValue, optionalValue, values)`
|
||||
- `requireValue=false` and `optionalValue=false`: argument has no value
|
||||
- `requireValue=true` and `optionalValue=false`: value must be present
|
||||
- `requireValue=false` and `optionalValue=true`: value is optional
|
||||
- `requireValue=true` and `optionalValue=true` is not allowed
|
||||
- If `values` is not empty, the provided value must be in that list
|
||||
|
||||
## License Information
|
||||
GNU General Public License v3.0 (GPLv3)<br />
|
||||
|
||||
2
pom.xml
2
pom.xml
@@ -6,7 +6,7 @@
|
||||
|
||||
<groupId>dev.unlegitdqrk</groupId>
|
||||
<artifactId>unlegitlibrary</artifactId>
|
||||
<version>1.7.9</version>
|
||||
<version>1.8.0</version>
|
||||
<url>https://unlegitdqrk.dev/</url>
|
||||
<description>Just a big library</description>
|
||||
|
||||
|
||||
@@ -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