From 688869d1a5d60b8bdd89c78e0deab6b53b04a694 Mon Sep 17 00:00:00 2001 From: Wyvest <45589059+Wyvest@users.noreply.github.com> Date: Sat, 28 May 2022 22:41:38 +0700 Subject: command utils --- .../utils/commands/arguments/ArgumentParser.java | 12 ++++++++ .../utils/commands/arguments/Arguments.java | 33 ++++++++++++++++++++++ .../utils/commands/arguments/BooleanParser.java | 11 ++++++++ .../utils/commands/arguments/DoubleParser.java | 10 +++++++ .../utils/commands/arguments/FloatParser.java | 11 ++++++++ .../utils/commands/arguments/IntegerParser.java | 8 ++++++ .../utils/commands/arguments/StringParser.java | 18 ++++++++++++ 7 files changed, 103 insertions(+) create mode 100644 src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/ArgumentParser.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/Arguments.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/BooleanParser.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/DoubleParser.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/FloatParser.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/IntegerParser.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/StringParser.java (limited to 'src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments') 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 { + private final TypeToken type = new TypeToken(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 { + + @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 { + @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 { + + @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 { + @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 { + + @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(); + } + } +} -- cgit