package brooklyn.cli.commands;

import com.google.common.base.Objects;
import com.sun.jersey.api.client.Client;
import org.codehaus.jackson.map.ObjectMapper;
import org.iq80.cli.Option;
import org.iq80.cli.OptionType;
import java.util.concurrent.Callable;
import java.lang.UnsupportedOperationException;

public abstract class BrooklynCommand implements Callable<Void> {

    public static final Client httpClient = Client.create(); // Jersey rest client
    public static final ObjectMapper jsonParser = new ObjectMapper(); // Jackson json parser

    public static final int DEFAULT_RETRY_PERIOD = 30;
    public static final String DEFAULT_ENDPOINT = "http://localhost:8080";

    @Option(type = OptionType.GLOBAL,
            name = { "--embedded" },
            description = "Start a simple embedded local web server")
    public boolean embedded = false;

    @Option(type = OptionType.GLOBAL,
            name = { "--endpoint" },
            description = "REST endpoint, default \""+DEFAULT_ENDPOINT+"\"")
    public String endpoint = DEFAULT_ENDPOINT;

    @Option(type = OptionType.GLOBAL,
            name = { "--retry" },
            description = "Will retry connection to the endpoint for this time period, default \""+DEFAULT_RETRY_PERIOD+"s\"")
    public int retry = DEFAULT_RETRY_PERIOD;

    @Option(type = OptionType.GLOBAL,
            name = { "--no-retry" },
            description = "Won't retry connection")
    public boolean noRetry = false;

    @Override
    public String toString() {
        return Objects.toStringHelper(getClass())
                .add("embedded", embedded)
                .add("endpoint", endpoint)
                .toString();
    }

    /**
     * Common code that should normally be executed by all commands
     *
     * @return null
     * @throws Exception
     */
    public Void call() throws Exception {

        // Embedded web server feature
        if(embedded)
            throw new UnsupportedOperationException(
                    "The \"--embedded\" option is not supported yet");

        return null;
    }

}


@@ -211,16 +211,52 @@
 onType;%0A
+import org.iq80.cli.ParseException;%0A
 import j
@@ -1893,24 +1893,110 @@
 xception %7B%0A%0A
+        // Additional higher level syntax validation%0A        additionalValidation();%0A%0A
         // E
@@ -2196,11 +2196,653 @@
 %0A    %7D%0A%0A
+    /**%0A     * Adds some additional validation for the cli arguments.%0A     *%0A     * Git-like-cli doesn't attempt to do this so this is the place%0A     * where some of the additional things that we need can go.%0A     *%0A     * Calling this will throw a %7B@link ParseException%7D that will be caght%0A     *%0A     * @throws ParseException%0A     */%0A    private void additionalValidation() throws ParseException %7B%0A        // Make sure that %22--retry%22 and %22--no-retry%22 are mutually exclusive%0A        if(noRetry && retry!=DEFAULT_RETRY_PERIOD)%0A            throw new ParseException(%22The %5C%22--retry%5C%22 and %5C%22--no-retry%5C%22 options are mutually exclusive!%22);%0A    %7D%0A%0A
 %7D%0A%0A

Add some validation for mutually exclusive options