Skip to content

Orphan commands

Revxrsal edited this page Feb 27, 2022 · 2 revisions

Due to the nature of annotations, all commands and subcommands must have known paths at compile-time. Therefore, it is impossible to create commands whose paths are not directly accessible at compile-time (for example, configuration files). The orphan commands API aims to fix this problem.

Usage

Command classes whose parents are unknown at compile-time must implement OrphanCommand, which tells Lamp to resolve the path at runtime for it.

To register an orphan command, it first must be wrapped using Orphans as follows:

CommandHandler handler = ...;
handler.register(Orphans.path("path here").handler(OrphanCommand));

Orphans.path() behaves exactly like @Command:

Orphans.path("foo", "bar") -> @Command("foo", "bar")
Orphans.path("foo bar", "buzz boom") -> @Command("foo bar", "buzz boom")

However, Orphans.path() also accepts strings that are not known at compile-time, for example configuration values.

Example

 public class Foo implements OrphanCommand {

     @Subcommand("bar")
     public void bar(CommandActor actor) {
         actor.reply("Hello!");
     }
 }

public static void main(String[] args) {
    ConsoleCommandHandler handler = ConsoleCommandHandler.create();
    handler.register(Orphans.path(args[0]).handler(new Foo()));
    handler.pollInput();
}

where args[0] = "buzz"

> buzz bar
Hello!
Clone this wiki locally