package etomica.util;

import java.io.File;
import java.lang.reflect.Field;

/**
 * Class that handles parsing of commandline arguments.  Arguments are used to
 * assign fields in the parameterWrapper, much as is done in ReadParameters for
 * reading input files.
 * 
 * @author Andrew Schultz
 */
public class ParseArgs {

    public ParseArgs() {
    }
    
    public ParseArgs(ParameterBase parameterWrapper) {
        setParameterWrapper(parameterWrapper);
    }
    
    /**
     * Returns the parameter wrapper.
     */
    public ParameterBase getParameterWrapper() {
        return wrapper;
    }

    /**
     * Sets the parameterWrapper
     */
    public void setParameterWrapper(ParameterBase newParameterWrapper) {
        wrapper = newParameterWrapper;
        fields = wrapper.getClass().getFields();
    }

    /**
     * Parses each argument and attempts to match it with a field from
     * parameterWrapper and sets the field to the value from the next argument.
     * If the next argument is another option (or if there are no more
     * options), the value is taken to be 'true' with the hope that the field
     * is a boolean.
     * Options are accepted in the form "-numSteps" or "--numSteps".
     * This routine handles boolean, int, long, double, String.  Arrays are
     * handled if they are quoted.
     * 
     * -slanty -temperature 1 -alpha "0.3 0.5"
     *  ==> slanty = true;
     *  ==> temperature = 1;
     *  ==> alpha = {0.3, 0.5};
     */
    public void parseArgs(String[] args) {
        parseArgs(args, false);
    }
    
    public void parseArgs(String[] args, boolean firstArgFile) {
        if (firstArgFile) {
            if (new File(args[0]).exists()) {
                ReadParameters paramReader = new ReadParameters(args[0], wrapper);
                paramReader.readParameters();
                args = (String[])Arrays.removeObject(args, args[0]);
            }
        }
        for (int i=0; i<args.length; i++) {
            if (args[i].charAt(0) != '-') {
                throw new RuntimeException("encountered "+args[i]+" when I was expecting an option");
            }
            String token = args[i].replaceAll("^--?", "");
            String value = "";
            if (i+1 == args.length || (args[i+1].charAt(0) == '-' && !Character.isDigit(args[i+1].charAt(1)))) {
                // last argument or next argument is another option.  hope this is a boolean parameter
                value = "true";
            }
            else {
                value = args[i+1];
                i++;
            }

            boolean foundField = false;
            for (int j=0; j<fields.length; j++) {
                if (token.equals(fields[j].getName())) {
                    wrapper.setValue(fields[j],value);
                    foundField = true;
                    break;
                }
                else if (token.equalsIgnoreCase("no"+fields[j].getName()) && value.equals("true")) {
                    wrapper.setValue(fields[j], "false");
                    foundField = true;
                    break;
                }
            }
            if (!foundField) {
                throw new RuntimeException("don't know what to do with token: "+token);
            }
        }
    }
    
    protected ParameterBase wrapper;
    protected Field[] fields;
}

@@ -1916,32 +1916,791 @@
     %7D%0A        %7D%0A
+        if (args.length == 1 && args%5B0%5D.equals(%22-help%22) %7C%7C args%5B0%5D.equals(%22-h%22)) %7B%0A            String strOut = %22options and defaults: %22;%0A            int len = strOut.length();%0A            System.out.print(strOut);%0A            for (int i=0; i%3Cfields.length; i++) %7B%0A                try %7B%0A                    strOut = %22 -%22+fields%5Bi%5D.getName()+%22 %22+fields%5Bi%5D.get(wrapper).toString();%0A                %7D%0A                catch (IllegalAccessException e) %7B%7D%0A                len += strOut.length();%0A                if (len %3E 80) %7B%0A                    len = strOut.length()+5;%0A                    strOut = %22%5Cn     %22+strOut;%0A                %7D%0A                System.out.print(strOut);%0A            %7D%0A            System.out.println();%0A            System.exit(0);%0A        %7D%0A
         for (int

handle -help and -h