/* This file is part of VoltDB.
 * Copyright (C) 2008-2012 VoltDB Inc.
 *
 * VoltDB is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * VoltDB 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with VoltDB.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.voltdb;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Field;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

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

public abstract class CLIConfig {

    @Retention(RetentionPolicy.RUNTIME) // Make this annotation accessible at runtime via reflection.
    @Target({ElementType.FIELD})       // This annotation can only be applied to class methods.
    public @interface Option {
        String opt() default "";
        boolean hasArg() default true;
        boolean required() default false;
        String desc() default "";
    }

    // Apache Commons CLI API - requires JAR
    protected final Options options = new Options();
    protected String cmdName = "command";

    protected String configDump;
    protected String usage;

    public void exitWithMessageAndUsage(String msg) {
        System.err.println(msg);
        printUsage();
        System.exit(-1);
    }

    public void printUsage() {
        // automatically generate the help statement
        HelpFormatter formatter = new HelpFormatter();
        formatter.setOptPrefix("--");
        formatter.printHelp(cmdName, options, false);
    }

    private void assignValueToField(Field field, String value) throws Exception {
        if ((value == null) || (value.length() == 0)) {
            return;
        }

        field.setAccessible(true);
        Class<?> cls = field.getType();

        if ((cls == boolean.class) || (cls == Boolean.class))
            field.set(this, Boolean.parseBoolean(value));
        else if ((cls == byte.class) || (cls == Byte.class))
            field.set(this, Byte.parseByte(value));
        else if ((cls == short.class) || (cls == Short.class))
            field.set(this, Short.parseShort(value));
        else if ((cls == int.class) || (cls == Integer.class))
            field.set(this, Integer.parseInt(value));
        else if ((cls == long.class) || (cls == Long.class))
            field.set(this, Long.parseLong(value));
        else if ((cls == float.class) || (cls == Float.class))
            field.set(this, Float.parseFloat(value));
        else if ((cls == double.class) || (cls == Double.class))
            field.set(this, Double.parseDouble(value));
        else if ((cls == String.class))
        	field.set(this, value);
        else if ((cls == char.class) || (cls == Character.class))
        	field.set(this, value.charAt(0));
        else
            assert(false);
    }

    public void parse(String cmdName, String[] args) {
        this.cmdName = cmdName;

        try {
            options.addOption("help", false, "Print this message");

            // add all of the declared options to the cli
            for (Field field : getClass().getDeclaredFields()) {
                if (field.isAnnotationPresent(Option.class) == false) {
                    continue;
                }

                Option option = field.getAnnotation(Option.class);

                String opt = option.opt();
                if ((opt == null) || (opt.trim().length() == 0)) {
                    opt = field.getName();
                }

                options.addOption(opt, option.hasArg(), option.desc());
            }

            CommandLineParser parser = new PosixParser();
            CommandLine cmd = parser.parse(options, args);

            if (cmd.hasOption("help")) {
                printUsage();
                System.exit(0);
            }

            // string key-value pairs
            Map<String, String> kvMap = new TreeMap<String, String>();

            for (Field field : getClass().getDeclaredFields()) {
                if (field.isAnnotationPresent(Option.class) == false) {
                    continue;
                }

                Option option = field.getAnnotation(Option.class);

                String opt = option.opt();
                if ((opt == null) || (opt.length() == 0)) {
                    opt = field.getName();
                }

                if (cmd.hasOption(opt)) {
                    if (option.hasArg()) {
                        assignValueToField(field, cmd.getOptionValue(opt));
                    }
                    else {
                        if (field.getClass().equals(boolean.class) || field.getClass().equals(Boolean.class)) {
                            try {
                                field.set(this, true);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                        else {
                            printUsage();
                        }
                    }
                }
                else {
                    if (option.required()) {
                        printUsage();
                    }
                }

                field.setAccessible(true);
                kvMap.put(opt, field.get(this).toString());
            }

            // check that the values read are valid
            // this code is specific to your app
            validate();

            // build a debug string
            StringBuilder sb = new StringBuilder();
            for (Entry<String, String> e : kvMap.entrySet()) {
                sb.append(e.getKey()).append(" = ").append(e.getValue()).append("\n");
            }
            configDump = sb.toString();
        }

        catch (Exception e) {
            System.err.println("Parsing failed. Reason: " + e.getMessage());
            printUsage();
            System.exit(-1);
        }
    }

    public void validate() {}

    public String getConfigDumpString() {
        return configDump;
    }
}

@@ -3355,32 +3355,56 @@
 %0A        else if
+ (value.length() == 1 &&
  ((cls == char.c
@@ -3429,32 +3429,33 @@
 haracter.class))
+)
 %0A        %09field.
@@ -3494,16 +3494,18 @@
     else
+ %7B
 %0A       
@@ -3509,26 +3509,202 @@
     
-    assert(false);
+%09System.err.println(%22Parsing failed. Reason: can not assign %22 + value + %22 to %22%0A        %09%09%09+ cls.toString() + %22 class%22);%0A        %09printUsage();%0A            System.exit(-1);%0A        %7D%0A            
 %0A   

print out a error message other than assertion failure if the parser can not assign value correctly