1 /*
2  * @(#) $Id: JSTKOptions.java,v 1.3 2003/07/08 08:13:52 pankaj Exp $
3  *
4  * Copyright (c) 2002-03 by Pankaj Kumar (http://www.pankaj-k.net). 
5  * All rights reserved.
6  *
7  * The license governing the use of this file can be found in the 
8  * root directory of the containing software.
9  */
10package org.jstk;
11
12import java.util.HashMap;
13import java.util.Vector;
14
15public class JSTKOptions implements JSTKArgs {
16    private HashMap options = new HashMap();
17    private HashMap defaults = null;
18    private Vector pArgs = new Vector();    // Positional Args.
19
20    public void parse(String[] args, int beginIndex){
21        String key, value;
22        int index = beginIndex;
23        while (args.length > index){
24            value = "true";
25            if (args[index].startsWith("-")){
26                key = args[index++].substring(1);
27                if (args.length > index && !args[index].startsWith("-"))
28                    value = args[index++];
29                set(key, value);
30            } else {
31                pArgs.add(args[index]);
32                ++index;
33            }
34        }
35    }
36
37    public int getNum(){
38        return pArgs.size();
39    }
40
41    public void setDefaults(HashMap defaults){
42        this.defaults = defaults;
43    }
44    public String get(String name){
45        String value = (String)options.get(name);
46        if (value == null && defaults != null)
47            value = (String)defaults.get(name);
48        return value;
49    }
50    public String get(int pos){
51        return (String)pArgs.elementAt(pos);
52    }
53
54    public void set(String name, String value){
55        options.put(name, value);
56    }
57}