Defining Commands
The Command Interface
The Command interface is the core abstraction. Each command provides:
name()— the primary command namealiases()— alternative namesdescription()— one-line description for help listingsdescribe(args)— detailed description for widgetsexecute(session, args)— the execution logiccompleters()— tab completion support
Using AbstractCommand
AbstractCommand is a convenient base class:
public class MyCommand extends AbstractCommand {
public MyCommand() {
super("mycommand", "mc", "my"); // name + aliases
}
@Override
public String description() {
return "Does something useful";
}
@Override
public Object execute(CommandSession session, String[] args) {
// Command logic here
return null;
}
}
Command Groups
SimpleCommandGroup organizes commands:
CommandGroup tools = new SimpleCommandGroup("tools",
new UpperCommand(),
new LowerCommand(),
new CountCommand()
);
CommandSession
The CommandSession provides the execution context:
session.terminal()— the terminalsession.in()— input streamsession.out()— output print streamsession.err()— error print streamsession.get(name)/session.put(name, value)— session variablessession.workingDirectory()— current working directorysession.lastExitCode()— last command's exit code