/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2011, Red Hat, Inc., and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.as.cli.handlers;


import java.util.List;

import org.jboss.as.cli.CommandContext;
import org.jboss.as.cli.operation.ParsedCommandLine;

/**
 * Connect handler.
 *
 * @author Alexey Loubyansky
 */
public class ConnectHandler extends CommandHandlerWithHelp {

    public ConnectHandler() {
        this("connect");
    }

    public ConnectHandler(String command) {
        super(command);
    }

    @Override
    public boolean hasArgument(int index) {
        return index <= 1;
    }

    @Override
    protected void doHandle(CommandContext ctx) {

        int port = -1;
        String host = null;
        final ParsedCommandLine parsedCmd = ctx.getParsedCommandLine();
        final List<String> args = parsedCmd.getOtherProperties();

        if(!args.isEmpty()) {
            if(args.size() != 1) {
                ctx.printLine("The command expects only one argument but got " + args);
                return;
            }
            final String arg = args.get(0);
            String portStr = null;
            int colonIndex = arg.indexOf(':');
            if(colonIndex < 0) {
                // default port
                host = arg;
            } else if(colonIndex == 0) {
                // default host
                portStr = arg.substring(1).trim();
            } else {
                host = arg.substring(0, colonIndex).trim();
                portStr = arg.substring(colonIndex + 1).trim();
            }

            if(portStr != null) {
                try {
                    port = Integer.parseInt(portStr);
                } catch(NumberFormatException e) {
                    ctx.printLine("The port must be a valid non-negative integer: '" + args + "'");
                    return;
                }

                if(port < 0) {
                    ctx.printLine("The port must be a valid non-negative integer: '" + args + "'");
                    return;
                }
            }
        }

        ctx.connectController(host, port);
    }
}

@@ -2080,17 +2080,21 @@
 x = arg.
-i
+lastI
 ndexOf('
@@ -2336,16 +2336,527 @@
  else %7B%0A
+                final boolean hasPort;%0A                int closeBracket = arg.lastIndexOf('%5D');%0A                if (closeBracket != -1) %7B%0A                    //possible ip v6%0A                    if (closeBracket %3E colonIndex) %7B%0A                        hasPort = false;%0A                    %7D else %7B%0A                        hasPort = true;%0A                    %7D%0A                %7D else %7B%0A                    //probably ip v4%0A                    hasPort = true;%0A                %7D%0A                if (hasPort) %7B%0A    
         
@@ -2899,32 +2899,36 @@
 nIndex).trim();%0A
+    
                 
@@ -2967,32 +2967,107 @@
 ex + 1).trim();%0A
+                %7D else %7B%0A                    host = arg;%0A                %7D%0A
             %7D%0A%0A 

Handle ipv6 addresses in cli