* fix args4j argument parsing issues (bug occurred if the format contains = symbols)

@see http://www.filebot.net/forums/viewtopic.php?f=8&t=1726&p=10131#p10131
This commit is contained in:
Reinhard Pointner 2014-06-10 17:05:41 +00:00
parent 32be4d6816
commit 36ef89faa1
2 changed files with 32 additions and 24 deletions

Binary file not shown.

View File

@ -1,63 +1,65 @@
package net.filebot.cli;
import java.util.AbstractMap.SimpleEntry;
import java.util.Map.Entry;
import java.util.LinkedHashMap;
import java.util.Map;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.OptionDef;
import org.kohsuke.args4j.spi.OptionHandler;
import org.kohsuke.args4j.spi.FieldSetter;
import org.kohsuke.args4j.spi.MapOptionHandler;
import org.kohsuke.args4j.spi.Parameters;
import org.kohsuke.args4j.spi.Setter;
public class BindingsHandler extends MapOptionHandler {
public class BindingsHandler extends OptionHandler<Entry<String, String>> {
public BindingsHandler(CmdLineParser parser, OptionDef option, Setter<Entry<String, String>> setter) {
public BindingsHandler(CmdLineParser parser, OptionDef option, Setter<? super Map<?, ?>> setter) {
super(parser, option, setter);
}
@Override
public String getDefaultMetaVariable() {
return "[name=value]";
}
@Override
public int parseArguments(Parameters params) throws CmdLineException {
FieldSetter fs = setter.asFieldSetter();
Map map = (Map) fs.getValue();
if (map == null) {
map = createNewCollection(fs.getType());
fs.addValue(map);
}
int pos = 0;
while (pos < params.size()) {
String[] nv = params.getParameter(pos).split("=", 2);
if (nv.length < 2 || nv[0].startsWith("-")) {
return pos;
}
String n = nv[0].trim();
String v = nv[1].trim();
if (!isIdentifier(n)) {
throw new CmdLineException(owner, String.format("\"%s\" is not a valid identifier", n));
}
setter.addValue(new SimpleEntry<String, String>(n, v));
map.put(n, v);
pos++;
}
return pos;
}
public boolean isIdentifier(String n) {
if (n.isEmpty())
return false;
for (int i = 0; i < n.length();) {
int c = n.codePointAt(i);
if (i == 0) {
if (!Character.isUnicodeIdentifierStart(c))
return false;
@ -65,10 +67,16 @@ public class BindingsHandler extends OptionHandler<Entry<String, String>> {
if (!Character.isUnicodeIdentifierPart(c))
return false;
}
i += Character.charCount(c);
}
return true;
}
@Override
protected Map createNewCollection(Class<? extends Map> type) {
return new LinkedHashMap();
}
}