Command Definition
The @CommandDefinition annotation is used to define a command class.
Required Properties
| Property | Type | Description |
|---|---|---|
name | String | The command name |
Optional Properties
| Property | Type | Default | Description |
|---|---|---|---|
aliases | String[] | {} | Alternative names for the command |
description | String | "" | Command description shown in help |
generateHelp | boolean | false | Auto-generate --help option |
disableParsing | boolean | false | Skip parsing (everything goes to @Arguments) |
version | String | "" | Version string (adds --version, -v option) |
validator | Class<? extends CommandValidator> | NullCommandValidator.class | Validator to run before execution |
resultHandler | Class<? extends ResultHandler> | NullResultHandler.class | Handler to run after execution |
activator | Class<? extends CommandActivator> | NullCommandActivator.class | Activator to check if command is available |
Example
@CommandDefinition(
name = "copy",
aliases = {"cp"},
description = "Copy files",
generateHelp = true
)
public class CopyCommand implements Command<CommandInvocation> {
@Option(shortName = 'r', description = "Recursive copy")
private boolean recursive;
@Override
public CommandResult execute(CommandInvocation invocation) {
// implementation
return CommandResult.SUCCESS;
}
}Command Interface
Your command must implement the Command<T extends CommandInvocation> interface:
public interface Command<T extends CommandInvocation> {
CommandResult execute(T commandInvocation)
throws CommandException, InterruptedException;
}CommandResult
Return one of the following:
CommandResult.SUCCESS- Command completed successfullyCommandResult.FAILURE- Command failedCommandResult.RETURN- Return from current subcommand
CommandInvocation
Provides access to:
println(String)- Output text to the consoleprint(String)- Output text without newlinestop()- Stop the consolegetShell()- Access the shellgetHelpInfo(String)- Get help text