Projekt

Obecné

Profil

Stáhnout (5.93 KB) Statistiky
| Větev: | Tag: | Revize:
1
package jdeserialize;
2
import java.io.*;
3
import java.util.*;
4

    
5
/**
6
 * Simple getopt()-like implementation.
7
 */
8
public class Getopt {
9
    private Map<String, Integer> options;
10
    private List<String> otherargs;
11
    private Map<String, String> descriptions;
12
    private Map<String, List<String>> optvals;
13

    
14
    public class OptionParseException extends Exception {
15
        public static final long serialVersionUID = 2898924890585885551L;
16
        public OptionParseException(String message) {
17
            super(message);
18
        }
19
    }
20

    
21
    /**
22
     * Gets the list arguments specified that were *not* options or their arguments, in
23
     * the order they were specified.
24
     * 
25
     * @return the list of non-option String arguments
26
     */
27
    public List<String> getOtherArguments() {
28
        return otherargs;
29
    }
30

    
31
    /**
32
     * Gets the set of all options specified, as well as the list of their arguments.
33
     *
34
     * @return a map of all options specified; values are lists of arguments
35
     */
36
    public Map<String, List<String>> getOptionValues() {
37
        return optvals;
38
    }
39

    
40
    /**
41
     * Constructor.  
42
     *
43
     * @param options Map of options to parse.  The key should be an option string (including
44
     * any initial dashes), and the value should be an Integer representing the number of
45
     * arguments to parse following the option.
46
     * @param descriptions Map of option descriptions.
47
     */
48
    public Getopt(Map<String, Integer> options, Map<String, String> descriptions) {
49
        this.options = options;
50
        this.descriptions = descriptions;
51
    }
52
    /**
53
     * Constructor.  
54
     */
55
    public Getopt() {
56
        this.options = new HashMap<String, Integer>();
57
        this.descriptions = new HashMap<String, String>();
58
    }
59

    
60
    /**
61
     * Determines whether or not the option was specified when the arguments were parsed.
62
     *
63
     * @return true iff the argument was specified (with the correct number of arguments).
64
     */
65
    public boolean hasOption(String opt) {
66
        return optvals.containsKey(opt);
67
    }
68

    
69
    /**
70
     * Gets the list of arguments for a given option, or null if the option wasn't
71
     * specified.
72
     * 
73
     * @param option the option 
74
     * @return the list of arguments for option
75
     */
76
    public List<String> getArguments(String option) {
77
        return optvals.get(option);
78
    }
79

    
80
    /**
81
     * Add an option to the internal set, including the number of arguments and the
82
     * description. 
83
     *
84
     * @param option option string, including any leading dashes
85
     * @param arguments number of arguments
86
     * @param description description of the option
87
     */
88
    public void addOption(String option, int arguments, String description) {
89
        options.put(option, arguments);
90
        descriptions.put(option, description);
91
    }
92

    
93
    /**
94
     * Do the parsing/validation.
95
     * @param args arguments to parse
96
     * @throws OptionParseException if a parse error occurs (the exception message will
97
     * have details)
98
     */
99
    public void parse(String[] args) throws OptionParseException {
100
        this.otherargs = new ArrayList<String>();
101
        this.optvals = new HashMap<String, List<String>>();
102

    
103
        for(int i = 0; i < args.length; i++) {
104
            if(optvals != null) {
105
                Integer count = options.get(args[i]);
106
                if(count != null) {
107
                    ArrayList<String> al = new ArrayList<String>(count.intValue());
108
                    for(int j = 0; j < count; j++) {
109
                        if(i+1+j >= args.length) {
110
                            throw new OptionParseException("expected " + count + " arguments after " + args[i]);
111
                        }
112
                        al.add(args[i+1+j]);
113
                    }
114
                    List<String> oldal = optvals.get(args[i]);
115
                    if(oldal == null) {
116
                        optvals.put(args[i], al);
117
                    } else {
118
                        oldal.addAll(al);
119
                    }
120
                    i += count;
121
                    continue;
122
                }
123
            }
124
            otherargs.add(args[i]);
125
        }
126
    }
127

    
128
    /**
129
     * Get a tabular description of all options and their descriptions, one per line.
130
     */
131
    public String getDescriptionString() {
132
        String linesep = System.getProperty("line.separator");
133
        StringBuffer sb = new StringBuffer();
134
        if(options != null && options.size() > 0) {
135
            sb.append("Options:").append(linesep);
136
            TreeSet<String> opts = new TreeSet<String>(this.options.keySet());
137
            for(String opt: opts) {
138
                sb.append("    ").append(opt);
139
                for(int i = 0; i < options.get(opt); i++) {
140
                    sb.append(" arg").append(i+1);
141
                }
142
                sb.append(": ").append(descriptions.get(opt)).append(linesep);
143
            }
144
        }
145
        return sb.toString();
146
    }
147

    
148
    public static void main(String[] args) {
149
        try {
150
            HashMap<String, Integer> options = new HashMap<String, Integer>();
151
            Getopt go = new Getopt();
152
            go.addOption("-optzero", 0, "zero-arg constructor");
153
            go.addOption("-optone", 1, "one-arg constructor");
154
            go.addOption("-opttwo", 2, "two-arg constructor");
155
            go.parse(args);
156
            System.out.println(go.getDescriptionString());
157
            System.out.println("options:");
158
            Map<String, List<String>> optvals = go.getOptionValues();
159
            for(String opt: optvals.keySet()) {
160
                System.out.print("    " + opt);
161
                for(String optval: optvals.get(opt)) {
162
                    System.out.print(" " + optval);
163
                }
164
                System.out.println("");
165
            }
166
            System.out.println("");
167
            System.out.println("otherargs:");
168
            for(String arg: go.getOtherArguments()) {
169
                System.out.println("    " + arg);
170
            }
171
        } catch (Throwable t) {
172
            t.printStackTrace();
173
        }
174
    }
175
}
(2-2/20)