package forager.ui;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;

import forager.client.Forager;
import forager.server.Overlord;

import galileo.net.NetworkDestination;

public class Launcher {

    public static final int DEFAULT_PORT = 53380;

    public static void main(String[] args)
    throws Exception {
        CommandLineParser parser = new GnuParser();
        Options options = new Options();

        options.addOption("s", "server", false,
                "Start a server daemon");
        options.addOption("p", "port", true,
                "Specifies the port to connect to or listen on.");
        options.addOption("t", "threads", true,
                "Maximum number of threads to use (client only)");

        CommandLine cl = parser.parse(options, args);
        if (cl.hasOption("server")) {
            Overlord server = new Overlord(getPort(cl));
            server.start();
        } else {
            Forager client;

            String[] clientArgs = cl.getArgs();
            if (args.length < 1) {
                printUsage();
                System.exit(1);
            }
            NetworkDestination server = new NetworkDestination(
                    clientArgs[0], getPort(cl));

            if (cl.hasOption("threads")) {
                int threads = Integer.parseInt(cl.getOptionValue("threads"));
                client = new Forager(server, threads);
            } else {
                client = new Forager(server);
            }

            client.start();
        }
        System.out.println(cl.getArgs()[0]);
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("ant", options);
    }

    private static int getPort(CommandLine cl) {
        int port;
        if (cl.hasOption("port")) {
            port = Integer.parseInt(cl.getOptionValue("port"));
        } else {
            port = DEFAULT_PORT;
        }

        return port;
    }

    private static void printUsage() {
        //TODO
    }
}

@@ -230,16 +230,75 @@
 Options;
+%0Aimport org.apache.commons.cli.UnrecognizedOptionException;
 %0A%0Aimport
@@ -978,36 +978,160 @@
 l = 
-parser.parse(options, args);
+null;%0A        try %7B%0A            cl = parser.parse(options, args);%0A        %7D catch (UnrecognizedOptionException e) %7B%0A            printUsage();%0A        %7D%0A
 %0A   
@@ -1413,40 +1413,8 @@
 ();%0A
-                System.exit(1);%0A
     
@@ -2292,14 +2292,156 @@
     
-//TODO
+printUsage(true);%0A    %7D%0A%0A    private static void printUsage(boolean exit) %7B%0A        //TODO%0A        if (exit) %7B%0A            System.exit(1);%0A        %7D
 %0A   

Handle unrecognized options