Skip to content

Repository files navigation

Command

Maven Central Version

A pure, functional, typesafe command line parser.

Import

Gradle

repositories {
mavenCentral()
}
dependencies {
implementation 'io.typst:command-bukkit:3.1.6'
}

Maven

<dependency>
<groupId>io.typst</groupId>
<artifactId>command-bukkit</artifactId>
<version>3.1.6</version>
</dependency>

Usage

// 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()));
}

FAQ

Why not just execute?

Command<Void> node = Command.mapping(
pair("foo", Command.argument(integer -> {
GlobalVariables.someVar = integer;
System.out.println("Input is: " + integer); // here to break purityreturnnull;
}, intArg))
)

1. Lines too long

If you inline command implementation into the node declaration, it will easily to be ugly and hard to maintain.

2. Concurrent

You can't run Command.parse without synchronization, because it mutates global variable.

3. Type safety

Loose type safety even in typed programming language, you don't know what command is parsed.

4. Testing

Hard to test what it printed.

5. Reusability

Can't run the command implementation without parsing the command arguments.

About

Typesafe command line parser

Topics

Resources

Stars

4 stars

Watchers

2 watching

Forks

Releases

Packages

Contributors

Languages