Search in sources :

Example 1 with LegacyCommand

use of org.jboss.as.cli.impl.aesh.cmd.operation.LegacyCommandContainer.LegacyCommand in project wildfly-core by wildfly.

the class HelpCommand method execute.

@Override
public CommandResult execute(CLICommandInvocation commandInvocation) throws CommandException, InterruptedException {
    CommandContext ctx = commandInvocation.getCommandContext();
    if (command == null || command.isEmpty()) {
        if (commands) {
            return listAvailable(commandInvocation);
        } else {
            ctx.printLine(commandInvocation.getHelpInfo("help"));
        }
        return CommandResult.SUCCESS;
    }
    if (commands) {
        throw new CommandException("commands option not usable with an argument");
    }
    if (command.size() > 2) {
        throw new CommandException("Command has more than one action");
    }
    String mainCommand = command.get(0);
    StringBuilder builder = new StringBuilder();
    for (int i = 0; i < command.size(); i++) {
        builder.append(command.get(i));
        if (i < command.size() - 1) {
            builder.append(" ");
        }
    }
    // An operation?
    if (OperationCommandContainer.isOperation(mainCommand)) {
        try {
            ctx.printLine(getOperationHelp(builder.toString(), commandInvocation.getCommandContext()));
        } catch (Exception ex) {
            ctx.printLine("Error getting operation help: " + ex.getLocalizedMessage());
        }
        return CommandResult.SUCCESS;
    }
    try {
        CommandLineParser parser = aeshRegistry.findCommand(mainCommand, builder.toString());
        // Special case for generic command that generates the help content on the fly.
        if (parser.getProcessedCommand().getCommand() instanceof LegacyCommand) {
            CommandHandler handler = ((LegacyCommand) parser.getProcessedCommand().getCommand()).getCommandHandler();
            if (handler instanceof GenericTypeOperationHandler) {
                try {
                    ((GenericTypeOperationHandler) handler).printDescription(commandInvocation.getCommandContext());
                } catch (CommandLineException ex1) {
                    throw new CommandException(ex1);
                }
            // We can only rely on handler, handler hides the actual file path
            // to its help. eg: remove-batch-line is actually batch-remove-line.txt
            } else if (handler instanceof CommandHandlerWithHelp) {
                try {
                    ((CommandHandlerWithHelp) handler).displayHelp(commandInvocation.getCommandContext());
                } catch (CommandLineException ex1) {
                    throw new CommandException(ex1);
                }
            }
        } else {
            ctx.printLine(parser.printHelp());
        }
    } catch (CommandNotFoundException ex) {
        throw new CommandException("Command " + builder.toString() + " does not exist.");
    }
    return CommandResult.SUCCESS;
}
Also used : CommandHandlerWithHelp(org.jboss.as.cli.handlers.CommandHandlerWithHelp) CommandContext(org.jboss.as.cli.CommandContext) CommandException(org.aesh.command.CommandException) CommandHandler(org.jboss.as.cli.CommandHandler) CommandNotFoundException(org.aesh.command.CommandNotFoundException) CommandException(org.aesh.command.CommandException) CommandLineException(org.jboss.as.cli.CommandLineException) CommandFormatException(org.jboss.as.cli.CommandFormatException) CommandLineException(org.jboss.as.cli.CommandLineException) GenericTypeOperationHandler(org.jboss.as.cli.handlers.GenericTypeOperationHandler) LegacyCommand(org.jboss.as.cli.impl.aesh.cmd.operation.LegacyCommandContainer.LegacyCommand) CommandLineParser(org.aesh.command.impl.parser.CommandLineParser) CommandNotFoundException(org.aesh.command.CommandNotFoundException)

Aggregations

CommandException (org.aesh.command.CommandException)1 CommandNotFoundException (org.aesh.command.CommandNotFoundException)1 CommandLineParser (org.aesh.command.impl.parser.CommandLineParser)1 CommandContext (org.jboss.as.cli.CommandContext)1 CommandFormatException (org.jboss.as.cli.CommandFormatException)1 CommandHandler (org.jboss.as.cli.CommandHandler)1 CommandLineException (org.jboss.as.cli.CommandLineException)1 CommandHandlerWithHelp (org.jboss.as.cli.handlers.CommandHandlerWithHelp)1 GenericTypeOperationHandler (org.jboss.as.cli.handlers.GenericTypeOperationHandler)1 LegacyCommand (org.jboss.as.cli.impl.aesh.cmd.operation.LegacyCommandContainer.LegacyCommand)1