private boolean executeCommand(String[] args)
{
String commandName = args[0];
if (COMMANDS.containsKey(commandName))
{
ClientCommand command = COMMANDS.get(commandName);
if (command.isConnectionRequired() && !isConnected())
{
outputWriter.println("The '" + commandName
+ "' command requires a connection. Please run the 'connect' command first.");
return true;
}
String[] params = new String[args.length - 1];
System.arraycopy(args, 1, params, 0, args.length - 1);
OptionParser optionParser = command.getOptionParser();
optionParser.acceptsAll(asList("h", "?", "help"), "display help");
OptionSet options = optionParser.parse(params);
if (options.has("help"))
{
try
{
optionParser.printHelpOn(this.outputWriter);
}
catch (IOException e)
{
throw new IllegalStateException(e);
}
}
else
{
try
{
return command.execute(this, options);
}
catch (Exception e)
{
getPrintWriter().write("Command failed: " + e.getLocalizedMessage());
e.printStackTrace(getPrintWriter());