use of org.jcryptool.commands.core.Command 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);
}
Aggregations