Search in sources :

Example 1 with IllegalCommandException

use of org.jcryptool.commands.core.api.IllegalCommandException in project core by jcryptool.

the class CommandEvaluator method evaluate.

public CommandEvaluator.EvaluationResult evaluate(String commandString) throws ParseException {
    if (commandString.contains("\n")) {
        // $NON-NLS-1$
        // $NON-NLS-1$
        String[] splitCommandStrings = commandString.split("((\\r\\n)|(\r)|(\n))");
        return evaluateMultiline(splitCommandStrings);
    }
    // $NON-NLS-1$
    int commandEnd = commandString.indexOf(" ");
    commandEnd = commandEnd > 0 ? commandEnd : commandString.length();
    String commandName = commandString.substring(0, commandEnd);
    String cleanCommandLine = commandString.trim();
    // $NON-NLS-1$
    String commandArgs = commandString.substring(commandEnd).trim();
    // $NON-NLS-1$
    StringBuilder result = new StringBuilder();
    Command command = commands.get(commandName);
    if (command != null) {
        CommandLine commandLine = null;
        if (isCallForShortHelp(cleanCommandLine)) {
            result.append(HelpCommand.getShortHelpFor(commandName, command, cleanCommandLine));
        } else if (isCallForDetailedHelp(cleanCommandLine)) {
            result.append(HelpCommand.getDetailedHelpFor(commandName, command, cleanCommandLine));
        } else
            try {
                String[] args = splitArgs(commandArgs);
                BasicParser parser = new BasicParser();
                commandLine = parser.parse(command.createOptions(), args);
                command.execute(commandLine);
                result.append(command.getResult());
            } catch (IllegalCommandException e) {
                result.append(e.getMessage());
            } catch (RuntimeException e) {
                result.append(Messages.CommandEvaluator_1);
                result.append(e.getLocalizedMessage());
            } catch (ParseException e) {
                // TODO only if it is a "help anormality"
                result.append(HelpCommand.getHelpForBadCommandCall(commandName, command, cleanCommandLine, e));
                return new EvaluationResult(result.toString(), ResultType.ERROR_SYNTAX);
            }
    } else {
        result.append(NLS.bind(Messages.CommandEvaluator_0, commandName));
    }
    return new EvaluationResult(result.toString(), ResultType.OK);
}
Also used : BasicParser(org.apache.commons.cli.BasicParser) CommandLine(org.apache.commons.cli.CommandLine) Command(org.jcryptool.commands.core.Command) HelpCommand(org.jcryptool.commands.core.HelpCommand) IllegalCommandException(org.jcryptool.commands.core.api.IllegalCommandException) ParseException(org.apache.commons.cli.ParseException)

Aggregations

BasicParser (org.apache.commons.cli.BasicParser)1 CommandLine (org.apache.commons.cli.CommandLine)1 ParseException (org.apache.commons.cli.ParseException)1 Command (org.jcryptool.commands.core.Command)1 HelpCommand (org.jcryptool.commands.core.HelpCommand)1 IllegalCommandException (org.jcryptool.commands.core.api.IllegalCommandException)1