// Copyright 1999 Memesoft.  All rights reserved.

package krb5.lib.util;

import java.util.*;

/**
 * Parses command line arguments
 */
public class Getopt {

	/**
	 * Holds the options and associated values
	 *
	 * @see java.util.Dictionary
	 */
    Dictionary optDict = new Hashtable();

	/**
	 * Enumerator through optDict
	 *
	 * @see java.util.Dictionary
	 */
    Enumeration e;

	/**
	 * Holds the arguments not associated with particular options
	 *
	 * @see java.lang.String
	 */
    String[] optStrList;

	/**
	 * Indicates that an invalid option was provided
	 *
	 * @see java.lang.String
	 */
    boolean invalidOptionsExist;

	/**
	 * Class constructor
	 *
	 * @param opts is of type String
	 * @param args is of type String[]
	 */
    public Getopt(String opts, String[] args) {
        Vector vector = new Vector();
        int i = 0;
        while (i < args.length) {
            if (args[i].charAt(0) == '-') {
                int a = opts.indexOf(args[i].charAt(1));
                if (a >= 0) {
                    String opt = String.valueOf(opts.charAt(a));
                    if (a < opts.length() - 1 &&
                        opts.charAt(a + 1) == ':' &&
                        i + 1 < args.length) {
                        optDict.put(opt, args[i + 1]);
                        i += 2;
                        continue;
                    }
                    optDict.put(opt, new String());
                } else
                    invalidOptionsExist = true;
            } else
                vector.addElement(args[i]);
            ++i;
        }
        optStrList = new String[vector.size()];
		vector.copyInto(optStrList);

    }

	/**
	 * Returns option table
	 *
	 * @return Dictionary is a result
	 */
    public Dictionary getOptTable() {
        return optDict;
    }

	/**
	 * Returns first option
	 *
	 * @return String is a result
	 */
    public String firstOpt() {
        resetOpt();
        if (e.hasMoreElements()) {
            return (String)e.nextElement();
        } 
        return null;
    }

	/**
	 * Returns next option
	 *
	 * @return String is a result
	 */
    public String nextOpt() {
        if (e.hasMoreElements()) {
            return (String)e.nextElement();
        }
        return null;
    }

	/**
	 * Resets option table
	 */
    public void resetOpt() {
        e = optDict.keys();
    }

	/**
	 * Returns option argument
	 *
	 * @return String is a result
	 * @param opt is of type String
	 */
    public String getOptArg(String opt) {
        return (String)optDict.get(opt);
    }

	/**
	 * Returns trailing arguments
	 *
	 * @return String[] is a result
	 */
    public String[] getTrailingArgs() {
        return optStrList;
    }

	/**
	 * Indicates whether it is an invalid option
	 *
	 * @return boolean is a result
	 */
    public boolean invalidOptions() {
        return invalidOptionsExist;
    }

	/**
	 * Main
	 *
	 * @param arg is of type String[]
	 */
    public static void main(String arg[]) {
        String optStr = "rp:s:t";
        Getopt getopt = new Getopt(optStr, arg);
        String[] tl = getopt.getTrailingArgs();
        System.out.println("trailing args");
        for (int i = 0; i < tl.length; i++) {
            System.out.println(tl[i]);
        }
        System.out.println("dictionary ");
        Dictionary adict = getopt.getOptTable();
        Enumeration k = adict.keys();
        for (Enumeration e = adict.elements()  ; e.hasMoreElements();) {
            System.out.print(k.nextElement() + "\t");
            System.out.println(e.nextElement());
        }     
        
        System.out.println("First opt " + getopt.firstOpt());
        System.out.println("Next opt " + getopt.nextOpt());
        getopt.resetOpt();
        System.out.println("After reset " + getopt.nextOpt());
        System.out.println("s is " + getopt.getOptArg(String.valueOf("s")));


    }
}