Orphan command
This page explains how to use orphan commands, which are commands defined without the @Command annotation and whose path will be determined dynamically at runtime.
Due to the nature of annotations, all commands and subcommands in a typical command framework must have known paths at compile-time. This requirement makes it impossible to create commands whose paths are dynamically determined, such as those sourced from configuration files or user input. The OrphanCommand
API solves this limitation by allowing commands to have paths that are resolved at runtime.
Overview
What is an OrphanCommand
?
OrphanCommand
?An OrphanCommand
is a command whose parent or path is not known at compile-time. Implementing the OrphanCommand
interface signals to the framework (Lamp) that the command's path will be dynamically determined and should be resolved at runtime.
How to Register an OrphanCommand
OrphanCommand
To register an OrphanCommand
, the command must be wrapped using the Orphans
utility. This utility allows specifying the command path dynamically and is similar to using the @Command
annotation but without the compile-time constraints.
Example Usage
Implement the
OrphanCommand
Interface: Create a class that implementsOrphanCommand
and define your command methods within it.Register the Orphan Command: Use the
Orphans.path()
method to set the path for the orphan command dynamically.
Example
Step 1: Implementing an OrphanCommand
OrphanCommand
In this example, Foo
is an orphan command class with a subcommand bar
.
Step 2: Registering the Orphan Command
Assuming args[0]
is "buzz"
, the above code will register a command with the path "buzz"
.
Example Command Execution
Orphans.path()
Behavior
Orphans.path()
BehaviorOrphans.path("foo", "bar")
is equivalent to@Command("foo", "bar")
Orphans.path("foo bar", "buzz boom")
is equivalent to@Command("foo bar", "buzz boom")
However, Orphans.path()
can also accept strings that are not known at compile-time, such as values loaded from configuration files or provided by user input.
Summary
The OrphanCommand
interface and Orphans
utility enable dynamic command path registration, allowing for greater flexibility and configurability in command definitions. This feature is especially useful when command paths need to be determined at runtime, bypassing the compile-time limitations of traditional annotations.
Last updated
Was this helpful?