A pure, functional, typesafe command line parser.
repositories {
mavenCentral()
}
dependencies {
implementation 'io.typst:command-bukkit:3.1.6'
}<dependency>
<groupId>io.typst</groupId>
<artifactId>command-bukkit</artifactId>
<version>3.1.6</version>
</dependency>// core/src/test/../CommandTest.java// MyCommand = AddItem | RemoveItem | OpenItemList | ReloadCommandCommand<MyCommand> command = Command.mapping(
pair("item", Command.mapping(
pair("open", Command.present(newOpenItemList())),
// intArg: Argument<Integer>// strArg: Argument<String>// AddItem::new = (Integer, String) -> AddItempair("add", Command.argument(AddItem::new, intArg, strArg)),
pair("remove", Command.argument(RemoveItem::new, intArg))
)),
pair("reload", Command.present(newReloadCommand()))
);
// parsingString[] args = newString[] {"item", "add", "0", "NAME"};
MyCommandalgebra = Command.parseO(args, command).orElse(null);
// execution, check with if-instanceof.// assumes `MyCommand` is sealed, treat like Enum.// therefore, this is a valid type casting (not unsafe).if (algebrainstanceofMyCommand.AddItem) {
MyCommand.AddItemaddItem = (MyCommand.AddItem) algebra;
println(String.format("Adding item %s, %s!", addItem.getIndex(), addItem.getName()));
} elseif (algebrainstanceofMyCommand.RemoveItem) {
MyCommand.RemoveItemremoveItem = (MyCommand.RemoveItem) algebra;
println(String.format("Removing item %s", removeItem.getIndex()));
}Command<Void> node = Command.mapping(
pair("foo", Command.argument(integer -> {
GlobalVariables.someVar = integer;
System.out.println("Input is: " + integer); // here to break purityreturnnull;
}, intArg))
)If you inline command implementation into the node declaration, it will easily to be ugly and hard to maintain.
You can't run Command.parse without synchronization, because it mutates global variable.
Loose type safety even in typed programming language, you don't know what command is parsed.
Hard to test what it printed.
Can't run the command implementation without parsing the command arguments.