Command Definition

Command Definition

The @CommandDefinition annotation is used to define a command class.

Required Properties

PropertyTypeDescription
nameStringThe command name

Optional Properties

PropertyTypeDefaultDescription
aliasesString[]{}Alternative names for the command
descriptionString""Command description shown in help
generateHelpbooleanfalseAuto-generate --help option
disableParsingbooleanfalseSkip parsing (everything goes to @Arguments)
versionString""Version string (adds --version, -v option)
validatorClass<? extends CommandValidator>NullCommandValidator.classValidator to run before execution
resultHandlerClass<? extends ResultHandler>NullResultHandler.classHandler to run after execution
activatorClass<? extends CommandActivator>NullCommandActivator.classActivator 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 successfully
  • CommandResult.FAILURE - Command failed
  • CommandResult.RETURN - Return from current subcommand

CommandInvocation

Provides access to:

  • println(String) - Output text to the console
  • print(String) - Output text without newline
  • stop() - Stop the console
  • getShell() - Access the shell
  • getHelpInfo(String) - Get help text