1
|
package jdeserialize;
|
2
|
import java.util.*;
|
3
|
|
4
|
/**
|
5
|
* Simple getopt()-like implementation.
|
6
|
*/
|
7
|
public class Getopt {
|
8
|
private Map<String, Integer> options;
|
9
|
private List<String> otherargs;
|
10
|
private Map<String, String> descriptions;
|
11
|
private Map<String, List<String>> optvals;
|
12
|
|
13
|
public class OptionParseException extends Exception {
|
14
|
public static final long serialVersionUID = 2898924890585885551L;
|
15
|
public OptionParseException(String message) {
|
16
|
super(message);
|
17
|
}
|
18
|
}
|
19
|
|
20
|
/**
|
21
|
* Gets the list arguments specified that were *not* options or their arguments, in
|
22
|
* the order they were specified.
|
23
|
*
|
24
|
* @return the list of non-option String arguments
|
25
|
*/
|
26
|
public List<String> getOtherArguments() {
|
27
|
return otherargs;
|
28
|
}
|
29
|
|
30
|
/**
|
31
|
* Gets the set of all options specified, as well as the list of their arguments.
|
32
|
*
|
33
|
* @return a map of all options specified; values are lists of arguments
|
34
|
*/
|
35
|
public Map<String, List<String>> getOptionValues() {
|
36
|
return optvals;
|
37
|
}
|
38
|
|
39
|
/**
|
40
|
* Constructor.
|
41
|
*
|
42
|
* @param options Map of options to parse. The key should be an option string (including
|
43
|
* any initial dashes), and the value should be an Integer representing the number of
|
44
|
* arguments to parse following the option.
|
45
|
* @param descriptions Map of option descriptions.
|
46
|
*/
|
47
|
public Getopt(Map<String, Integer> options, Map<String, String> descriptions) {
|
48
|
this.options = options;
|
49
|
this.descriptions = descriptions;
|
50
|
}
|
51
|
/**
|
52
|
* Constructor.
|
53
|
*/
|
54
|
public Getopt() {
|
55
|
this.options = new HashMap<String, Integer>();
|
56
|
this.descriptions = new HashMap<String, String>();
|
57
|
}
|
58
|
|
59
|
/**
|
60
|
* Determines whether or not the option was specified when the arguments were parsed.
|
61
|
*
|
62
|
* @return true iff the argument was specified (with the correct number of arguments).
|
63
|
*/
|
64
|
public boolean hasOption(String opt) {
|
65
|
return optvals.containsKey(opt);
|
66
|
}
|
67
|
|
68
|
/**
|
69
|
* Gets the list of arguments for a given option, or null if the option wasn't
|
70
|
* specified.
|
71
|
*
|
72
|
* @param option the option
|
73
|
* @return the list of arguments for option
|
74
|
*/
|
75
|
public List<String> getArguments(String option) {
|
76
|
return optvals.get(option);
|
77
|
}
|
78
|
|
79
|
/**
|
80
|
* Add an option to the internal set, including the number of arguments and the
|
81
|
* description.
|
82
|
*
|
83
|
* @param option option string, including any leading dashes
|
84
|
* @param arguments number of arguments
|
85
|
* @param description description of the option
|
86
|
*/
|
87
|
public void addOption(String option, int arguments, String description) {
|
88
|
options.put(option, arguments);
|
89
|
descriptions.put(option, description);
|
90
|
}
|
91
|
|
92
|
/**
|
93
|
* Do the parsing/validation.
|
94
|
* @param args arguments to parse
|
95
|
* @throws OptionParseException if a parse error occurs (the exception message will
|
96
|
* have details)
|
97
|
*/
|
98
|
public void parse(String[] args) throws OptionParseException {
|
99
|
this.otherargs = new ArrayList<String>();
|
100
|
this.optvals = new HashMap<String, List<String>>();
|
101
|
|
102
|
for(int i = 0; i < args.length; i++) {
|
103
|
if(optvals != null) {
|
104
|
Integer count = options.get(args[i]);
|
105
|
if(count != null) {
|
106
|
ArrayList<String> al = new ArrayList<String>(count.intValue());
|
107
|
for(int j = 0; j < count; j++) {
|
108
|
if(i+1+j >= args.length) {
|
109
|
throw new OptionParseException("expected " + count + " arguments after " + args[i]);
|
110
|
}
|
111
|
al.add(args[i+1+j]);
|
112
|
}
|
113
|
List<String> oldal = optvals.get(args[i]);
|
114
|
if(oldal == null) {
|
115
|
optvals.put(args[i], al);
|
116
|
} else {
|
117
|
oldal.addAll(al);
|
118
|
}
|
119
|
i += count;
|
120
|
continue;
|
121
|
}
|
122
|
}
|
123
|
otherargs.add(args[i]);
|
124
|
}
|
125
|
}
|
126
|
|
127
|
/**
|
128
|
* Get a tabular description of all options and their descriptions, one per line.
|
129
|
*/
|
130
|
public String getDescriptionString() {
|
131
|
String linesep = System.getProperty("line.separator");
|
132
|
StringBuffer sb = new StringBuffer();
|
133
|
if(options != null && options.size() > 0) {
|
134
|
sb.append("Options:").append(linesep);
|
135
|
TreeSet<String> opts = new TreeSet<String>(this.options.keySet());
|
136
|
for(String opt: opts) {
|
137
|
sb.append(" ").append(opt);
|
138
|
for(int i = 0; i < options.get(opt); i++) {
|
139
|
sb.append(" arg").append(i+1);
|
140
|
}
|
141
|
sb.append(": ").append(descriptions.get(opt)).append(linesep);
|
142
|
}
|
143
|
}
|
144
|
return sb.toString();
|
145
|
}
|
146
|
|
147
|
public static void main(String[] args) {
|
148
|
try {
|
149
|
Getopt go = new Getopt();
|
150
|
go.addOption("-optzero", 0, "zero-arg constructor");
|
151
|
go.addOption("-optone", 1, "one-arg constructor");
|
152
|
go.addOption("-opttwo", 2, "two-arg constructor");
|
153
|
go.parse(args);
|
154
|
System.out.println(go.getDescriptionString());
|
155
|
System.out.println("options:");
|
156
|
Map<String, List<String>> optvals = go.getOptionValues();
|
157
|
for(String opt: optvals.keySet()) {
|
158
|
System.out.print(" " + opt);
|
159
|
for(String optval: optvals.get(opt)) {
|
160
|
System.out.print(" " + optval);
|
161
|
}
|
162
|
System.out.println("");
|
163
|
}
|
164
|
System.out.println("");
|
165
|
System.out.println("otherargs:");
|
166
|
for(String arg: go.getOtherArguments()) {
|
167
|
System.out.println(" " + arg);
|
168
|
}
|
169
|
} catch (Throwable t) {
|
170
|
t.printStackTrace();
|
171
|
}
|
172
|
}
|
173
|
}
|