diff options
author | Wyvest <45589059+Wyvest@users.noreply.github.com> | 2022-05-28 22:41:38 +0700 |
---|---|---|
committer | Wyvest <45589059+Wyvest@users.noreply.github.com> | 2022-05-28 22:41:38 +0700 |
commit | 688869d1a5d60b8bdd89c78e0deab6b53b04a694 (patch) | |
tree | 079620c9471115b88c046046d6db4f5f567a9425 /src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments | |
parent | fe58fa4924522e24ff32d2a4abb2ae959252d553 (diff) | |
download | OneConfig-688869d1a5d60b8bdd89c78e0deab6b53b04a694.tar.gz OneConfig-688869d1a5d60b8bdd89c78e0deab6b53b04a694.tar.bz2 OneConfig-688869d1a5d60b8bdd89c78e0deab6b53b04a694.zip |
command utils
Diffstat (limited to 'src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments')
7 files changed, 103 insertions, 0 deletions
diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/ArgumentParser.java b/src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/ArgumentParser.java new file mode 100644 index 0000000..d9d51b0 --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/ArgumentParser.java @@ -0,0 +1,12 @@ +package cc.polyfrost.oneconfig.utils.commands.arguments; + +import com.google.common.reflect.TypeToken; +import org.jetbrains.annotations.Nullable; + +@SuppressWarnings("unstable") +public abstract class ArgumentParser<T> { + private final TypeToken<T> type = new TypeToken<T>(getClass()) {}; + public final Class<?> typeClass = type.getRawType(); + @Nullable + public abstract T parse(Arguments arguments); +} diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/Arguments.java b/src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/Arguments.java new file mode 100644 index 0000000..74a0840 --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/Arguments.java @@ -0,0 +1,33 @@ +package cc.polyfrost.oneconfig.utils.commands.arguments; + +public class Arguments { + private int position = 0; + public final String[] args; + public final boolean greedy; + + public Arguments(String[] args, boolean greedy) { + this.args = args; + this.greedy = greedy; + } + + public String poll() { + ++position; + return args[position - 1]; + } + + public String peek() { + if (hasNext()) { + return args[position]; + } else { + return null; + } + } + + public boolean hasNext() { + return position < args.length; + } + + public int getPosition() { + return position; + } +} diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/BooleanParser.java b/src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/BooleanParser.java new file mode 100644 index 0000000..dfdca2d --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/BooleanParser.java @@ -0,0 +1,11 @@ +package cc.polyfrost.oneconfig.utils.commands.arguments; + +import org.jetbrains.annotations.Nullable; + +public class BooleanParser extends ArgumentParser<Boolean> { + + @Override + public @Nullable Boolean parse(Arguments arguments) { + return Boolean.parseBoolean(arguments.poll()); + } +} diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/DoubleParser.java b/src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/DoubleParser.java new file mode 100644 index 0000000..8c85849 --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/DoubleParser.java @@ -0,0 +1,10 @@ +package cc.polyfrost.oneconfig.utils.commands.arguments; + +import org.jetbrains.annotations.Nullable; + +public class DoubleParser extends ArgumentParser<Double> { + @Override + public @Nullable Double parse(Arguments arguments) { + return Double.parseDouble(arguments.poll()); + } +} diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/FloatParser.java b/src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/FloatParser.java new file mode 100644 index 0000000..7053fcb --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/FloatParser.java @@ -0,0 +1,11 @@ +package cc.polyfrost.oneconfig.utils.commands.arguments; + +import org.jetbrains.annotations.Nullable; + +public class FloatParser extends ArgumentParser<Float> { + + @Override + public @Nullable Float parse(Arguments arguments) { + return Float.parseFloat(arguments.poll()); + } +} diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/IntegerParser.java b/src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/IntegerParser.java new file mode 100644 index 0000000..6910d4b --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/IntegerParser.java @@ -0,0 +1,8 @@ +package cc.polyfrost.oneconfig.utils.commands.arguments; + +public class IntegerParser extends ArgumentParser<Integer> { + @Override + public Integer parse(Arguments arguments) { + return Integer.parseInt(arguments.poll()); + } +} diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/StringParser.java b/src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/StringParser.java new file mode 100644 index 0000000..6a34722 --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/StringParser.java @@ -0,0 +1,18 @@ +package cc.polyfrost.oneconfig.utils.commands.arguments; + +public class StringParser extends ArgumentParser<String> { + + @Override + public String parse(Arguments arguments) { + if (arguments.greedy) { + StringBuilder builder = new StringBuilder(); + while (arguments.hasNext()) { + String arg = arguments.poll(); + builder.append(arg).append(" "); + } + return builder.toString().trim(); + } else { + return arguments.poll(); + } + } +} |