-
-
Notifications
You must be signed in to change notification settings - Fork 48
Orphan commands
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.
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.
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!
👋 If you're having trouble, need support, or just feel like chatting, feel free to hop by our Discord server!