View Single Post
Old 07-15-2003, 07:34 AM   #8 (permalink)
technobard
Centurion Nova Prime
 
technobard's Avatar
 
Join Date: May 2002
Location: Oak Park, IL (USA)
Posts: 287
technobard is on a distinguished road
Mine takes:
Code:
java myprog propfile=filename user=techie pass=lin32 -y -b -u
propfile contains a list of name=value entries. It can also take comments.

I included a main to show basic use.
Code:
import java.util.*;
import java.io.*;

public class CommandLine {
    StringTokenizer st;
    Hashtable ht;      //stores cmdline name-value pairs like "sid=actm03p"
    Vector switches;   //stores cmdline switches like "-n"
    String [] cmdstr;  // stores args[] from raw cmdline
    
    /** Creates a new instance of CommandLine */
    public CommandLine(String[] cmdstr) {
        this.cmdstr = cmdstr;
        ht = new Hashtable();
        switches = new Vector();
        parse();
        propFile();
    }
    
    public boolean parse() {
        String key = "";
        String value = "";
        int cnt = 0;
        boolean flag = false;
        
        for (int i=0; i<cmdstr.length; i++) {
            st = new StringTokenizer(cmdstr[i], "=");
            cnt = st.countTokens();
            if (cnt == 2) {
                key = st.nextToken();
                value = st.nextToken();
                ht.put(key, value);
                flag = true;
            } else if (cnt == 1) {
                switches.add(st.nextToken());
                flag=true;
            } else {
                flag = false;
            }
        }
        return flag;
    } // end of parse
    
    public void propFile() {
        String key = "";
        String value = "";
        
        if (checkKey("propfile")) {
            // Read properties file.
            Properties properties = new Properties();
            try {
                properties.load(new FileInputStream(getValue("propfile")));
                Iterator it = properties.keySet().iterator();
        
                //loop through properties and add to ht (hashtable) if not already present
                while (it.hasNext()) {
                    key = (String)it.next();
                    value = (String)properties.get(key);
                    if (!checkKey(key)) {
                        ht.put(key, value);
                    }
                }
                properties.clear();  // empty properties object
            } catch (IOException e) {
                System.out.println("Failed propFile..." + e);
                System.exit(2);
            }
        }
    }  // end of propFile
    
    
    public void printPairs() {
        String key = "";
        String value = "";
        
        Iterator it = ht.keySet().iterator();
        
        while (it.hasNext()) {
            key = (String)it.next();
            value = (String)ht.get(key);
            System.out.println(key + "=" + value);
        }
    } // end of printPairs
    
    public void printSwitches() {
        System.out.println("");
        System.out.println("Switches = " + switches.toString());
    } // end of printSwitches
    
    public String getValue(String key) {
        return (String)ht.get(key);
    }
    
    public boolean checkSwitch(String sw) {
        return switches.contains(sw);
    }
    
    public boolean checkKey(String key) {
        return ht.containsKey(key);
    }
    
    public static void main(String[] args) {
        CommandLine cl = new CommandLine(args);
        cl.printPairs();
        cl.printSwitches();
        // cl.getValue(somekey) returns the "value" of any name/value pair
       //cl.checkSwitch("-y") returns true if "-y" was included on the commandline
    }
}
technobard is offline   Reply With Quote