* parameters.
*/
public Map parse(String[] args) throws ParseException {
Map options = new LinkedHashMap();
Iterator params = parameterFormat.values().iterator();
ArgumentFormat lastFormat = null;
for (int i=0; i<args.length; i++) {
if (args[i].length() == 0) {
continue;
}
ArgumentFormat format;
if (args[i].charAt(0) == '-') {
String option = args[i].substring(1);
format = (ArgumentFormat) optionFormat.get(option);
if (format == null) {
throw new ParseException("Unknown option '"+option+"' at position "+i, i);
}
}
else {
format = params.hasNext() ? (ArgumentFormat) params.next() :
((lastFormat != null) && (lastFormat.isVariableLength())) ? lastFormat : null;
if (format == null) {
throw new ParseException("Unrecognized parameter at position "+i, i);
}
}
if ((format.getParameters() != null) &&
(format.getParameters().length > 0)) {
int diff = (format.isParameter()) ? 1 : 0;
List values = parseValues(args, i+(1-diff), format);
i += Math.max(values.size() - diff, 0);
if (format.isVariableLength() &&
options.containsKey(format.getOption())) {
List extValues = (List)options.get(format.getOption());
extValues.addAll(values);
}
else {
addValues2Option(format.getOption(), values, options);
}
}
else {
addValues2Option(format.getOption(), null, options);
}
lastFormat = format;
}
while (params.hasNext()) {
ArgumentFormat af = (ArgumentFormat) params.next();
if (af.isMandatory()) {
throw new ArgumentParseException(-1, null, af, af.getParameters()[0]);
}
}
for (Iterator it = optionFormat.values().iterator(); it.hasNext();) {
ArgumentFormat of = (ArgumentFormat) it.next();
if (of.isMandatory() && !options.containsKey(of.getOption())) {
List defaults = new ArrayList();
for (int i=0; i<of.getParameters().length; i++) {
if (of.getParameters()[i].getDefaultValue() != null) {
defaults.add(parseParameterValue(of.getParameters()[i],
of.getParameters()[i].getDefaultValue(),
of, i));
}
}
if (defaults.size() == 0) {
throw new ArgumentParseException( -1, null, of, of.getParameters()[0]);
}
else {
addValues2Option(of.getOption(), defaults, options);
}
}
}
return options;
}